Linux applets that display file tree structure information

Source: Internet
Author: User
# Include <stdio. h> # include <sys/types. h> # include <sys/STAT. h> # include <unistd. h> # include <dirent. h> # include <errno. h> # include <stdlib. h> # include <string. h> // maximum directory depth # define max_dir_depth 64 # DEFINE _ Len 4 # define max_file_len 256 # define max_len (64 * _ Len) + max_file_len) # define max_path_len (64*32) void print_dir (const char * file_name, int depth, char * show_c, struct dirent * pdirres) {int I; int Pos = depth * _ Len; show_c [pos-_ Len + 1] = '|'; for (I = 0; I <_ LEN-1; I ++) show_c [pos-I] = '-'; show_c [POS + 1] = '\ 0'; printf ("% S % s \ n", show_c, file_name ); if (! Pdirres) {show_c [pos-_ Len + 1] = '';} for (I = 0; I <_ LEN-1; I ++) show_c [pos-I] = '';} void clear_c (INT depth, char * show_c) {show_c [(depth-1) * _ Len + 1] = '';} int is_last_dir (dir * pdir) {return 0;} int _ tree_dir (const char * dirname, int depth, char * show_c) {dir * pdir; struct stat file_stat; struct dirent diren; struct dirent * pdirres; char * tmpname; If (depth> = max_dir_depth) Return-1; Tmpname = malloc (max_path_len); If (! Tmpname) Return-1;/* Open dir */pdir = opendir (dirname); If (null = pdir) {printf ("% S % s \ n", dirname, strerror (errno); goto err;} do {If (readdir_r (pdir, & diren, & pdirres) {closedir (pdir); printf ("% s \ n ", strerror (errno); goto err;} If (! Pdirres) {clear_c (depth, show_c); break;} If (strcmp (". ", diren. d_name) = 0 | strcmp (".. ", diren. d_name) = 0) continue; snprintf (tmpname, max_path_len, "% S/% s", dirname, diren. d_name); bzero (& file_stat, sizeof (file_stat); If (-1 = Stat (tmpname, & file_stat) {closedir (pdir ); printf ("% s \ n", strerror (errno); goto err;} print_dir (diren. d_name, depth, show_c, pdirres); If (s_isdir (file_stat.st_mode) {// If (is_last_dir (pdir) Clear (depth, show_c); _ tree_dir (tmpname, depth + 1, show_c);} while (null! = Pdirres); closedir (pdir); free (tmpname); Return 0; err: Free (tmpname); Return-1;} int tree_dir (const char * path) {char * show_c; int ret; show_c = malloc (max_len); If (! Show_c) return errno; memset (show_c, '', max_len); ret = _ tree_dir (path, 1, show_c); free (show_c); return ;} # define version "version V1.0: created by dremi" int main (INT argc, char ** argv) {If (argc <2) Return-1; if (strcmp ("-V", argv [1]) = 0) {printf ("% s \ n", version); Return 0 ;} return tree_dir (argv [1]);}

Version 2:

 
int __tree_dir(const char* dirname, int depth, char *show_c){    DIR     *pDir;    struct stat file_stat;    struct dirent diren;    struct dirent *pDirRes;    char  *olddir;    if(depth >= MAX_DIR_DEPTH) return -1;    olddir= malloc(MAX_FILE_LEN);    if(!olddir) return -1;    getcwd(olddir, MAX_FILE_LEN);    if(chdir(dirname)){free(olddir);        printf("%s %s\n", dirname, strerror(errno));        return -1;    }    /* open dir */    pDir = opendir("./");    if(NULL == pDir){free(olddir);        printf("%s %s\n", dirname , strerror(errno));         return -1;    }    do{                       if(readdir_r(pDir, &diren, &pDirRes)){            closedir(pDir);free(olddir);            printf("%s\n", strerror(errno));            return -1;        }        if(!pDirRes) { clear_c(depth, show_c); break; }        if(strcmp(".", diren.d_name) == 0 || strcmp("..", diren.d_name)==0) continue;        bzero(&file_stat, sizeof(file_stat));        if(-1 == stat(diren.d_name, &file_stat)){            closedir(pDir);free(olddir);            printf("%s\n", strerror(errno));            return -1;        }        print_dir(diren.d_name, depth, show_c, pDirRes);        if(S_ISDIR(file_stat.st_mode)){//            if(is_last_dir(pDir)) clear(depth, show_c);            __tree_dir(diren.d_name, depth+1, show_c);        }    }while(NULL != pDirRes);    closedir(pDir);    chdir(olddir);    free(olddir);    return 0;}


The effect is as follows:

 

 |---main_x64 |---main_x86 |---test_asm |   |---a.out |   |---asm.c |   |---get_reg.c |---test_dll |   |---main |   |---mk_dll.sh |   |---test_dll1.c |   |---test_dll1.h |   |---test_dll2.c |   |---test_dll2.h |   |---libtestdll1.so |   |---libtestdll2.so |   |---main.c |---test_log |   |---test_log.c |   |---a.out |---test1.c |---test2.c |---com_prog


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.