作业一、
1.输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <error.h>
void my_file(struct stat buf);
int my_dp(char *p, struct stat buf);
char *get_filePermssion(mode_t m, char ptr[10]);
void Aget_filePermssion(mode_t m);
char get_filetype(mode_t M);
int File_owner(mode_t buf);
int File_group(mode_t buf);
void file_time(time_t t, struct stat buf);
int main(int argc, const char *argv[])
{
struct stat buf;
char s[50] = "";
strcpy(s, argv[1]);
my_dp(s, buf);
return 0;
}
char *get_filePermssion(mode_t m, char ptr[10]) // 注意这里ptr[10]只是为了提醒用户需要传入一个容量为10的组
{
char per[] = "rwx";
for (int i = 0; i < 9; i++)
{
if ((m & (0400 >> i)) == 0)
{
ptr[i] = '-';
continue;
}
ptr[i] = per[i % 3];
}
return ptr;
}
char get_filetype(mode_t M)
{
if (S_ISREG(M))
return '-';
else if (S_ISDIR(M))
return 'd';
else if (S_ISCHR(M))
return 'c';
else if (S_ISBLK(M))
return 'b';
else if (S_ISFIFO(M))
return 'P';
else if (S_ISLNK(M))
return 'l';
else if (S_ISSOCK(M))
return 's';
}
int File_owner(mode_t buf)
{
struct passwd *pwd = getpwuid(buf);
if (NULL == pwd)
{
perror("getpwuid");
return -1;
}
printf("%s ", pwd->pw_name);
return 0;
}
int File_group(mode_t buf)
{
struct group *grp = getgrgid(buf);
if (NULL == grp)
{
perror("getgrgrd");
return -1;
}
printf("%s ", grp->gr_name);
return 0;
}
void file_time(time_t t, struct stat buf)
{
time(&t);
struct tm *info = NULL;
info = localtime(&buf.st_ctime);
printf("%5d月 %02d %02d:%02d ",
info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min);
}
void my_file(struct stat buf)
{
char str[10] = "";
char *s = get_filePermssion(buf.st_mode, str);
printf("%s", str);
// 获取文件类型
char filetype = get_filetype(buf.st_mode);
printf("%c", filetype);
// 文件的硬链接数
printf(" %ld ", buf.st_nlink);
// 文件所属用户
File_owner(buf.st_uid);
// 文件所属用户组
File_group(buf.st_gid);
// 文件大小
printf("%5ld", buf.st_size);
// 文件修改的时间
time_t t;
file_time(t, buf);
}
int my_dp(char *p, struct stat buf)
{
//char name[50] = "";
//strcpy(name, p);
DIR *dp = opendir(p);
if (NULL == dp)
{
perror("dp");
return -1;
}
int count = 1;
struct dirent *readptr = NULL;
while (1)
{
readptr = readdir(dp);
if (NULL == readptr)
{
if (0 == error)
{
printf("目录读取完毕\n");
break;
}
else
{
perror("readdir");
return -1;
}
}
if (readptr->d_name[0] != '.')
{
char name[50] = "";
strcpy(name, p);
strcat(name, readptr->d_name);
//printf("name=%s\n", name);
if (stat(name, &buf) < 0)
{
perror("stat");
return -1;
}
my_file(buf);
printf("%s\n", readptr->d_name);
}
}
if (closedir(dp) < 0)
{
perror("closedir");
return -1;
}
}
作业二、
2.拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int dp_r = open("1.jpg", O_RDONLY);
int dp_w = open("2.jpg", O_RDWR | O_CREAT | O_TRUNC, 0777);
if (dp_r < 0)
{
perror("open");
printf("line:%d\n", __LINE__);
return -1;
}
if (dp_w < 0)
{
perror("open");
printf("line:%d\n", __LINE__);
return -1;
}
// 计算1.jpg文件的大小
off_t res = lseek(dp_r, 0, SEEK_END);
res+=1;
int hulf = res / 2;
//printf("%d",res);
// 创建父子进程
pid_t cpid = fork();
char c;
if (cpid > 0 )
{
lseek(dp_r,0,SEEK_SET);
for (int i = 0; i < hulf; i++)
{
if (read(dp_r, &c, sizeof(c)) != 0)
{
write(dp_w, &c, sizeof(c));
}
else
{
return -1;
}
}
}
else if (0 == cpid)
{
sleep(5);
//lseek(dp_r,hulf+1,SEEK_SET);
for (int i = 0; i < hulf-1; i++)
{
if (read(dp_r, &c, sizeof(c)) != 0)
{
write(dp_w, &c, sizeof(c));
}
else
{
return -1;
}
}
}
if (close(dp_w) < 0)
{
perror("close");
return -1;
}
return 0;
}