Subdirectory listing

Maninder Chawla mani at csun1.UUCP
Sun May 21 07:04:30 AEST 1989


>From article <215 at cs.columbia.edu>, by olasov at 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 at csun1.cs.uga.edu
mani%csun1.cs.uga.edu at gatech.edu
 

_____________________________________________________________________________


# include <string.h>
# include <stdio.h>
# include <sys/types.h>
# include <sys/dir.h>
# include <sys/stat.h>
# 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 <stdio.h>
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);
}
	   



More information about the Comp.unix.wizards mailing list