#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
The file where the #include <sys/stat.h>//stat function is located
#include <dirent.h>
int main (void)
{
DIR *DP;
struct Dirent *ep;
struct STAT st;
Char dirp[50];
printf ("Please input a dir name:/n");
scanf ("%s", &DIRP); Read Directory Name
Dp=opendir ("Dirp"); Open the given directory
printf ("filename:/ttype:/tpermission/taccesstime/tlastmodtime/tsize/t");
if (dp!=null)//If the directory opened successfully, then the operation is performed.
{
while (ep = Readdir (DP))//Read the cycle of each directory entry
{
if (ep->d_name[0]!= '. ') To determine if the first character of the file name is '. ', if it is, to indicate that it is an implied file, we do not move, otherwise operation
{
Use the STAT function to open the file information, the first parameter is the path of the file, the second parameter holds the information of the file
if (stat (ep->d_name,&st)!=-1)/Read success
{
printf ("%s/t", ep->d_name);
if ((ST.ST_MODE&S_IFMT) ==s_ifdir)//Determine the type of file
printf ("directory/t"); Directory
else if ((ST.ST_MODE&S_IFMT) ==s_ifblk)//block file
printf ("Block special file/t");
else if ((ST.ST_MODE&S_IFMT) ==S_IFCHR)//special character file
printf ("character Special file/t");
else if ((ST.ST_MODE&S_IFMT) ==s_ifreg)//Normal file
printf ("Ordinary file/t");
else if ((ST.ST_MODE&S_IFMT) ==S_IFIFO)//pipe file
printf ("Pipefile file/t");
printf ("%o/t", st.st_mode&0x1ff); Permissions for files
printf ("%15s/t", CTime (St.st_atime)); File creation time
printf ("%15s/t", CTime (St.st_mtime)); File Last Modified Time
printf ("%ld/n", st.st_size); Size of File
}
}
}
Closedir (DP)//Close Directory
}
Else
Puts ("couldn ' t open the directory./n"); Output cannot open path when open is unsuccessful
return 0;
}