linux下用C語言實現(xiàn)ls -l的功能

首先上圖:

ls- l效果圖

ls -l 是查看文件的詳細信息:

主要包括:文件類型,權(quán)限信息,所有者信息,所有組信息,文件大小,時間,文件名。

補充:

(1)判斷文件類型的方法

buf.st_mode & S_IFMT 的值等于哪一個文件類型的宏,那么這個文件就是該類型

? ? ? ? S_IFMT? ? 0170000? bit mask for the file type bit fields

? ? ? ? S_IFSOCK? 0140000? socket//套接字文件

? ? ? ? S_IFLNK? ? 0120000? symbolic link//符號鏈接

? ? ? ? S_IFREG? ? 0100000? regular file//普通文件

? ? ? ? S_IFBLK? ? 0060000? block device//塊設(shè)備文件

? ? ? ? S_IFDIR? ? 0040000? directory//目錄文件

? ? ? ? S_IFCHR? ? 0020000? character device//字符設(shè)備文件

? ? ? ? S_IFIFO? ? 0010000? FIFO

(2)判斷權(quán)限(權(quán)限按8字節(jié)存儲)

buf.st_mode & 權(quán)限對應(yīng)宏 == 權(quán)限本身 則擁有該權(quán)限

所屬者權(quán)限

{

????????S_IRUSR? ? 00400? ? owner has read permission

? ? ? ? S_IWUSR? ? 00200? ? owner has write permission

? ? ? ? S_IXUSR? ? 00100? ? owner has execute permission

}

所屬組權(quán)限

{

? ? ? ? S_IRGRP? ? 00040? ? group has read permission

????????S_IWGRP? ? 00020? ? group has write permission

? ? ? ? S_IXGRP? ? 00010? ? group has execute permission ?

}

其他人權(quán)限

{

? ? ? ? S_IROTH? ? 00004? ? others have read permission

? ? ? ? S_IWOTH? ? 00002? ? others have write permission

? ? ? ? S_IXOTH? ? 00001? ? others have execute permission

}

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <sys/types.h>

#include <dirent.h>

#include <time.h>

#include <sys/stat.h>

#include <pwd.h>

#include <grp.h>


int main(int argc, char *argv[])

{

????????struct dirent *file = NULL;

????????struct stat buf;

????????struct tm *myt = NULL;

????????int ret = 0;

????????int i = 0;

????????DIR *dir = opendir(".");

????????if(dir == NULL)

????????{

????????????????perror("opendir");

????????????????return -1;

????????}

????????while((file = readdir(dir)) != NULL)

????????{

? ? ? ? ? ? ? ? ? ? if(file->d_name[0] != '.')//去除隱藏文件

????????????????????{

????????????????????????????????ret = lstat(file->d_name, &buf);//獲取文件屬性

????????????????????????????????if(ret < 0)

????????????????????????????????{

????????????????????????????????????????perror("latat");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return -1;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myt = localtime(&buf.st_mtime);//獲取時間

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?switch(buf.st_mode & S_IFMT)//判斷文件類型

????????????????????????????????{

????????????????????????????????????????????case S_IFSOCK:printf("s ");break;//套接字

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?case S_IFLNK: printf("l ");break;//鏈接

? ? ? ? ? ? ? ????????????????????????????? case S_IFREG: printf("- ");break;//普通文件

????????????????????????????????????????????case S_IFBLK: printf("b ");break;//塊設(shè)備文件

????????????????????????????????????????????case S_IFDIR: printf("d ");break;//目錄文件

????????????????????????????????????????????case S_IFCHR: printf("c ");break;//字符設(shè)備文件

? ? ? ? ? ? ? ? ????????????????????????????case S_IFIFO: printf("p ");break;//管道文件

????????????????????????????????}

????????????????????????????????char arr[3] = {'r', 'w', 'x'};//打印權(quán)限(權(quán)限問題在文章開頭有介紹)

????????????????????????????????for(i=0;i<9;i++)

????????????????????????????????{

????????????????????????????????????????????printf("%c",buf.st_mode & (0400>>i) ? arr[i%3] : '-');

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?printf("? %ld? ",buf.st_nlink);//硬鏈接數(shù)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?printf("? %s? ",getpwuid(buf.st_uid)->pw_name);//所屬者

????????????????????????????????????printf("%s? ",getgrgid(buf.st_gid)->gr_name);//所屬組

????????????????????????????????????printf("%-8ld? ",buf.st_size);//內(nèi)存大小

????????????????????????????????????printf("%d月 %d %02d:%02d? ", myt->tm_mon+1, myt->tm_mday, myt->tm_hour, myt->tm_min);//時間

????????????????????????????????????printf("%s\n",file->d_name);//文件名

? ? ? ? ? ? ????????? }

? ? ? ? ? ? }

return 0;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容