linux : my_ls实现

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <time.h>
//最大文件名长度512,文件数量1024
#define MAXNAME 512
#define MAXFILE 1024
//定义输出每一列最大元素后空白格长度
#define blank 1
#include <math.h> 

int a_flag = 0;
int l_flag = 0;
int dir_num = 0;
int col_max[MAXFILE] = {0};

void size_and_show(char filename[][MAXNAME], int num, int * row, int *col);
void show(char filename[][MAXNAME], int num, int *row, int *col);
void do_ls(const char *);
void show_info(const char *filename, struct stat *st);
void do_stat(const char *);
void mode_to_str(char *modestr, mode_t mode);

void do_ls(const char *name){
    DIR *dir = NULL;
    struct dirent *direntp;
    char names[MAXFILE][MAXNAME] = {0};
    int cn = 0;
    if ((dir = opendir(name)) == NULL){
        //文件
        if (access(name, R_OK) == 0) {
            //添加do_stat部分
            //可读
            printf("%s ", name);
        }
    }else{
        //读出目录中文件,并记录文件名,记录数量:
        while((direntp = readdir(dir))){
            //printf("%s ", direntp->d_name);
            if ((a_flag == 0) && direntp->d_name[0] == '.'){
                continue;//隐藏文件不输出
            }
            strcpy(names[cn++], direntp->d_name);
        }
        //
        if (l_flag == 0){
            int row, col;
            size_and_show(names, cn, &row, &col);
            //printf("row = %d col = %d\n num = %d", row, col, cn);
            //printf("%d %d %d", col_max[0], col_max[1], col_max[2]);
            show(names, cn, &row, &col);
        }else{
            for (int i = 0; i < cn; i++){
                do_stat(names[i]);
            }
        }
        printf("\n");
    }
    return;
}

void show(char filename[][MAXNAME], int num, int *row, int *col){
    /*
    for (int i = 0; i < *col; i++){
        for (int j = 0; (j < *row) && ((i * *row + j) < num); j++){
            printf("j = %d\n", j);
            printf("%s", filename[i * *row + j]);
            /*
            for (int n = 0; n < (col_max[i] - strlen(filename[i * *row + j])); n++){
                printf(" ");
            }
            
            printf("\n");
        }
    }
    */
    //按行输出
    for (int i = 0; i < *row; i++){
        for (int j = i;j < i + (*row * *col) && j < num; j = j + *row){
            int tmp = j / *row;
            printf("%-*s", col_max[tmp], filename[j]);
        }
        printf("\n");
    }

}




//传出文件指针,文件数,文件输出行列?
void size_and_show(char filename[][MAXNAME], int num, int *row, int *col){
    //文件描述符
    struct winsize size;
    int len[num], max = 0, total = 0;
    memset(len, 0, sizeof(int) * num);
    //标准输出的终端号,打开模式,文件描述符
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0){
        perror("iostl");
        exit(1);
    }
    
    //记录len[i](每个文件名长度), 并统计最长字段、字段总长
    for (int i = 0; i < num; i++){
        len[i] = strlen(filename[i]);
        if (max < len[i]) max = len[i];
        total += len[i] + blank;
    }
    

    //最大文件名长度大于窗口列长
    if (max + 1 >= size.ws_col){
        *row = num;
        *col = 1;
        return;
    }

    //文件总长度小于窗口总列长
    if (total <= size.ws_col){
        *row = 1;
        *col = num;
        return;
    }

    //先找出一个可能满足要求的列数
    int try_begin = 0;
    for (int i = 0, len_temp = 0; i < num; i++){
        len_temp += len[i] + blank;
        //len_temp为暂时的一行字符数之和
        if (len_temp >= size.ws_col){
            try_begin = i;
            break;
        }
    }
    
    //按这个列数排列,看是否有行超出窗口长度
    for (int i = try_begin - 1; ;i--){
        //行文件数组【1, i】,记录每行文件名
        int try_sum = 0;//每行最值之和
        int *wide = (int *)malloc(sizeof(int) * i);
        *row = (int)ceil(num / i);//数组个数/列数 = 每列文件个数(行数)
        //对每列
        for (int x = 0; x < i; x++){
            //每列中每个元素
            for (int y = x * *row; y < (x + 1) * *row && y < num; y++){
                //每列元素最值
                if (wide[x] < len[y]) wide[x] = len[y];
            }
            try_sum += wide[x] + blank;
            //printf("wide = %d\n", wide[x] + blank);
        }

        if (try_sum > size.ws_col){
            continue;
        }else{
            *col = i;
            for (int k = 0; k < i; k++){
                col_max[k] = wide[k] + blank;
                //printf("cm = %d\n", col_max[k]);
            }
            break;
        }
    }
}


void do_stat(const char *filename){
    struct stat st;
    if (lstat(filename, &st) < 0){
        perror("do_stat");
        exit(1);
    }
    show_info(filename, &st);
    return ;
}

void mode_to_str(char *modestr, mode_t mode){
    if (S_ISREG(mode)) modestr[0] = '-';
    if (S_ISDIR(mode)) modestr[0] = 'd';
    if (S_ISCHR(mode)) modestr[0] = 'c';
    if (S_ISBLK(mode)) modestr[0] = 'b';
    if (S_ISSOCK(mode)) modestr[0] = 's';
    if (S_ISFIFO(mode)) modestr[0] = 'p';
    if (S_ISLNK(mode)) modestr[0] = 'l';

    if (mode & S_IRUSR) modestr[1] = 'r';
    if (mode & S_IWUSR) modestr[2] = 'w';
    if (mode & S_IXUSR) modestr[3] = 'x';
    if (mode & S_IRGRP) modestr[4] = 'r';
    if (mode & S_IWGRP) modestr[5] = 'w';
    if (mode & S_IXGRP) modestr[6] = 'x';
    if (mode & S_IROTH) modestr[7] = 'r';
    if (mode & S_IWOTH) modestr[8] = 'w';
    if (mode & S_IXOTH) modestr[9] = 'x';

}


void show_info(const char *filename, struct stat *st){
    char modestr[15] = "----------";
    mode_to_str(modestr, st->st_mode);
    printf("%s ", modestr);
    printf("%5d ", st->st_nlink);
    printf("%7d ", st->st_uid);
    printf("%7d ", st->st_gid);
    printf("%9d ", st->st_size);
    printf("%.15s ", 4 + ctime(&st->st_mtime));
    printf("%s\n", filename);
}


int main(int argc, char *argv[]){
    char ch;
    while((ch = getopt(argc, argv, "al")) != -1){
        switch (ch){
            case 'a': a_flag = 1; break;
            case 'l': l_flag = 1; break;
            default:
            fprintf(stderr, "Usage:%s [-a|-l] \n", argv[0]);
            exit(1);
        }
    }
    argc -= (optind - 1);//可能是把2个参数删掉
    argv += (optind - 1);
    dir_num = argc;

    if (argc == 1){
        do_ls(".");
    }else{
        for (int i = 1; i < argc; i++){
            do_ls(argv[i]);
        }
    }
    

    
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
myls命令包含9个选项: (1) myls : 在缺省选项的情况下,列出当前文件夹下的普通文件(不包含隐藏文件)。 例如:当前目录包含文件home1.c, home2.c, .home3.c,输入myls后,列出的文件名为home1.c, home2.c. (2) myls –a: 列出当前文件夹下的所有文件(包含隐藏文件)。 例如:当前目录包含文件home1.c、home2.c、.home3.c,输入myls -a后,列出所有的文件名为home1.c, home2.c, .home3.c. (3) myls –l: 列出当前文件夹下普通文件的详细信息,包括文件模式,文件链接数,文件所属用户,文件所属用户组,文件大小,文件最后修改时间,文件名。并且在最后一行显示该目录下所显示的文件的文件块数。 例如:显示drwxr-xr-x 3 eli eli 4096 Nov 16 23:07 Desktop (4) myls -R 递归列出文件及其子文件。 例如:当前目录为home, 其中包含文件home1, home2, home3. 其中home1是目录文件,包含文件home11, home12, home2不是目录文件,home3是目录文件,包含文件home31, home32。 输入myls –R后,列出的文件名为 ./home: home1 home2 home3 ./home/home1: home11 home12 ./home/home3: home31 home32. (5) myls –u: 列出当前文件夹下用户x的普通文件,若输入myls -u bb,则显示所属bb的普通文件。 例如:文件home1, home2, home3属于aa,文件tmp1, tmp2, tmp3属于bb, 则若输入myls –u aa, 则显示home1,home2,home3,若输入myls -u bb, 则显示tmp1,tmp2,tmp3。 (6) myls –S: 对文件进行排序,需要输入比较参数。 myls –S 的参数包括: time——按最近修改时间排序 name——按文件名的字典序排序 size——按文件的大小从小到大排序 link——按文件链接数从少到多排序 (7) myls -1: 将当前文件夹下的文件按照一行一个的方式显示。 (8) myls –s: 在各个文件开头显示这个文件的文件块大小。 (9) myls /dirname: 显示/dirname下的文件。 编译 gcc main.c -o myls 执行 ./myls 可加若干参数,具体见上描述 程序并不完整,可能会有BUG,希望广大网友指点,交流~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值