第4天

第一题

从终端获取文件路径及名字

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


void getfilestat(struct stat buf)
{
	//文件的类型和权限
	printf("mode:0%o\n",buf.st_mode);

    switch(buf.st_mode & S_IFMT)
	{
	case S_IFSOCK: putchar('s');  break;
	case S_IFLNK: putchar('l');  break;
	case S_IFREG: putchar('-');  break;
	case S_IFDIR: putchar('d');  break;
	case S_IFCHR: putchar('s');  break;
	case S_IFIFO: putchar('p');  break;
	case S_IFBLK: putchar('b');  break;
	}
    
//	char buf[]="rwx";
	for(int i=0;i<9;i++)
	{
		if((buf.st_mode & (0400>>i)) == 0)
		{
			putchar('-');
			continue;
		}
		switch(i%3)
		{
		case 0:putchar('r');break;
		case 1:putchar('w');break;
		case 2:putchar('x');break;
		}
		//printf("  %c\n",buf[i%3]);
	}


	//文件的硬链接数
	printf("   %ld",buf.st_nlink);

	//文件的所属用户
	//printf("   %d",buf.st_uid);
    //将uid转换成名字
	struct passwd* pwd  =  getpwuid(buf.st_uid);
	if(NULL == pwd)
	{
		ERR_MSG("getpwuid");
		return ;
	}
	printf("   %s",pwd->pw_name);


	//文件所属组用户
    //printf("  %d",buf.st_gid);
	//将gid转换成名字
	struct group* grp = getgrgid(buf.st_gid);
	if(NULL == grp)
	{
		ERR_MSG("getgrgid");
		return ;
	}
	printf("   %s",grp->gr_name);

	//文件大小
	printf("  %ld",buf.st_size);

	//文件的修改时间
	struct tm* info=localtime(&buf.st_ctime);
    printf("   %2d月  %02d  %02d:%02d:%02d",\
				info->tm_mon+1,info->tm_mday,\
				info->tm_hour,info->tm_min,info->tm_sec);
	
	return;

}


int main(int argc, const char *argv[])
{
	char a[20]="";
	printf("请输入文件的路径以及名字:");
	fscanf(stdin,"%s",a);
    
	struct stat buf;
	if(stat(a,&buf)<0)
	{
		ERR_MSG("stat");
		return -1;
	}
    
	if((buf.st_mode & S_IFMT) == S_IFDIR)
	{
         DIR * dp = opendir(a);
		 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)
				 {
					// printf("目录读取完毕\n");
					 break;
				 }
				 else
				 {
					 ERR_MSG("readdir");
					 return -1;
				 }
			 }
			 printf("  %s\n",rp->d_name);
			 struct stat c;
			 if(stat(rp->d_name,&c)<0)
			 {
                  ERR_MSG("stat");
				  return -1;
			 }
			 getfilestat(c);
		 }
		 closedir(dp);

	}
	else
		getfilestat(buf);

	return 0;
}

用文件IO实现拷贝

#include
#include
#include
#include
#include
#include

int main(int argc, const char *argv[])
{
	int fd_r = open("./1.c",O_RDONLY);
	if(fd_r <0)
	{
        ERR_MSG("open");
		return -1;
	}

	int fd_w = open("./4.c",O_WRONLY|O_CREAT|O_TRUNC,0664);
	if(fd_w < 0)
	{
		ERR_MSG("open");
		return -1;
	}
	off_t size = lseek(fd_r,0,SEEK_END);
	char a;
	ssize_t res = 0;
	pid_t cpid = fork();
	
	if(0 == cpid)
	{
        lseek(fd_r,size/2,SEEK_SET);
		while(1)
		{
			bzero(&a,sizeof(char));
			res = read(fd_r,&a,sizeof(char));
			if( 0 == res)
				break;
			if(write(fd_w,&a,res)<0)
			{
				ERR_MSG("write");
				return -1;
			}
		}

	}
	else if(cpid > 0)
	{
		lseek(fd_r,0,SEEK_SET);
		lseek(fd_w,0,SEEK_SET); 
		while(1)
		{
			bzero(&a,sizeof(char));
			res = read(fd_r,&a,sizeof(char));
			if( 0 == res)
				break;
			if(write(fd_w,&a,res/2)<0)
			{
				ERR_MSG("write");
				return -1;
			}

			
		}
	}

	close(fd_r);
	close(fd_w);




	return 0;
}

 

你可能感兴趣的:(算法,linux)