作业-11.10

1、要求输入目录的路径后,能够打印出指定路径下所有文件的详细信息,类似ls -l

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

int printf_file_mes(const char file[], int num);
void get_file_permission(unsigned int tmp_mod, char file_mod[], int num);
char get_file_type(unsigned int tmp_filetype);

int main(int argc, const char *argv[])
{
    if(argc < 2)
    {
        fprintf(stderr, "请在命令行输入文件名\n");
        return -1;
    } //打开目录
    DIR *dp = opendir(argv[1]);
    if(NULL == dp)
    {
        perror("opendir");
        return -1;
    }
    int i = 0;
    struct dirent *rp = NULL;
    char filepathname[256]="0";
    while(1)
    {
        rp = readdir(dp);
        if(NULL == rp)
        {
            if(0 == errno)
            {
                printf("目录读取完毕\n");
                break;
            } else
            {
                perror("readdir");
                return -1;
            }
        }
        bzero(filepathname, sizeof(filepathname));
        strcat(filepathname, argv[1]);
        strcat(filepathname, rp->d_name);
        int num = strlen(argv[1]);
        printf_file_mes(filepathname, num);
    }
    
    //关闭目录
    if(closedir(dp) < 0)
    {
        perror("closedir");
        return -1;
    }
    return 0;
}
void get_file_permission(unsigned int tmp_mod, char file_mod[], int num)
{
    bzero(file_mod,num);
    unsigned int flag = 1<<9;
    char str[4]="rwx";
    for(int i=0; i<9; i++)
    {
        flag >>= 1;
        if(!(tmp_mod & flag))
        {
            file_mod[i] = '-';
        } else
        {
            file_mod[i] = str[i%3];
        }
    }
}
char get_file_type(unsigned int tmp_filetype)
{
    char file_type[8] = "pcdb-ls";
    unsigned int num_filetype[7] = {010,020,040,060,0100,0120,0140};
    for(int i=0; i<8; i++)
    {
        if(0 == (tmp_filetype ^ num_filetype[i]))
        {
            return file_type[i];
        }
    }
}
int printf_file_mes(const char file[], int num)
{
    struct stat buf;
    if(lstat(file, &buf) < 0)
    {
        perror("stata");
        return -1;
    }

    //文件的类型和权限
    char file_mod[10]; //将buf.st_mode中的文件权限提取出来
    unsigned int tmp_mod = buf.st_mode & ~((~1)<<10);
    unsigned int tmp_filetype = (buf.st_mode & S_IFMT) >> 9;
    char c = get_file_type(tmp_filetype);
    printf("%c", c);

    //将文件权限由数字转化为字母
    get_file_permission(tmp_mod, file_mod, 10);

    //打印转换后的文件权限
    printf("%s ", file_mod);

    //硬连接数
    printf("%4ld ", buf.st_nlink);

    //文件所属用户
    //printf("%d ", buf.st_uid);
    struct passwd *tmp_usname = getpwuid(buf.st_uid);
    printf("%s ", tmp_usname->pw_name);

    //文件所属组名
    struct group *tmp_groupname = getgrgid(buf.st_gid);
    printf("%s ", tmp_groupname->gr_name);

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

    //时间
    time_t t = buf.st_ctime;
    struct tm *tmp_time = localtime(&t);
    printf("%02d %02d %02d:%02d ", tmp_time->tm_mon+1, tmp_time->tm_mday, \

                tmp_time->tm_hour\ , tmp_time->tm_min);


    printf("%s\n", file+num);


    return 0;
}

(1)使用相对路径

(2)使用绝对路径

2、用父子进程拷贝一张图片,用文件IO实现,要求 子进程拷贝后半部分,

父进程拷贝前半部分,按照cpu调度机制同时执行

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
    if(argc < 3)
    {
        fprintf(stderr, "请从命令行输入图片名和复制后的文件名\n");
        return -1;
    }
    if(!strcmp(argv[1], argv[2]))
    {
        fprintf(stderr, "复制后的文件名和原文件名不能相同!!!\n");
        return -1;
    }

    //打开文件
    int fd = open(argv[1], O_RDONLY);
    int fd_w = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0777);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }
    if(fd_w < 0)
    {
        perror("open");
        return -1;
    }
    ssize_t res = 0;
    ssize_t res_w = 0;
    char arr[128] = "0";
    int len = sizeof(arr);
    int count = 0;
    count = (lseek(fd, 0, SEEK_END)/len);
    count++;
    printf("count = %d.\n", count);

    //创建子进程
    pid_t pid = fork();
    if(pid < 0)
    {
        perror("fork");
        return -1;
    }

    //父进程复制前半部分
    if(!pid)
    {
        sleep(2);
        printf("子进程\n");

        //将指针偏移到子进程写入数据的末尾
        lseek(fd, (count/2)*len, SEEK_SET);
        lseek(fd_w, (count/2)*len, SEEK_SET);
        while(1)
        {
            bzero(arr, sizeof(arr));
            res = read(fd, arr , len);
            if(!res)
            {
                break;
            } else if(res < 0)
            {
                perror("read");
                return -1;
            }
            res_w = write(fd_w, arr, res);
            if(res_w < 0)
            {
                perror("write");
                return -1;
            }
        }

        //关闭父进程中的文件读取和写入
        close(fd);
        close(fd_w);
        putchar(10);
    } else {


        //子进程复制后半部分
        printf("父进程\n");
        int tmp = count;

        //将指针偏移到文件开头
        lseek(fd , 0, SEEK_SET);
        lseek(fd_w, 0, SEEK_SET);
        while(1)
        {
            bzero(arr, sizeof(arr));
            res = read(fd, arr , len);
            if(res < 0)
            {
                perror("read");
                return -1;
            }

            //写入数据
            res_w = write(fd_w, arr, res);
            if(res_w < 0)
            {
                perror("write");
                return -1;
            }

            //所需要的拷贝次数--
            count -= 1;

            //如果拷贝的数据比总数据的一半就退出循环
            if(count <= (tmp/2))
            {
                break;
            }
        }

        //关闭子进程中的文件读取和写入
        close(fd);
        close(fd_w);
    }


    return 0;
}

(1)复制前文件列表

(2)复制后文件列表

(3)复制的图片打开

3、创建一个孤儿进程,创建一个僵尸进程

(1)孤儿进程

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    int jc = fork();
    if(!jc)
    {
        while(1)
        {
            printf("child:pid = %d,ppid=%d\n",getpid(),getppid());
            sleep(5);
        }
    }

    sleep(5);
    return 0;
}

(2)僵尸进程

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    int jc = fork();
    if(!jc)
    {
        exit(0);
    } else
    {
        while(1);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值