is to traverse all the files under a directory to display the information
Linux is a bit around here.
To open the directory via Opendir, return a DIR structure
Use Readdir to read the DIR structure, return the first item under the directory, is a dirent struct, call Readdir again to read the second item, and so on
The dirent structure contains the file name of the file, and the details of the file can be obtained through stat.
Pay attention to the stat structure to malloc, no, then segment fail?? It seems to be the word. If there is no allocation, the pointer will be randomly pointed.
Read and write execution permissions in mode_t, there is no direct output of the function, to write a self, to determine whether there is no such permission, with the operation, not sentenced and so on.
The rest of the information is in stat, which only outputs the St_mode.
#include <dirent.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>voidShow (mode_t mode) {if(mode&s_irusr) printf ("R"); Elseprintf ("-"); if(mode&s_iwusr) printf ("W"); Elseprintf ("-"); if(mode&s_ixusr) printf ("x"); Elseprintf ("-"); /*********group********/ if(mode&s_irgrp) printf ("R"); Elseprintf ("-"); if(mode&s_iwgrp) printf ("W"); Elseprintf ("-"); if(mode&s_ixgrp) printf ("x"); Elseprintf ("-"); /**********else***************/ if(mode&s_iroth) printf ("R"); Elseprintf ("-"); if(mode&s_iwoth) printf ("W"); Elseprintf ("-"); if(mode&s_ixoth) printf ("x"); Elseprintf ("-"); printf ("\ n");}intMainintargcChar*argv[]) {DIR*DP; structdirent*dir; structStat * BUF = (structStat *)malloc(sizeof(structstat)); intFD; DP= Opendir (argv[1]); while(dir = Readdir (DP))! =NULL) {printf ("%s",dir->d_name); //fd = open (DIR->D_NAME,O_RDWR); //Fstat (FD,BUF);Stat (dir->d_name,buf); Show (Buf-St_mode); } printf ("DONE !!!! \ n"); }
Implement Ls-l