1.输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l。
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <dirent.h>
#include <errno.h>
char *getUName(__uid_t uid)
{
struct passwd *pwd = getpwuid(uid);
if (NULL == pwd)
{
perror("getpwuid");
return NULL;
}
return pwd->pw_name;
}
char get_type(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';
}
else
{
return '\0';
}
}
char *get_filePermssion(mode_t m)
{
char per[] = "rwx";
static char ptr[10] = "";
for (int i = 0; i < 9; i++)
{
if ((m & (0400 >> i)) == 0)
{
ptr[i] = '-';
}
else
{
ptr[i] = per[i % 3];
}
}
return ptr;
}
int info(char *file)
{
struct stat buf;
struct tm *info = NULL;
if (stat(file, &buf) < 0)
{
perror("stat");
return -1;
}
info = localtime(&buf.st_ctime);
printf("%c%s %ld %s %s %ld %d月 %02d %02d:%02d %s\n",
get_type(buf.st_mode),
get_filePermssion(buf.st_mode),
buf.st_nlink,
getUName(buf.st_uid),
getUName(buf.st_gid),
buf.st_size,
info->tm_mon + 1,
info->tm_mday,
info->tm_hour,
info->tm_min,
file);
return 0;
}
char *ls(DIR *dp)
{
if (NULL == dp)
{
perror("opendir");
return NULL;
}
int count = 0;
struct dirent *readptr = NULL;
while (1)
{
readptr = readdir(dp);
if (NULL == readptr)
{
if (0 == errno)
{
// printf("目录读取完毕\n");
break;
}
else
{
perror("readdir");
return NULL;
}
}
// char name[20];
// strcpy(name,readptr->d_name);
if (readptr->d_name[0] == '.')
{
continue;
}
// printf("[%d] %s\n", ++count, readptr->d_name);
return readptr->d_name;
}
return 0;
}
int main(int argc, const char *argv[])
{
// 命令行不传参默认使用本目录
char dir[40] = "./";
if (argc > 1)
{
// printf("v=%s\n", argv[1]);
strcpy(dir, argv[1]);
}
char *res;
char file[50] = "";
int count = 0;
DIR *dp = opendir(dir);
// printf("dir=%s\n", dir);
// char file[20] = "1.txt";
while (1)
{
res = ls(dp);
if (res == NULL)
{
break;
}
strcpy(file, dir);
strcat(file, res);
// puts(file);
// printf("file=%s\n", file);
if (info(file))
{
printf("信息列出失败\n");
}
memset(file, 0, sizeof(file));
count++;
}
printf("共%d个文件", count);
// 关闭
if (closedir(dp) < 0)
{
perror("closedir");
return 0;
}
}
2.拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int fd_r = open("1.png", O_RDONLY);
int fd_w = open("2.png", O_RDWR | O_CREAT | O_TRUNC, 0777);
if (fd_r < 0 || fd_w < 0)
{
perror("open");
return -1;
}
char buf;
ssize_t res = 0;
int len = lseek(fd_r, 0, SEEK_END);
if ((len % 2) != 0)
{
len++;
}
int half_r = len / 2;
printf("half_r=%d", half_r);
printf("正在复制\n");
pid_t cpid = fork();
if (cpid > 0)
{ // 父进程
lseek(fd_r, 0, SEEK_SET);
lseek(fd_w, 0, SEEK_SET);
for (int i = 0; i < half_r; i++)
{
res = read(fd_r, &buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
write(fd_w, &buf, res);
}
}
else
{ // 子进程
sleep(10);
for (int i = 0; i < half_r; i++)
{
res = read(fd_r, &buf, sizeof(buf));
if (res < 0)
{
perror("read");
return -1;
}
write(fd_w, &buf, res);
}
}
if (close(fd_r) < 0 || close(fd_w) < 0)
{
perror("close");
return -1;
}
printf("复制成功\n");
return 0;
}