/* tree - simple indented directory If no arguments are provided, then current working directory is assumed otherwise, the arguments are considered to be directory names David Horton, Centre for Information Technology Research, University of Queensland horton@citr.uq.oz.au */ #include #define _INCLUDE_POSIX_SOURCE /* HP/UX */ #include #define _INCLUDE_HPUX_SOURCE /* HP/UX */ #define _INCLUDE_XOPEN_SOURCE /* HP/UX */ #include #include #include #define MF 200 #define FNLEN 200 #if !defined(S_ISLNK) # define S_ISLNK(_M) ((_M & S_IFMT)==S_IFLNK) /* test for symbolic link */ #endif #include #define INDENT " " static char just_dirs = '\0'; static char all_files = '\0'; void tree(char *root, int depth) { /* Process a unix directory */ DIR *dirp; struct dirent *dp; struct stat s; char buf[FNLEN]; int i; dirp = opendir(root); /* Open the top of the directory tree */ if (dirp == NULL) return; /* Cannot open */ while ((dp = readdir(dirp)) != NULL) { /*process entries in directory*/ if ( strcmp(dp->d_name, ".") == 0) continue; /* don't go infinite */ if ( strcmp(dp->d_name, "..") == 0) continue; /* don't go up */ if ( !all_files && (dp->d_name[0] == '.')) continue; /* skip "hidden files */ sprintf(buf,"%s/%s", root, dp->d_name); if (lstat(buf,&s)) { continue; /* no data available */ } if( (S_ISDIR(s.st_mode)) || (just_dirs == '\0')) { /* Output the filename */ for(i = 0; i < depth; i++) printf(INDENT); printf("%s\n", dp->d_name); } if (S_ISLNK(s.st_mode)) continue; /* don't follow symbolic links */ if (S_ISDIR(s.st_mode)) { /* go down sub-tree */ tree(buf, depth+1); } if (!S_ISREG(s.st_mode)) { /* only look at regular files */ continue; } } (void) closedir(dirp); } static char Copyright[] = "@(#) Copyright 1994, David Horton"; static char rcs[] = "@(#)$RCSfile$ $Revision$"; main(int argc, char *argv[]) { int i, j; for (i = 1; i < argc; i++) { if(argv[i][0] == '-') { switch(argv[i][1]) { case 'a': all_files = 'Y'; break; case 'd': just_dirs = 'Y'; break; case 'h': fprintf(stderr, "Usage: %s [-h] [-v] [-a] [-d] [directory]...\n",argv[0]); exit(0); break; case 'v': fprintf(stderr, "%s : %s\n",argv[0], Copyright); fprintf(stderr, "%s\n", rcs); exit(0); break; } for (j=i+1; j <= argc; j++) { argv[j-1] = argv[j]; } i--; argc--; } } if (argc == 1) { /* no args -> read filenames from stdin */ tree(".", 0); } else { /* otherwise a list of directory heads */ for (i = 1; i < argc; i++) { printf("%s\n", argv[i]); tree(argv[i], 1); } } exit (0); }