/* * each.c * * Run a command on each of a bunch of files * * © 1998 Straylight/Edgeware */ /*----- Licensing note ----------------------------------------------------* * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, write to the Free Software Foundation, * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*----- Header files ------------------------------------------------------*/ #include #include #include #include #include "kernel.h" #include "swis.h" #include "swiv.h" #include "glob.h" /*----- Run commands ------------------------------------------------------*/ typedef struct each_ctx { int estat; const char *skel; size_t sksz; } each_ctx; /* --- @run@ --- * * * Arguments: @const char *f@ = pointer to a filename * @void *ctx@ = pointer to a context block * * Returns: --- * * Use: Handles files. */ static void run(const char *f, void *ctx) { each_ctx *ex = ctx; char buf[1024]; int e; _swi(OS_SubstituteArgs, _inr(0, 4), f, buf, sizeof(buf), ex->skel, ex->sksz); e = system(buf); switch (e) { case 0: /* Nothing to do */; break; case -2: fprintf(stderr, "each: couldn't run `%s': %s\n", buf, _kernel_last_oserror()->errmess); default: ex->estat = e; break; } } /* --- @main@ --- * * * Arguments: @int argc@ = number of arguments * @char *argv[]@ = list of arguments * * Returns: Zero if all went well * * Use: Installs files in the right places. This is a bit of a * rip-off of unix `cp'. */ int main(int argc, char *argv[]) { each_ctx ex; int i; /* --- Make sure we have some arguments --- */ if (argc < 3) { fprintf(stderr, "Usage: each COMMAND FILE...\n"); exit(1); } /* --- Go to work --- */ ex.skel = argv[1]; ex.sksz = strlen(ex.skel); ex.estat = 0; for (i = 2; i < argc; i++) { if (glob(argv[i], run, &ex) == 0) run(argv[i], &ex); } /* --- Done --- */ return (ex.estat); } /*----- That's all, folks -------------------------------------------------*/