IO-DAY(4)-(父进程、子进程.........)

文章包含两个部分:一是编写C程序来模拟ls-l命令,显示指定路径下所有文件的详细信息,包括权限、类型、所有者、组、大小和修改时间;二是实现图片拷贝,父进程拷贝图片前半部分,子进程拷贝后半部分,以此完成全图的复制。
摘要由CSDN通过智能技术生成

作业一、

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

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <error.h>

void my_file(struct stat buf);
int my_dp(char *p, struct stat buf);
char *get_filePermssion(mode_t m, char ptr[10]);
void Aget_filePermssion(mode_t m);
char get_filetype(mode_t M);
int File_owner(mode_t buf);
int File_group(mode_t buf);
void file_time(time_t t, struct stat buf);

int main(int argc, const char *argv[])
{
    struct stat buf;
    char s[50] = "";
    strcpy(s, argv[1]);
    my_dp(s, buf);

    return 0;
}

char *get_filePermssion(mode_t m, char ptr[10]) // 注意这里ptr[10]只是为了提醒用户需要传入一个容量为10的组
{
    char per[] = "rwx";
    for (int i = 0; i < 9; i++)
    {
        if ((m & (0400 >> i)) == 0)
        {
            ptr[i] = '-';
            continue;
        }

        ptr[i] = per[i % 3];
    }

    return ptr;
}

char get_filetype(mode_t M)
{
    if (S_ISREG(M))
        return '-';
    else if (S_ISDIR(M))
        return 'd';
    else if (S_ISCHR(M))
        return 'c';
    else if (S_ISBLK(M))
        return 'b';
    else if (S_ISFIFO(M))
        return 'P';
    else if (S_ISLNK(M))
        return 'l';
    else if (S_ISSOCK(M))
        return 's';
}

int File_owner(mode_t buf)
{
    struct passwd *pwd = getpwuid(buf);
    if (NULL == pwd)
    {
        perror("getpwuid");
        return -1;
    }
    printf("%s ", pwd->pw_name);

    return 0;
}

int File_group(mode_t buf)
{
    struct group *grp = getgrgid(buf);
    if (NULL == grp)
    {
        perror("getgrgrd");
        return -1;
    }
    printf("%s ", grp->gr_name);

    return 0;
}

void file_time(time_t t, struct stat buf)
{
    time(&t);
    struct tm *info = NULL;
    info = localtime(&buf.st_ctime);
    printf("%5d月 %02d %02d:%02d ",
           info->tm_mon + 1, info->tm_mday,
           info->tm_hour, info->tm_min);
}

void my_file(struct stat buf)
{
    char str[10] = "";
    char *s = get_filePermssion(buf.st_mode, str);
    printf("%s", str);

    // 获取文件类型
    char filetype = get_filetype(buf.st_mode);
    printf("%c", filetype);

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

    // 文件所属用户
    File_owner(buf.st_uid);

    // 文件所属用户组
    File_group(buf.st_gid);

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

    // 文件修改的时间
    time_t t;
    file_time(t, buf);
}

int my_dp(char *p, struct stat buf)
{
    //char name[50] = "";
    //strcpy(name, p);

    DIR *dp = opendir(p);
    if (NULL == dp)
    {
        perror("dp");
        return -1;
    }

    int count = 1;
    struct dirent *readptr = NULL;
    while (1)
    {
        readptr = readdir(dp);

        if (NULL == readptr)
        {
            if (0 == error)
            {
                printf("目录读取完毕\n");
                break;
            }
            else
            {
                perror("readdir");
                return -1;
            }
        }

        if (readptr->d_name[0] != '.')
        {
            char name[50] = "";
            strcpy(name, p);
            strcat(name, readptr->d_name);
            //printf("name=%s\n", name);

            if (stat(name, &buf) < 0)
            {
                perror("stat");
                return -1;
            }
            my_file(buf);
            printf("%s\n", readptr->d_name);
        }
    }

    if (closedir(dp) < 0)
    {
        perror("closedir");
        return -1;
    }
}

作业二、

2.拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。

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

int main(int argc, const char *argv[])
{
    int dp_r = open("1.jpg", O_RDONLY);
    int dp_w = open("2.jpg", O_RDWR | O_CREAT | O_TRUNC, 0777);
    if (dp_r < 0)
    {
        perror("open");
        printf("line:%d\n", __LINE__);
        return -1;
    }
    if (dp_w < 0)
    {
        perror("open");
        printf("line:%d\n", __LINE__);
        return -1;
    }

    // 计算1.jpg文件的大小
    off_t res = lseek(dp_r, 0, SEEK_END);
    res+=1;
    int hulf = res / 2;
    //printf("%d",res);

    // 创建父子进程
    pid_t cpid = fork();
    char c;

    if (cpid > 0 )
    {
        lseek(dp_r,0,SEEK_SET);
        for (int i = 0; i < hulf; i++)
        {
            if (read(dp_r, &c, sizeof(c)) != 0)
            {
                write(dp_w, &c, sizeof(c));
            }
            else
            {
                return -1;
            }
        }
    }
    else if (0 == cpid)
    {
        sleep(5);
        //lseek(dp_r,hulf+1,SEEK_SET);
        for (int i = 0; i < hulf-1; i++)
        {
            if (read(dp_r, &c, sizeof(c)) != 0)
            {
                write(dp_w, &c, sizeof(c));
            }
            else
            {
                return -1;
            }
        }
    }

    if (close(dp_w) < 0)
    {
        perror("close");
        return -1;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值