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