C 打印文件属性 和 父子进程拷贝文件

C语言 实现 ls -al

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>


void get_file_format_time(struct tm* t, char s[16]) {
    sprintf(s, "%d-%d %d:%d ", t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min);
}

void get_file_type(struct stat* st, char s[1]) {
    switch(st->st_mode & __S_IFMT) {
        case __S_IFREG:
            sprintf(s, "%s", "-");
            break;
        case __S_IFDIR:
            sprintf(s, "%s", "d");
            break;
        case __S_IFLNK:
            sprintf(s, "%s", "l");
            break;
        case __S_IFBLK:
            sprintf(s, "%s", "b");
            break;
        case __S_IFCHR:
            sprintf(s, "%s", "c");
            break;
        case __S_IFIFO:
            sprintf(s, "%s", "p");
            break;
        case __S_IFSOCK:
            sprintf(s, "%s", "s");
            break;
    }
}

void get_file_hardlink_no(struct stat* st, char s[16]) {
    sprintf(s, "%ld", st->st_nlink);
}

void get_file_owner(struct stat* st, char s[16]) {
    sprintf(s, "%s %s", getpwuid(st->st_uid)->pw_name, getgrgid(st->st_gid)->gr_name);
}

void get_file_size(struct stat* st, char s[16]) {
    sprintf(s, "%ld", st->st_size);
}

void get_file_permission(struct stat* st, char s[128]) {
    int i = 8;
    int m = st->st_mode & 0777;
    while (i >= 0) {
        
        if ((m & (1 << i)) != 0) {
            if (i%3 == 2) {
                sprintf(s, "%s%c", s, 'r');
            } else if (i%3 == 1) {
                sprintf(s, "%s%c", s, 'w');
            } else {
                sprintf(s, "%s%c", s, 'x');
            }
        } else {
            sprintf(s, "%s%c", s, '-');
        }
        i --;
    }
}

struct passwd wd;

int main(int argc, const char *argv[]) {
    DIR *dir;
    struct dirent *dt;
    if ((dir = opendir("./")) == NULL) {
        perror("open ./ error:");
        return -1;
    }

    while ((dt = readdir(dir)) != NULL) {
        struct stat st;
        if (stat(dt->d_name, &st)) {
            perror("get file stat error");
            return -1;
        }
        char descriptor[512] = {0};
        char file_size[16] = {0};
        char file_type[16] = {0};
        char permission[16] = {0};
        char hard_link_no[16] = {0};
        char file_owner[16] = {0};
        char file_time[16] = {0};

        get_file_type(&st, file_type);
        get_file_size(&st, file_size);
        get_file_permission(&st, permission);
        get_file_hardlink_no(&st, hard_link_no);
        get_file_owner(&st, file_owner);
        get_file_format_time(localtime(&st.st_atime), file_time);
        sprintf(descriptor, "%s%s %s %s %5ld %-12s %s", file_type, permission, hard_link_no, file_owner, st.st_size, file_time, dt->d_name);
        printf("%s\n", descriptor); 
    }
    closedir(dir);                                   
    return 0;
}

父进程 和 子进程 各自拷贝文件一半(顺序拷贝, 共用一个文件描述符)

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

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

void copy(int src_fd, int des_fd, int size) {
    char s[128] = {0};
    int res = 0;
    int already_copy = 0;
    while ((res = read(src_fd, s, sizeof(s))) > 0) {
        already_copy += res;
        if (already_copy > size) {
            int red = already_copy - size;
            write(des_fd, s, res - red);
        } else {
            write(des_fd, s, res);
        }
    }
}

int main(int argc, const char *argv[]) {
    int src_fd = open("time.png", O_RDONLY);
    int des_fd = open("time1.png", O_WRONLY|O_CREAT|O_TRUNC, 0666);
    if (src_fd == -1 || des_fd == -1) {
        PRINT_ERR("open file error");
    }
    int size = lseek(src_fd, 0, SEEK_END);
    int child_size = size/2;
    int parent_size = size - size/2;

    int pid = fork();
    if (pid == 0) {
        lseek(src_fd, size - size/2 ,SEEK_SET);
        lseek(des_fd, size - size/2 ,SEEK_SET);
        copy(src_fd, des_fd, child_size);
    } else {
        sleep(1);
        lseek(src_fd, 0 ,SEEK_SET);
        lseek(des_fd, 0 ,SEEK_SET);
        copy(src_fd, des_fd, parent_size);
    }
    close(src_fd);
    close(des_fd);
    return 0;
}

父进程 和 子进程 各自拷贝文件一半(并发拷贝 , 使用各自文件描述符)

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

#define PRINT_ERR(msg)  \
        do {                            \
                printf("%s:%s:%d\n", __FILE__, __func__, __LINE__); \
                perror(msg);    \
                exit(-1);               \
        } while (0)

void copy(int src_fd, int des_fd, int start, int size) {
    char s[128] = {0};
    int res = 0;
    int already_copy = 0;

    lseek(src_fd, start, SEEK_SET);
    lseek(des_fd, start, SEEK_SET);

    while ((res = read(src_fd, s, sizeof(s))) > 0) {
        already_copy += res;
        if (already_copy > size) {
            int red = already_copy - size;
            write(des_fd, s, res - red);
        } else {
            write(des_fd, s, res);
        }
    }
}

int main(int argc, const char *argv[]) {
    int src_fd = open("time.png", O_RDONLY);
    int des_fd = open("time1.png", O_WRONLY|O_CREAT|O_TRUNC, 0666);
    if (src_fd == -1 || des_fd == -1) {
        PRINT_ERR("open file error");
    }
    int size = lseek(src_fd, 0, SEEK_END);
    int child_size = size/2;
    int parent_size = size - size/2;
    close(src_fd);
    close(des_fd);

    int pid = fork();
    if (pid == 0) {
        int src_fd = open("time.png", O_RDONLY);
        int des_fd = open("time1.png", O_WRONLY);
        copy(src_fd, des_fd, size - size/2, child_size);
        close(src_fd);
        close(des_fd);
    } else {
        int src_fd = open("time.png", O_RDONLY);
        int des_fd = open("time1.png", O_WRONLY);
        copy(src_fd, des_fd, 0, parent_size);
        close(src_fd);
        close(des_fd);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值