1.输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <dirent.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include<time.h>
#include<pwd.h>
#include <grp.h>
/*
*function : 获取文件类型
*@param [in]:
*@param [out]:
*@return :
*/
void get_filetype(mode_t m){ //m == buf.st_mode
switch (m & S_IFMT) {
case S_IFBLK: printf("b");break;
case S_IFCHR: printf("c");break;
case S_IFDIR: printf("d");break;
case S_IFIFO: printf("p");break;
case S_IFLNK: printf("l");break;
case S_IFREG: printf("-");break;
case S_IFSOCK: printf("s");break;
}
}
/*
*function : 获取文件权限
*@param [in]:
*@param [out]:
*@return :
*/
void get_filemode(mode_t m,char perm[10]){
char str[] = "rwx";
for(int i=0;i<9;i++){
if((m & (0400 >> i)) == 0){
perm[i]='-';
continue;
}
perm[i]=str[i%3];
}
}
/*
*function : 获取文件详细信息
*@param [in]: 文件的名字
*@param [out]:
*@return :
*/
void get_filemessage(char *pathname,char *name){
struct stat buf;
stat(pathname,&buf);
//获取文件类型
get_filetype(buf.st_mode);
//获取文件权限
char perm[10]="";
get_filemode(buf.st_mode,perm);
printf("%s ",perm);
//硬链接数
printf(" %ld ",buf.st_nlink);
//uid
struct passwd *uid = getpwuid(buf.st_uid);
printf("%s ",uid->pw_name);
//gid
struct group *gid = getgrgid(buf.st_gid);
printf("%s ",gid->gr_name);
//文件大小
printf("%ld\t",buf.st_size);
//时间
struct tm* t = localtime(&buf.st_ctime);
printf("%2d月 %02d %02d:%02d\t",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
//文件的名字
printf("%s\n",name);
}
int main(int argc,const char *argv[])
{
if(argc < 2){
printf("请输入目录名\n");
return -1;
}
DIR * dir = opendir(argv[1]);
if(NULL == dir){
perror("opendir");
return -1;
}
while(1){
struct dirent *readper = readdir(dir);
if(NULL == readper){
if(0 == errno){
printf("目录读取完毕\n");
break;
}else{
perror("readdir");
return -1;
}
}
if(readper->d_name[0] == '.')
continue;
char name[100]="";
strcat(name,argv[1]);
strcat(name,readper->d_name);
get_filemessage(name,readper->d_name);
}
closedir(dir);
return 0;
}
2.拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc,const char *argv[])
{
int fd_r = open("./picture.bmp", O_RDONLY);
if(fd_r < 0){
perror("open");
printf("line : %d\n",__LINE__);
return -1;
}
int fd_w = open("./cppicture.bmp", O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd_w < 0){
perror("open");
printf("line : %d\n",__LINE__);
return -1;
}
off_t fd_size = lseek(fd_r, 0, SEEK_END);
pid_t id = fork();
if(id > 0){
//父进程单独运行
// long count = 0;
char c = '\0';
ssize_t res;
lseek(fd_r,0,SEEK_SET);
while(1){
res = read(fd_r, &c,1 );
if(-1 == res){
perror("read");
printf("line : %d\n",__LINE__);
return -1;
}
if(lseek(fd_r,0,SEEK_CUR) == (fd_size/2)){
printf("父进程文件读取完毕\n");
break;
}
res = write(fd_w,&c,1);
if(-1 == res){
perror("write");
printf("line : %d\n",__LINE__);
return -1;
}
}
}else if(id == 0){
//子进程单独运行
sleep(1);//自动放弃CPU资源,为了保证父进程先运行
char s[128] = "";
ssize_t res;
lseek(fd_r,(fd_size/2)-1,SEEK_SET);
while(1){
bzero(s,sizeof(s));
res = read(fd_r, s,sizeof(s));
if(-1 == res){
perror("read");
printf("line : %d\n",__LINE__);
return -1;
}
if(0 == res){
printf("子进程文件读取完毕\n");
break;
}
res = write(fd_w,s,res);
if(-1 == res){
perror("write");
printf("line : %d\n",__LINE__);
return -1;
}
}
}else{
perror("fork");
return -1;
}
sleep(1);
close(fd_r);
close(fd_w);
return 0;
}