从终端获取一个文件的路径以及名字。
若该文件是目录文件,则将该文件下的所有文件的属性显示到终端,类似ls -l该文件夹
若该文件不是目录文件,则显示该文件的属性到终端上,类似ls -l这单个文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
void get_filePermission(mode_t m)
{
char str[]="rwxrwxrwx"; //256 = 100 000 000
int i=0;
for(i=0;i<9;i++)
{
if(256>>i & m)
printf("%c",str[i]);
else
putchar('-');
}
}
void gettime(mode_t m)
{
time_t t=m;
struct tm* info=NULL;
info=localtime(&t);
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);
}
void get_fileType(mode_t m)
{
switch(m& S_IFMT)
{
case S_IFSOCK: putchar('s'); break;
case S_IFLNK: putchar('l'); break;
case S_IFREG: putchar('-'); break;
case S_IFBLK: putchar('b'); break;
case S_IFDIR: putchar('d'); break;
case S_IFCHR: putchar('c'); break;
case S_IFIFO: putchar('p'); break;
}
return;
}
void output(char *str,char *fp)
{
struct stat buf;
if(stat(str,&buf) < 0)
{
// ERR_MSG("stat");
return ;
}
get_fileType(buf.st_mode);
get_filePermission(buf.st_mode);
printf(" %ld", buf.st_nlink);
struct passwd* pwd = getpwuid(buf.st_uid);
if(NULL == pwd)
{
// ERR_MSG("getpwuid");
return;
}
printf(" %s",pwd->pw_name);
struct group* grpgid = getgrgid(buf.st_gid);
if(NULL == grpgid)
{
// ERR_MSG("getgruid");
return;
}
printf(" %s",grpgid->gr_name);
// output(str);
printf(" %ld", buf.st_size);
gettime(buf.st_ctime);
printf(" %s\n",fp);
}
int panduan(char *str,struct stat buf)
{
if(stat(str,&buf)<0)
{
// ERR_MSG("stat");
return -1;
}
if(S_ISDIR(buf.st_mode))
return 1;
else
return 2;
}
int main(int argc, const char *argv[])
{
printf("请输入目录或文件名:\n");
//char str[256]="HQYJ/IO/day4/dir.c";
char str[256];
scanf("%s",str);
struct stat buf;
int n = panduan(str,buf);
if(n==1)
{
DIR *dp = opendir(str);
char str1[256];
if(NULL == dp)
{
// ERR_MSG("opendir");
return -1;
}
printf("opendir success\n");
while(1)
{
struct dirent *rp = readdir(dp);
if(NULL == rp)
{
if(0 == errno)
{
break;
}
else
{
// ERR_MSG("readdir");
return -1;
}
}
if(rp->d_name[0]!='.')
{
char *p = strcpy(str1,str);
p[strlen(str)]='/';
p[strlen(str)+1]='\0';
output(strcat(p,rp->d_name),rp->d_name);
// printf("%s\n",rp->d_name);
}
}
}
else
output(str,str);
return 0;
}
结果为
文件IO函数实现,拷贝文件。子进程先拷贝后半部分,父进程再拷贝前半部分。允许使用sleep函数。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
FILE *p=fopen("2.c","r");
if(NULL == p)
{
perror("fopen");
return -1;
} FILE *p1=fopen("zuoye2_2.txt","w+");
if(NULL == p1)
{
perror("fopen");
return -1;
}
int count=0;
while(1)
{
if(fgetc(p)==EOF)
break;
count++;
}
int d=count/2;
char str[d];
char str1[count-d];
pid_t cpid = fork();
if(cpid==0)
{
fseek(p,0,SEEK_SET);
fread(str,d,1,p);
fwrite(str,d,1,p1);
}
else
{
sleep(1);
fseek(p,d,SEEK_SET);
fread(str1,count-d,1,p);
fwrite(str1,count-d,1,p1);
}
fclose(p);
fclose(p1);
while(1)
{
sleep(1);
}
return 0;
}
结果为