1. 程式人生 > >用C語言實現ls -l指令

用C語言實現ls -l指令

正常情況下我們在作業系統中使用ls -l指令的結果

在這裡插入圖片描述

第一列表示 檔案讀寫許可權
第二列表示 檔案的深度
第三列和第四列分別表示檔案的屬主和屬組
第五列是檔案的大小,以位元組作為單位
第六,七,八列是檔案最後一次修改的時間
最後一列是檔名

程式碼

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h> #include <unistd.h> #include <pwd.h> #include <grp.h> char* get_mode(mode_t m,char* str)//顯示檔案讀寫許可權函式 { if(S_ISREG(m)) strcpy(str,"-"); else if(S_ISDIR(m)) strcpy(str,"d"); else if(S_ISCHR(m)) strcpy(str,"c"); else if(S_ISBLK(m)) strcpy
(str,"b"); else if(S_ISFIFO(m)) strcpy(str,"f"); else if(S_ISLNK(m)) strcpy(str,"l"); else if(S_ISSOCK(m)) strcpy(str,"n"); // 屬主許可權 strcat(str,m&S_IRUSR?"r":"-"); strcat(str,m&S_IWUSR?"w":"-"); strcat(str,m&S_IXUSR?"x":"-"); // 同組許可權 strcat(str,m&
S_IRGRP?"r":"-"); strcat(str,m&S_IWGRP?"w":"-"); strcat(str,m&S_IXGRP?"x":"-"); // 其它許可權 strcat(str,m&S_IROTH?"r":"-"); strcat(str,m&S_IWOTH?"w":"-"); strcat(str,m&S_IXOTH?"x":"-"); return str; } int _time(int year)//計算是否是閏年函式 { if(year%4==0 && year%100 !=0 || year%400 == 0) return 29; return 28; } void time_ch(time_t num)//通過秒數來計算日期 { int year=1970; int month =1; int day =1; num = num + 8*3600; while(num >= 86400) { num-=86400; day++; if(month==1 && day == 32) { month++; day =1; } else if(month == 2 && day ==_time(year)+1) { month++; day =1; } else if(month == 3 && day == 32) { month++; day =1; } else if(month == 4 && day == 31) { month++; day=1; } else if(month == 5 && day == 32) { month++; day=1; } else if(month == 6 && day == 31) { month++; day=1; } else if(month == 7 && day == 32) { month++; day=1; } else if(month == 8 && day == 32) { month++; day=1; } else if(month == 9 && day == 31) { month++; day=1; } else if(month == 10 && day == 32) { month++; day=1; } else if(month == 11 && day == 31) { month++; day=1; } else if(month == 12 && day == 32) { month=1; day=1; year++; } } int hour = num/3600; int minute =num/60 -hour*60; printf("%2d月 %2d %2d:%2d ",month,day,hour,minute); } int main(int argc,char** argv,char** environ)//主函式 { char* dir_name=NULL; if(argc == 1) { dir_name="."; } else if(argc == 2) { dir_name = argv[1]; } else { puts("user:ls dir"); return -1; } DIR* dp=opendir(dir_name); if(NULL == dp) { perror("opendir"); return -1; } struct dirent* de=readdir(dp); for(;de;de=readdir(dp)) { if('.'==de->d_name[0]) continue; //通過檔名獲得檔案資訊 struct stat s; int ret = lstat(de->d_name,&s); if(0 > ret) { perror("stat"); return -1; } char str[11] = {}; printf("%s ",get_mode(s.st_mode,str));//型別 struct passwd *passwd; passwd = getpwuid(s.st_uid); printf ("%s ", passwd->pw_name);//主名 struct group *group; group = getgrgid(passwd->pw_gid); printf ("%s ", group->gr_name); //組名 printf("%5lu ",s.st_size);//大小 time_ch(s.st_mtime);//時間 printf("%s\t",de->d_name);//檔名 printf("\n"); } closedir(dp); }

PS.檔案深度沒有寫出來,正在修改中,希望有寫出來的大佬將這塊函式分享一下。

實現效果

在這裡插入圖片描述