![](https://img-blog.csdnimg.cn/d3f9ef09b60147998f711f573f73962b.png)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
//拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。
int main(int argc, const char *argv[])
{
pid_t cpid = fork();
int fd_r=open("./1.png",O_RDONLY);
int fd_w=open("./2.png",O_WRONLY | O_CREAT | O_TRUNC,0664);
if(fd_r<0)
{
perror("open");
printf("%d",__LINE__);
return -1;
}
if(fd_w<0)
{
perror("open");
printf("%d",__LINE__);
return -1;
}
char s;
int res = 0;
int half = lseek(fd_r,0,SEEK_END)/2;
lseek(fd_r,0,SEEK_SET);
int d=0;
if(0 != cpid)
{
while(d<half)
{
res = read(fd_r,&s,sizeof(s));
write(fd_w,&s,res);
d++;
}
}
else
{
sleep(1);
lseek(fd_r,half,SEEK_SET);
while((res = read(fd_r,&s,sizeof(s)))>0)
{
write(fd_w,&s,res);
}
}
close(fd_r);
close(fd_w);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<time.h>
#include<pwd.h>
#include<grp.h>
#include<errno.h>
#include<dirent.h>
#include<string.h>
//1.输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l
void get_mode(mode_t m)
{
int i=0;
for(i=0;i<9;i++)
{
if((m & (0400>>i)) == 0)
{
putchar('-');
continue;
}
switch(i%3)
{
case 0:
putchar('r');
break;
case 1:
putchar('w');
break;
case 2:
putchar('x');
break;
}
}
printf(" ");
}
void getFiletyper(mode_t m)
{
if(S_ISREG(m))
putchar('-');
else if(S_ISDIR(m))
putchar('d');
else if(S_ISCHR(m))
putchar('c');
else if(S_ISBLK(m))
putchar('b');
else if(S_ISFIFO(m))
putchar('p');
else if(S_ISLNK(m))
putchar('l');
else if(S_ISSOCK(m))
putchar('s');
}
int main(int argc, const char *argv[])
{
DIR* dir = opendir("./");
if(NULL == dir)
{
perror("opendir");
return -1;
}
while(1)
{
char s[20];
strcpy(s,argv[1]);
struct dirent *dir1=readdir(dir);
struct stat buf;
if(stat(strcat(s,dir1->d_name),&buf)<0)
{
perror("stat");
return -1;
}
if(NULL == dir1)
{
if(0 == errno)
{
printf("读取完毕\n");
break;
}
else
{
perror("readdir");
return -1;
}
}
struct tm *t = NULL;
//文件类型以及文件权限
// printf("mode:%o\n",buf.st_mode);
get_mode(buf.st_mode);
//文件硬件链接数
printf("%ld\t",buf.st_nlink);
//文件所属用户
// printf("uid:%d\n",buf.st_uid);
struct passwd* pwd = getpwuid(buf.st_uid);
if(NULL == pwd)
{
perror("getpwuid");
return -1;
}
printf("%s\t",pwd->pw_name);
// printf("gid:%d\n",buf.st_gid);
struct group* grp = getgrgid(buf.st_gid);
if(NULL == grp)
{
perror("getgrgid");
return -1;
}
printf("%s\t",grp->gr_name);
//文件大小
printf("%ld\t",buf.st_size);
//文件修改时间
// printf("%ld ",buf.st_ctime);
t=localtime(&buf.st_ctime);
printf("%02d月%02d日",t->tm_mon+1,t->tm_mday);
printf("%02d:%02d \t",t->tm_hour,t->tm_min);
printf("%s",dir1->d_name);
printf("\n");
}
return 0;
}