题目:
题目一:列出指定目录的属性表
题目二:子进程先拷贝一张图片后半部分,父进程后拷贝后半部分
代码:
题目一代码:
#include<dirent.h>
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include <pwd.h>
#include <grp.h>
void get_FTime(struct stat buf)
{
char *mon[12]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一","十二"};
struct tm* info=NULL;
info=localtime(&buf.st_ctime);
printf(" %s %d %02d:%02d",mon[info->tm_mon],info->tm_mday,info->tm_hour,info->tm_min);
}
void get_FG(struct stat buf)
{
struct group* grp = getgrgid(buf.st_gid);
if(NULL == grp)
{
perror("getgrgid");
return;
}
printf(" %s", grp->gr_name);
}
void get_FU(struct stat buf)
{
struct passwd* pwd = getpwuid(buf.st_uid);
if(NULL == pwd)
{
perror("getpwuid");
return ;
}
printf(" %s", pwd->pw_name);
}
char *get_FP(mode_t mode,char pstr[10])
{
int a=0400;
char b[3]="rwx";
int i=0,j=0;
while(a!=0)
{
if((mode&a)!=0) pstr[j]=b[i];
else pstr[j]='-';
a=a>>1;
i++;
if(i==3)i=0;
j++;
}
return pstr;
}
char get_FT(mode_t mode)
{
if(S_ISREG(mode)) return '-';
else if(S_ISDIR(mode)) return 'd';
else if(S_ISCHR(mode)) return 'c';
else if(S_ISBLK(mode)) return 'b';
else if(S_ISFIFO(mode)) return 'p';
else if(S_ISLNK(mode)) return 'l';
else if(S_ISSOCK(mode)) return 's';
}
void character(const char *argv)
{
char pst[10]=" ";
struct stat buf;
if(stat(argv,&buf)<0)
{
perror("stat");
return;
}
printf("%c",get_FT(buf.st_mode));
printf("%s",get_FP(buf.st_mode,pst));
printf(" %ld",buf.st_nlink);
get_FU(buf);
get_FG(buf);
printf(" %6ld",buf.st_size);
get_FTime(buf);
printf(" %2s\n",argv);
}
int main(int argc,const char *argv[])
{
#if 1
DIR *dp=opendir(argv[1]);
if(NULL==dp)
{
perror("opendir");
return -1;
}
struct dirent *rp=NULL;
while(1)
{
rp=readdir(dp);
if(NULL==rp)
{
if(errno!=0)
{
perror("readdir");
return -1;
}
else
{
printf("目录读取完毕\n");
break;
}
}
if(rp->d_name[0]!='.')
{
character(rp->d_name);
}
}
if(closedir(dp)<0)
{
perror("closedir");
return -1;
}
#endif
return 0;
}
题目二代码:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//以读的方式打开源文件
int fd_r = open(argv[1], O_RDONLY);
if(fd_r < 0)
{
perror("open");
return -1;
}
//以写的方式打开目标文件
int fd_w = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd_w < 0)
{
perror("open");
return -1;
}
//从源文件获取数据,写入到目标文件中
//直到文件中的数据读取完毕
char buf[1] = "";
ssize_t res = 0;
off_t ress = lseek(fd_r, 0, SEEK_END);
off_t ress_now=0;
pid_t pid=fork();
if(pid>0)
{
sleep(1);
ress_now=lseek(fd_r, 0, SEEK_SET);
lseek(fd_w,0,SEEK_SET);
while(ress_now<ress/2)
{
bzero(buf, sizeof(buf));
//注意读多少写多少
res = read(fd_r, buf, sizeof(buf));
ress_now=lseek(fd_r, 0, SEEK_CUR);
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("文件拷贝完毕\n");
break;
}
if(write(fd_w, buf, res) < 0)
{
perror("write");
return -1;
}
}
}
else if(pid==0)
{
ress_now=lseek(fd_r, -(ress/2), SEEK_END);
lseek(fd_w,(ress/2),SEEK_SET);
while(ress_now<ress)
{
bzero(buf, sizeof(buf));
//注意读多少写多少
res = read(fd_r, buf, sizeof(buf));
ress_now=lseek(fd_r, 0, SEEK_CUR);
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
printf("文件拷贝完毕\n");
break;
}
if(write(fd_w, buf, res) < 0)
{
perror("write");
return -1;
}
}
}
else
{
perror("fork");
return -1;
}
//while(1)sleep(1);
//关闭文件
close(fd_r);
close(fd_w);
return 0;
}
运行结果:
结果一:
ubuntu@ubuntu:12.8$ ./a.out ./
-rwxrwxr-x 1 ubuntu ubuntu 13208 十二 8 18:27 a.out
-rw-rw-r-- 1 ubuntu ubuntu 1786 十二 8 16:50 01_opendir.c
-rw-rw-r-- 1 ubuntu ubuntu 186176 十二 8 16:50 b.c
-rw-rw-r-- 1 ubuntu ubuntu 20 十二 8 17:17 a.c
-rw-rw-r-- 1 ubuntu ubuntu 2504 十二 8 18:27 opendir.c
-rwxrwxr-x 1 ubuntu ubuntu 186176 十二 8 16:06 a0be0d9cb07fef8959db3e47acae1c79.jpeg
-rw------- 1 ubuntu ubuntu 241664 十二 8 09:53 core
目录读取完毕
ubuntu@ubuntu:12.8$ ls -l
总用量 632
-rw-rw-r-- 1 ubuntu ubuntu 1786 十二 8 16:50 01_opendir.c
-rwxrwxr-x 1 ubuntu ubuntu 186176 十二 8 16:06 a0be0d9cb07fef8959db3e47acae1c79.jpeg
-rw-rw-r-- 1 ubuntu ubuntu 20 十二 8 17:17 a.c
-rwxrwxr-x 1 ubuntu ubuntu 13208 十二 8 18:27 a.out
-rw-rw-r-- 1 ubuntu ubuntu 186176 十二 8 16:50 b.c
-rw------- 1 ubuntu ubuntu 241664 十二 8 09:53 core
-rw-rw-r-- 1 ubuntu ubuntu 2504 十二 8 18:27 opendir.c
结果二:
ubuntu@ubuntu:12.8$ ./a.out a0be0d9cb07fef8959db3e47acae1c79.jpeg b.c
ubuntu@ubuntu:12.8$ diff a0be0d9cb07fef8959db3e47acae1c79.jpeg b.c