IOday4

1.目录文件

输入任意路径,将该路径下所有文件的详细信息显示出来,类似ls -l

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
void type_file(struct stat st){
    
    if( 0100000 == (st.st_mode&__S_IFMT)){
        printf("%c",'-');
    }else if(0140000 == (st.st_mode&__S_IFMT)){
        printf("%c",'s');
    }else if(0120000 == (st.st_mode&__S_IFMT)){
        printf("%c",'l');
    }else if(0060000 == (st.st_mode&__S_IFMT)){
        printf("%c",'b');
    }else if(0040000 == (st.st_mode&__S_IFMT)){
        printf("%c",'d');
    }else if(0020000 == (st.st_mode&__S_IFMT)){
        printf("%c",'c');
    }else if(0010000 == (st.st_mode&__S_IFMT)){
        printf("p");
    }
}
//文件的形式(目录...)
void Typede_file(mode_t m){
    if(S_ISREG(m)){
        printf("%c",'-');
    }else if(S_ISDIR(m)){
        printf("%c",'d');
    }else if(S_ISCHR(m)){
        printf("%c",'c');
    }else if(S_ISBLK(m)){
        printf("%c",'b');
    }else if(S_ISFIFO(m)){
        printf("%c",'p');
    }else if(S_ISLNK(m)){
        printf("%c",'l');
    }else{
        printf("%c",'s');
    }
}
//文件权限的输出
void get_filePermissing1(mode_t m){ 
    int a = 256;
    for(int i = 1; i < 10;i++){
        if(0 == (m & a)){
            printf("%c",'-');
            a = a / 2;
            continue;
        }

        if(1 == i%3) {
            if(m & a){
                printf("%c",'r');
            }
        }
        else if(2 == i%3) {
            if(m & a){
                printf("%c",'w');
            }
        }
        else if(0 == i%3) {
            if(m & a){
                printf("%c",'x');
            }
        }
    a = a/2;
    }
}
//用户名
void getuid_name(struct stat st){
    printf(" %s",getpwuid(st.st_uid)->pw_name);
}
//组用户名字
void getgid_name(struct stat st){
    printf(" %s",getgrgid(st.st_gid)->gr_name);
}
int main(int argc,const char * argv[])
{
    if(argc < 2){
        printf("输入的argv少于两条请重新输入\n");
        return -1;
    }
    //目录文件创建
    DIR * dr ;
    struct dirent *dt;
    if(NULL == (dr = opendir(argv[1]))){
        perror("opendir");
        return -1;
    }
    //普通文件创建
    
    struct tm *info = NULL;

    char s[30] = "";
    
    struct stat st;
    int count = 0;
    while(NULL != (dt = readdir(dr))){
        //printf("%s",dt->d_name);
        memset(s,0,sizeof(s));
        strcpy(s,argv[1]);
        if( '.' == dt->d_name[0]){
            continue;
        }
        if(-1 == stat(strcat(s,dt->d_name),&st)){
        perror("stat");
        return -1;
        }
        //printf("name = %s\t inode = %ld\t szie = %d\ttype =%c\n",dt->d_name,dt->d_ino,dt->d_reclen,dt->d_type);
        type_file(st);
        get_filePermissing1(st.st_mode);//文件的权限
        printf(" %ld",st.st_nlink);
        getuid_name(st);
        getgid_name(st);

        printf("%6ld",st.st_size);
        time_t t = st.st_mtime;
        info = localtime(&t);

         printf ("%2d月  %02d %02d:%02d",info ->tm_mon+1, info ->tm_mday, \
        info ->tm_hour, info -> tm_min);//时间
        printf(" %s\n",dt->d_name);
        count++;
            
    }
    closedir(dr);
    return 0;
}

输出的信息是

 ls -l 和写的对比

 

 

 2.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc,const char * argv[])
{
    int offset = 5685030;
    int fd,fd1;
    //打开文件
    if( -1 == (fd = open("/home/ubuntu/xwf/IOday3/labixiaoxin.bmp",O_RDWR))){
        perror("open");
        printf("%d\n",__LINE__);
        return -1;
    }
    //打开文件1
    if( -1 == (fd1 = open("/home/ubuntu/xwf/IOday3/labixiaoxincopy1.bmp",O_WRONLY|O_CREAT|O_TRUNC,0666))){
        perror("open");
        printf("%d\n",__LINE__);
        return -1;
    }

    //创建子进程
    pid_t chid = fork();
    char buf = '0';
    ssize_t res = 0;
    //如果是子进程就偏移到中间,父进程不变5685030这是我文件大小
    if(0 == chid){//子进程偏移
        lseek(fd,offset/2,SEEK_SET);
        lseek(fd1,offset/2,SEEK_SET);
        //
        while (0 != (res = (read(fd,&buf,sizeof(buf)))) )
        {
            
            write(fd1,&buf,res);
            memset(&buf,0,sizeof(buf));
        }
    }else if(chid > 0){//父进程执行
        sleep(20);
        lseek(fd,0,SEEK_SET);
        lseek(fd1,0,SEEK_SET);
        while(lseek(fd,0,SEEK_CUR) != offset/2){
            memset(&buf,0,sizeof(buf));
            res = (read(fd,&buf,sizeof(buf)));
            write(fd1,&buf,res);
        }
    }

    
    
    close(fd);
    close(fd1);
    
    return 0;
}

运行出来的

 原来

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值