[comp.sources.amiga] foreach--executes commands multiply with wildcard expansion. 1 of 1.

ahh@j.cc.purdue.edu (Brent L. Woods) (03/11/88)

Program Name:  foreach
Submitted By:  Jonas Flygare
Summary:  A program that allows multiple executions of a command
          with Amiga-style wildcards (even if the command doesn't
          support them).
Poster Boy:  Brent Woods  (ahh@j.cc.purdue.edu)
Tested.  Part 1 of 1.

NOTES:



Brent Woods, Co-Moderator, comp.{sources,binaries}.amiga

USENET:  ...!j.cc.purdue.edu!ahh     ARPANET:  ahh@j.cc.purdue.edu
BITNET:  PODUM@PURCCVM               PHONE:  +1 (317) 743-8421
USNAIL:  320 Brown St., #406  /  West Lafayette, IN  47906

================================================================
#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	foreach.readme
#	foreach.c
# This archive created: Wed Mar  9 15:42:58 1988
# By:	Brent L. Woods (Co-Moderators Unlimited.)
cat << \SHAR_EOF > foreach.readme
(I would be surprised if this haven't been written already, it isn't
that advanced, but I couldn't find it on my fishdisks.)

Here is a small program I wrote in just about 1.5 hours, after
being really fed up with having to do 15 compress <filename>
just because compress won't accept wildcards. Argh! (I just got
the sources, and have fixed that.. ;-) But, I found the program to
be useful for other things, so I kept it around. It probably is full
of bugs (it *is* short, but that never stopped me from writing buggy
programs.. Try TECO if you want to produce more bugs with one line of
code, than You thought was possible.. ) Anyway, You get the uuencoded
executable, together with the source, and this file. That is just 
about all documentation You get. The program is called foreach, as in
"foreach #?.c compress -v" which will run compress (with flag -v) on
all files matching #?.c. It isn't blindingly fast, since I put a call
to Execute in there. It isn't *that* slow either. Considering the time 
put into this You get it for free. (But when I write my *really* great
program I'll have you pay through the nose to get it.. ;-) However..
If I should happen to recieve email regarding obvious bugs or great
improvements, I would feel really happy. So please do. 

Here's how to reach me.. (first one preferred)
jonasf@kuling.UUCP (domain: jonasf@kuling.uu.se)
flax@suadb.UUCP    (domain: flax@suadb.kth.se possibly .su.se)

or:

Jonas Flygare
Vaktargatan 32 F:621
754 22 Uppsala
SHAR_EOF
cat << \SHAR_EOF > foreach.c
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <libraries/dosextens.h>

/* Compiled with Lattice v4.0 */

void main(argc, argv)
int argc;
char **argv;
{
    char *filestring;
    char *commandstring;
    int error;
    int i;
    struct FILEINFO info;
    int attr=0;
    
    /* One could really look more at the arguments more here... */
    
    if (argc<3)
    {
        printf("Usage is %ls <filepattern> <command> <extra args>\n", argv[0]);
        exit(0);
    }
    
    /* Get filepattern and command to apply */
    
    filestring=strdup(argv[1]);
    commandstring=strdup(argv[2]);
    
    /* Make sure command gets all the supplied args.. */
    
    for(i=3;i<argc;i++)
    {
        strcat(commandstring, " ");
        strcat(commandstring, argv[i]);
    }
    
    strcat(commandstring, " ");
        
    if((error=dfind(&info, filestring, attr))!=0)
    {
        printf("\nNo file named %ls", filestring);
        exit(0);
    } 
    
    do 
    if(Execute(strcat(strdup(commandstring), info.fib_FileName),0,0)==0)
    {
        printf("Execute failed with command %ls\n and filename %ls\n",
        commandstring, info.fib_FileName);
        exit(0);
    }
    
    /* First strdup to create an untouched copy of commandstring, then */
    /* execute it with current filename. */
    
    while(!dnext(&info));  /* Nice test. */
    
    printf("\nDone!\n");
    
}
SHAR_EOF
#	End of shell archive
exit 0