Execute a command w/wildcards & recursive descent

BURNS,JIM gt0178a at prism.gatech.EDU
Fri Aug 31 12:15:53 AEST 1990


Listed below is a 'C' program I wrote because I got tired of 'ls -R' and
'find' not understanding wildcards, even if quoted. Can anyone tell me how
to do something like

find . -type d -exec 'general command with wildcards and {}' \;

or any other equivalent Unix command? Obviously, I could do

find . -type d -exec scriptfile {} \;

and use $1 in the script any way I choose, but I don't want a different
script for every command I would want to use. If the solution uses 'find',
it must do the equivalent of evaluating '{}/wildcardpattern' for those
commands that want a list of filenames, not just a directory name, even
though something like '{}/*' is unexpandable in 'find'.

The program uses the SysV function ftw(3c) and could be executed with the
command 'ftw startdirectory unixcommand commandparms', e.g. 'ftw /usr/include
grep pattern \*.h'.

#include <stdio.h>
#include <ftw.h>
#include <string.h>

char command[80];
int walk();

main(argc,argv)
 
int	argc;
char	*argv[];

{
int ftwrtn,i;

	if (argc < 3) printf("USAGE: ftw path command\nargc=%d\n",argc);
	else {
	   strcpy(command,argv[2]);
	   for (i=3;i<argc;i++){
	      strcat(command," ");
	      strcat(command,argv[i]);
	   }
	   if((ftwrtn=ftw(argv[1],walk,60))==-1) perror("ftw(3c)");
	}
	exit(ftwrtn);
}

int walk(path,stat,num)
 
char *path;
int *stat,num;

{
char *cwd,*getcwd(),buf[100];

	if (num==FTW_D) {
	   if((cwd=getcwd(buf,100))==NULL) perror("getcwd(3c)");
	   if(chdir(path)==-1) perror("chdir(2)");
	   printf("****************************************\n");
	   printf("in %s do: %s\n",path,command);
	   printf("****************************************\n");
/* adapt the following line according to your system's syntax */
/*	   if(fflush(0)==-1) perror("fflush(3s)");*/
	   printf("\nSystem(3s) return value=%x\n\n\n",system(command));
	   if(chdir(cwd)==-1) perror("chdir(2)");
	}
	return(0);
}
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a at prism.gatech.edu



More information about the Comp.unix.questions mailing list