/************************************************************************* > File name:treedir.c > Author: Krischou > Mail:[email protected] > Created time:tue-05:04:50 PM CST ******************************* *****************************************/#include<stdio.h>#include<sys/types.h>#include<dirent.h>#include<unistd.h>#include<string.h>#include<stdlib.h>#include<sys/stat.h>voidPrint_tree (Char* DirName,intspc_cnt);intMainintargcChar* argv[])//./main dir{ if(ARGC = =1) {Print_tree (".",0); }Else if(ARGC = =2) {Print_tree (argv[1],0); } return 0 ;}voidPrint_tree (Char* DirName,intspc_cnt) {DIR* PDIR;//Directory Pointers structDirent* Pent;//you can get the struct-body pointer for the d_name of the subdirectory. structStat My_stat;//gets the structure of the file information according to the absolute path Charold_path[ -]="";//Save the original work pathGETCWD (Old_path, -); Pdir= Opendir (dirname);//Open Directory if(Pdir = =NULL) {Perror ("Opendir"); Exit (1); } chdir (dirname); //switch to the directory you want to traverse while((Pent = Readdir (pdir))! =NULL) { if(STRNCMP (Pent->d_name,".",1) ==0|| STRNCMP (Pent->d_name,"..",2) ==0) { Continue ; } memset (&my_stat,0,sizeof(My_stat)); if(Stat (pent->d_name, &my_stat) = =-1) {perror ("Stat"); Exit (1); } if(S_isdir (My_stat.st_mode)) {printf ("%*s%s\n", spc_cnt," ", pentd_name); Print_tree (Pent->d_name, spc_cnt +5); }Else{printf ("%*s%s\n", spc_cnt," ", pentd_name); }} closedir (Pdir); ChDir (Old_path); //program end switch back to original directory}