1#include <unistd.h>2#include <stdio.h>3#include <dirent.h>4 5#include <string.h>6#include <sys/stat.h>7#include <stdlib.h>8 9 voidPrintdir (Char* Dir,intDepth//This is the main print directory function, parameter dir to characterize the path, parameter depth to represent the indentation of the spaceTen { OneDIR *DP;//declares a DIR * structure of DP A structDirent *entry;//Declare the DIRENT structure pointer entry This structure contains ino_t format D_ino and char format d_name[] - structStat statbuf;//declaring the statbuf of the stat format - the if(DP = Opendir (dir)) ==null)//, open Directory, create directory stream, determine open directory is not empty - { -fprintf (stderr,"cannot open directory:%s\n", dir);//if empty, write a hint to the standard error. - return; + } -ChDir (dir);//change to the incoming directory . + while((Entry=readdir (DP))!=null)//read the directory stream DP, get a pointer to the successful word into the loop A { atLstat (ENTRY->D_NAME,&STATBUF);//gets the description of the specified path (dirent structure pointer) to the STAT structure STATBUF - if(S_isdir (Statbuf.st_mode))//determine if this path file is a directory, -{//It's a catalogue, a loop. - if(strcmp (".", entry->d_name) = =0|| strcmp"..", entry->d_name) = =0) - Continue;//if it is. Or. directory, continue execution. -printf"%*s%s/\n", Depth," ", entry->d_name);//first, according to depth indentation, print the file name of this directory inPrintdir (Entry->d_name, depth+4);//recursive invocation of the function itself, that is, into this directory, indentation increased by four spaces -}//(Layer 2) In this function only the file, do not enter this loop to Else +printf"%*s%s\n", Depth," ", entry->d_name);//if it is not a directory, print the file name according to depth indentation - } theChDir"..");//If the first loop fails, return to the parent directory (Layer 2) After the print is complete, jump out of the directory and continue with the unfinished print *Closedir (DP);//Close the Open stream (Layer 2) to close the stream to prevent more than $ }Panax Notoginseng intMain (intargcChar* argv[])//Main function - { the Char* topdir=".";//the Topdir that defines the char * type points to the current directory. + if(argc>=2)//if the input parameter is not empty Atopdir=argv[1];//assign the path you want to print to Topdir the //Argv[0] Represents the program itself, if the input parameter is empty, it is the default print directory is Topdir= "." +printf"Directory scan of%s\n", Topdir);//Print a word -Printdir (Topdir,0);//Call function to implement loop printing $printf"done.\n"); $ -Exit0); -}
Operating effect:
1[Email protected]:~/Desktop $ ls2A. outApue.h apue.h~ printdir.c printdir.c~ Untitled Document no title document ~3[Email protected]:~/Desktop $ gcc printdir.c4[Email protected]:~/Desktop $./A. out 5 Directory scan of.6printdir.c~7 Untitled Document8apue.h~9A. outTen printdir.c One apue.h AUntitled Document ~ -Done.
Reference documents:
Linux Programming Neil Matthew
UNIX Environment Advanced Programming W. Richard Stevens
Http://www.cnblogs.com/avril/archive/2010/03/22/1691477.html
A Linux directory scanner