作业:
1.输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l。
2.拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。
1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
char *get_file(mode_t m, char name[])
{
char per[] = "rwx";
for (int i = 0; i < 9; i++)
{
if (m & (0400 >> i))
{
// if (0 == i % 3)
// putchar('r');
// else if (1 == i % 3)
// putchar('w');
// else if (2 == i % 3)
// putchar('x');
name[i] = per[(i % 3)];
}
else
{
name[i] = '-';
}
}
return name;
}
int getall(char *file)
{
struct stat buf;
char static name[10] = "";
if (stat(file, &buf) < 0)
{
perror("sten");
return -1;
}
struct tm *info = localtime(&buf.st_mtime);
struct passwd *pwd = getpwuid(buf.st_uid);
if (NULL == pwd)
{
perror("uid");
return -1;
}
struct group *grp = getgrgid(buf.st_gid);
if (NULL == grp)
{
perror("gid");
return -1;
}
get_file(buf.st_mode, name);
printf("-%s ", name);
printf("%ld ", buf.st_nlink);
printf("%s ", pwd->pw_name);
printf("%s ", grp->gr_name);
printf("%9ld ", buf.st_size);
printf("%d-%02d-%02d %02d:%02d:%02d ",
info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min, info->tm_sec);
return 0;
}
int main(int argc, const char *argv[])
{
DIR *dp = opendir(argv[1]);
char name[30] = "";
if (NULL == dp)
{
printf("line:%d", __LINE__);
perror("opendir");
return -1;
}
struct dirent *readptr = NULL;
while (1)
{
readptr = readdir(dp);
if (NULL == readptr)
{
if (!errno)
{
printf("finished\n");
return 0;
}
else
{
printf("line:%d", __LINE__);
perror("readir");
return -1;
}
}
if ('.' == readptr->d_name[0])
continue;
strcpy(name, argv[1]);
if (name[strlen(name) - 1] != '/')
strcat(name, "/");
strcat(name, readptr->d_name);
getall(name);
printf("%s\n", readptr->d_name);
}
if (closedir(dp) < 0)
{
perror("closedir");
return -1;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d", __LINE__); \
perror(msg); \
} while (0)
int main(int argc, const char *argv[])
{
int a = 10;
int fd_r = open(argv[1], O_RDONLY);
int fd_w = open("./copy.png", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd_r < 0 || fd_w < 0)
{
ERR_MSG("open");
return -1;
}
pid_t cpid = fork();
if (cpid > 0)
{
char str[128];
int size = lseek(fd_r, 0, SEEK_END);
printf("%d\n",size);
ssize_t res;
int sum = 0;
lseek(fd_r,0,SEEK_SET);
while (1)
{
res = read(fd_r, str, sizeof(str));
write(fd_w, str, res);
sum += res;
if (sum>=size/2)
{
printf("%d\n",sum);
printf("父函数copy完毕 line:%d\n", __LINE__);
break;
}
else if (res < 0)
{
ERR_MSG("read");
return 0;
}
}
}
else if (0 == cpid)
{
sleep(1);
char str[128];
while (1)
{
ssize_t res = read(fd_r, str, sizeof(str));
if (0 == res)
{
printf("子函数copy完毕 line:%d\n", __LINE__);
break;
}
else if (res < 0)
{
perror("read");
return 0;
}
write(fd_w, str, res);
}
}
if (close(fd_r) < 0)
{
ERR_MSG("close");
return -1;
}
if (close(fd_w) < 0)
{
ERR_MSG("close");
return -1;
}
while (1)
sleep(1);
return 0;
}