chiark / gitweb /
Initial revision
[ssr] / StraySrc / Utilities / c / each
1 /*
2  * each.c
3  *
4  * Run a command on each of a bunch of files
5  *
6  * © 1998 Straylight/Edgeware
7  */
8
9 /*----- Licensing note ----------------------------------------------------*
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, write to the Free Software Foundation,
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "kernel.h"
34 #include "swis.h"
35 #include "swiv.h"
36
37 #include "glob.h"
38
39 /*----- Run commands ------------------------------------------------------*/
40
41 typedef struct each_ctx {
42   int estat;
43   const char *skel;
44   size_t sksz;
45 } each_ctx;
46
47 /* --- @run@ --- *
48  *
49  * Arguments:   @const char *f@ = pointer to a filename
50  *              @void *ctx@ = pointer to a context block
51  *
52  * Returns:     ---
53  *
54  * Use:         Handles files.
55  */
56
57 static void run(const char *f, void *ctx)
58 {
59   each_ctx *ex = ctx;
60   char buf[1024];
61   int e;
62
63   _swi(OS_SubstituteArgs, _inr(0, 4),
64        f, buf, sizeof(buf), ex->skel, ex->sksz);
65   e = system(buf);
66   switch (e) {
67     case 0:
68       /* Nothing to do */;
69       break;
70     case -2:
71       fprintf(stderr, "each: couldn't run `%s': %s\n",
72               buf, _kernel_last_oserror()->errmess);
73     default:
74       ex->estat = e;
75       break;
76   }
77 }
78
79 /* --- @main@ --- *
80  *
81  * Arguments:   @int argc@ = number of arguments
82  *              @char *argv[]@ = list of arguments
83  *
84  * Returns:     Zero if all went well
85  *
86  * Use:         Installs files in the right places.  This is a bit of a
87  *              rip-off of unix `cp'.
88  */
89
90 int main(int argc, char *argv[])
91 {
92   each_ctx ex;
93   int i;
94
95   /* --- Make sure we have some arguments --- */
96
97   if (argc < 3) {
98     fprintf(stderr, "Usage: each COMMAND FILE...\n");
99     exit(1);
100   }
101
102   /* --- Go to work --- */
103
104   ex.skel = argv[1];
105   ex.sksz = strlen(ex.skel);
106   ex.estat = 0;
107
108   for (i = 2; i < argc; i++) {
109     if (glob(argv[i], run, &ex) == 0)
110       run(argv[i], &ex);
111   }
112
113   /* --- Done --- */
114
115   return (ex.estat);
116 }
117
118 /*----- That's all, folks -------------------------------------------------*/