[BACK]Return to apply.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / apply

Annotation of src/usr.bin/apply/apply.c, Revision 1.12

1.12    ! deraadt     1: /*     $OpenBSD: apply.c,v 1.11 2002/02/16 21:27:43 millert Exp $      */
1.1       deraadt     2: /*     $NetBSD: apply.c,v 1.3 1995/03/25 03:38:23 glass Exp $  */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Jan-Simon Pendry.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)apply.c    8.4 (Berkeley) 4/4/94";
                     43: #else
1.12    ! deraadt    44: static char rcsid[] = "$OpenBSD: apply.c,v 1.11 2002/02/16 21:27:43 millert Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: #include <sys/wait.h>
                     49:
                     50: #include <ctype.h>
                     51: #include <err.h>
                     52: #include <paths.h>
                     53: #include <signal.h>
                     54: #include <stdio.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
                     57: #include <unistd.h>
                     58:
1.11      millert    59: void   usage(void);
                     60: int    system(const char *);
1.1       deraadt    61:
                     62: int
                     63: main(argc, argv)
                     64:        int argc;
                     65:        char *argv[];
                     66: {
                     67:        int ch, clen, debug, i, l, magic, n, nargs, rval;
                     68:        char *c, *cmd, *p, *q;
1.12    ! deraadt    69:        size_t len;
1.1       deraadt    70:
                     71:        debug = 0;
                     72:        magic = '%';            /* Default magic char is `%'. */
                     73:        nargs = -1;
1.3       millert    74:        while ((ch = getopt(argc, argv, "a:d0123456789")) != -1)
1.1       deraadt    75:                switch (ch) {
                     76:                case 'a':
                     77:                        if (optarg[1] != '\0')
                     78:                                errx(1,
                     79:                                    "illegal magic character specification.");
                     80:                        magic = optarg[0];
                     81:                        break;
                     82:                case 'd':
                     83:                        debug = 1;
                     84:                        break;
                     85:                case '0': case '1': case '2': case '3': case '4':
                     86:                case '5': case '6': case '7': case '8': case '9':
                     87:                        if (nargs != -1)
                     88:                                errx(1,
                     89:                                    "only one -# argument may be specified.");
                     90:                        nargs = optopt - '0';
                     91:                        break;
                     92:                default:
                     93:                        usage();
                     94:                }
                     95:        argc -= optind;
                     96:        argv += optind;
                     97:
                     98:        if (argc < 2)
                     99:                usage();
                    100:
                    101:        /*
                    102:         * The command to run is argv[0], and the args are argv[1..].
                    103:         * Look for %digit references in the command, remembering the
                    104:         * largest one.
                    105:         */
                    106:        for (n = 0, p = argv[0]; *p != '\0'; ++p)
                    107:                if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
                    108:                        ++p;
                    109:                        if (p[0] - '0' > n)
                    110:                                n = p[0] - '0';
                    111:                }
                    112:
                    113:        /*
                    114:         * If there were any %digit references, then use those, otherwise
                    115:         * build a new command string with sufficient %digit references at
                    116:         * the end to consume (nargs) arguments each time round the loop.
                    117:         * Allocate enough space to hold the maximum command.
                    118:         */
1.12    ! deraadt   119:        len = sizeof("exec ") - 1 +
        !           120:            strlen(argv[0]) + 9 * (sizeof(" %1") - 1) + 1;
        !           121:        if ((cmd = malloc(len)) == NULL)
1.1       deraadt   122:                err(1, NULL);
                    123:
                    124:        if (n == 0) {
1.12    ! deraadt   125:                size_t l;
        !           126:
1.1       deraadt   127:                /* If nargs not set, default to a single argument. */
                    128:                if (nargs == -1)
                    129:                        nargs = 1;
                    130:
1.12    ! deraadt   131:                l = snprintf(cmd, len, "exec %s", argv[0]);
        !           132:                if (l >= len)
        !           133:                        err(1, "snprintf");
        !           134:                len -= l;
        !           135:                p = cmd + l;
        !           136:
        !           137:                for (i = 1; i <= nargs; i++) {
        !           138:                        l = snprintf(p, len, " %c%d", magic, i);
        !           139:                        if (l >= len)
        !           140:                                err(1, "snprintf");
        !           141:                        len -= l;
        !           142:                        p += l;
        !           143:                }
1.1       deraadt   144:
                    145:                /*
                    146:                 * If nargs set to the special value 0, eat a single
                    147:                 * argument for each command execution.
                    148:                 */
                    149:                if (nargs == 0)
                    150:                        nargs = 1;
                    151:        } else {
1.12    ! deraadt   152:                (void)snprintf(cmd, len, "exec %s", argv[0]);
1.1       deraadt   153:                nargs = n;
                    154:        }
                    155:
                    156:        /*
                    157:         * Grab some space in which to build the command.  Allocate
                    158:         * as necessary later, but no reason to build it up slowly
                    159:         * for the normal case.
                    160:         */
                    161:        if ((c = malloc(clen = 1024)) == NULL)
                    162:                err(1, NULL);
                    163:
                    164:        /*
                    165:         * (argc) and (argv) are still offset by one to make it simpler to
                    166:         * expand %digit references.  At the end of the loop check for (argc)
                    167:         * equals 1 means that all the (argv) has been consumed.
                    168:         */
                    169:        for (rval = 0; argc > nargs; argc -= nargs, argv += nargs) {
                    170:                /*
                    171:                 * Find a max value for the command length, and ensure
                    172:                 * there's enough space to build it.
                    173:                 */
                    174:                for (l = strlen(cmd), i = 0; i < nargs; i++)
1.6       deraadt   175:                        l += strlen(argv[i+1]);
1.1       deraadt   176:                if (l > clen && (c = realloc(c, clen = l)) == NULL)
                    177:                        err(1, NULL);
                    178:
                    179:                /* Expand command argv references. */
                    180:                for (p = cmd, q = c; *p != '\0'; ++p)
1.12    ! deraadt   181:                        if (p[0] == magic && isdigit(p[1]) && p[1] != '0') {
        !           182:                                sprintf(q, "%s", argv[(++p)[0] - '0']);
        !           183:                                q += strlen(q);
        !           184:                        } else
1.1       deraadt   185:                                *q++ = *p;
                    186:
                    187:                /* Terminate the command string. */
                    188:                *q = '\0';
                    189:
                    190:                /* Run the command. */
                    191:                if (debug)
                    192:                        (void)printf("%s\n", c);
                    193:                else
                    194:                        if (system(c))
                    195:                                rval = 1;
                    196:        }
                    197:
                    198:        if (argc != 1)
                    199:                errx(1, "expecting additional argument%s after \"%s\"",
                    200:                    (nargs - argc) ? "s" : "", argv[argc - 1]);
                    201:        exit(rval);
                    202: }
                    203:
                    204: /*
                    205:  * system --
                    206:  *     Private version of system(3).  Use the user's SHELL environment
                    207:  *     variable as the shell to execute.
                    208:  */
                    209: int
                    210: system(command)
                    211:        const char *command;
                    212: {
                    213:        static char *name, *shell;
                    214:        pid_t pid;
1.10      millert   215:        int pstat;
                    216:        sigset_t mask, omask;
1.1       deraadt   217:        sig_t intsave, quitsave;
                    218:
                    219:        if (shell == NULL) {
                    220:                if ((shell = getenv("SHELL")) == NULL)
                    221:                        shell = _PATH_BSHELL;
                    222:                if ((name = strrchr(shell, '/')) == NULL)
                    223:                        name = shell;
                    224:                else
                    225:                        ++name;
                    226:        }
                    227:        if (!command)           /* just checking... */
                    228:                return(1);
                    229:
1.10      millert   230:        sigemptyset(&mask);
                    231:        sigaddset(&mask, SIGCHLD);
                    232:        sigprocmask(SIG_BLOCK, &mask, &omask);
1.4       bitblt    233:        switch(pid = fork()) {
1.1       deraadt   234:        case -1:                        /* error */
                    235:                err(1, "fork");
                    236:        case 0:                         /* child */
1.10      millert   237:                sigprocmask(SIG_SETMASK, &omask, NULL);
1.9       deraadt   238:                execl(shell, name, "-c", command, (char *)NULL);
1.1       deraadt   239:                err(1, "%s", shell);
                    240:        }
                    241:        intsave = signal(SIGINT, SIG_IGN);
                    242:        quitsave = signal(SIGQUIT, SIG_IGN);
1.5       deraadt   243:        pid = waitpid(pid, &pstat, 0);
1.10      millert   244:        sigprocmask(SIG_SETMASK, &omask, NULL);
1.1       deraadt   245:        (void)signal(SIGINT, intsave);
                    246:        (void)signal(SIGQUIT, quitsave);
1.5       deraadt   247:        return(pid == -1 ? -1 : pstat);
1.1       deraadt   248: }
                    249:
                    250: void
                    251: usage()
                    252: {
                    253:        (void)fprintf(stderr,
1.8       aaron     254:            "usage: apply [-#] [-a magic] [-d] command argument [...]\n");
1.1       deraadt   255:        exit(1);
                    256: }