Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!gatech!csun1!mani From: mani@csun1.UUCP (Maninder Chawla) Newsgroups: comp.unix.wizards Subject: Re: Subdirectory listing Message-ID: <187@csun1.UUCP> Date: 20 May 89 21:04:30 GMT References: <215@cs.columbia.edu> Distribution: usa Organization: Univ. of GA, CS Dept., Athens Lines: 86 From article <215@cs.columbia.edu>, by olasov@cs.columbia.edu (Benjamin Olasov): > > > I'm interested in finding a unix command usage that will return the complete > path names of all subdirectories below a given directory, so that, given a /* stuff deleted */ I am sure there are lots of ways to do this. Here's a recursive C program to do it. It has its own limitations like maximum path length. I have a tree printing version of it too. It uses curses, terminfo etc. The listing for syserr.h is also included. --mani mani@csun1.cs.uga.edu mani%csun1.cs.uga.edu@gatech.edu _____________________________________________________________________________ # include # include # include # include # include # include <"syserr.h"> char *path, *pname; int cnt; main() { path=(char *)malloc(100); pname=(char *)malloc(100); strcpy(path,".");cnt = 0; workout(); } workout() { struct stat *sbuf; struct direct *dlink; int nread; char *dname, *temp; DIR *dirp; sbuf=(struct stat *)malloc(sizeof(struct stat)); dlink=(struct direct *)malloc(sizeof(struct direct)); dname=(char *)malloc(20); if ((dirp=opendir(path)) == NULL ) syserr("open root"); while ((dlink=readdir(dirp)) != NULL) { if ( !dlink->d_ino ) continue; strcpy(dname,dlink->d_name); if (!strcmp(dname,".") || !strcmp(dname,"..")) continue; strcpy(pname,path);strcat(pname,"/");strcat(pname,dname); if (stat(pname, sbuf)== -1) syserr("stat"); if (((sbuf->st_mode) & S_IFMT) != S_IFDIR) continue; printf("%d %s is a directory. Path is %s\n",cnt++,dname,pname); strcat(path,"/"); strcat(path, dname); workout(); } closedir(dirp); if ((temp=strrchr(path,'/'))!=NULL) *temp = '\0'; } /* end of program */ _________________________________________________________________________ syserr.h listing # include syserr(msg) /* print system call error message & quit */ char *msg; { extern int errno, sys_nerr; extern char *sys_errlist[]; fprintf(stderr, "ERROR: %s (%d", msg, errno); if (errno > 0 && errno < sys_nerr ) fprintf(stderr,";%s)\n", sys_errlist[errno]); else fprintf(stderr,")\n"); exit(1); }