主要用到的函数可以参考头文件,仅仅支持ls -l这功能,扩展就交给大家了0.0
相关测试图片:
编译 gcc -std=c99 ls_l.c -o ls
运行 ./ls -l
( 请勿在文件结构复杂的目录下执行,程序会挂的!)
话不多说,直接上码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/stat.h> 5 #include <sys/types.h> 6 #include <string.h> 7 #include <time.h> 8 #include <pwd.h> 9 #include <grp.h> 10 #include <time.h> 11 #include <dirent.h> 12 13 #define MAX_FILE_NUM 200 14 15 16 //可能还有一些小问题没有解决,功能基本已经实现,如有建议,望大佬赐教 17 18 typedef struct LS 19 { 20 char mode[15]; // 文件的模式 21 int dir_num; // 是否目录或目录中包含目录的数量 22 char user[20]; // 文件的用户名 23 char group[20]; // 文件的组名 24 long size; // 文件的字节数 25 char time[30]; // 文件的最后修改时间 26 char year[5]; // 拓展用,年份 27 char mon[5]; // 月份 28 char hour[5]; // 时 29 char min[5]; // 分 30 int st_mode; // 文件类型和权限 31 char name[20]; // 文件名 32 }LS; 33 34 // 获取文件的模式 35 char* file_mode(mode_t m,char* str) 36 { 37 if(S_ISREG(m)) 38 str[0] = '-'; 39 else if(S_ISDIR(m)) 40 str[0] = 'd'; 41 else if(S_ISCHR(m)) 42 str[0] = 'c'; 43 else if(S_ISBLK(m)) 44 str[0] = 'b'; 45 else if(S_ISFIFO(m)) 46 str[0] = 'q'; 47 else if(S_ISLNK(m)) 48 str[0] = 'l'; 49 // else if(S_ISSOCK(m)) 50 // str[0] = 's'; 51 else 52 str[0] = '?'; 53 54 str[1] = '