]> git.saurik.com Git - apple/shell_cmds.git/blobdiff - env/env.c
shell_cmds-216.60.1.tar.gz
[apple/shell_cmds.git] / env / env.c
index 42b754288e22190921c2c06ebc0a4457342f881e..3dc152ad37a7b26f7f373e328736fbb4123508a9 100644 (file)
--- a/env/env.c
+++ b/env/env.c
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  * SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
 #ifndef lint
-__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
-       The Regents of the University of California.  All rights reserved.\n");
+static const char copyright[] =
+"@(#) Copyright (c) 1988, 1993, 1994\n\
+       The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
+#if 0
 #ifndef lint
-/*static char sccsid[] = "@(#)env.c    8.3 (Berkeley) 4/2/94";*/
-__RCSID("$NetBSD: env.c,v 1.10 1997/10/18 13:55:28 lukem Exp $");
+static char sccsid[] = "@(#)env.c      8.3 (Berkeley) 4/2/94";
 #endif /* not lint */
+#endif
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
 
 #include <err.h>
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <locale.h>
-#include <errno.h>
 
-int    main __P((int, char **));
-static void usage __P((void));
+#include "envopts.h"
+
+extern char **environ;
+
+int     env_verbosity;
+
+static void usage(void);
 
 int
-main(argc, argv)
-       int argc;
-       char **argv;
+main(int argc, char **argv)
 {
-       extern char **environ;
-       extern int optind;
-       char **ep, *p;
+       char *altpath, **ep, *p, **parg;
        char *cleanenv[1];
-       int ch;
-
-       setlocale(LC_ALL, "");
+       int ch, want_clear;
+       int rtrn;
 
-       while ((ch = getopt(argc, argv, "-i")) != -1)
-               switch((char)ch) {
-               case '-':                       /* obsolete */
+       altpath = NULL;
+       want_clear = 0;
+       while ((ch = getopt(argc, argv, "-iP:S:u:v")) != -1)
+               switch(ch) {
+               case '-':
                case 'i':
-                       environ = cleanenv;
-                       cleanenv[0] = NULL;
+                       want_clear = 1;
+                       break;
+               case 'P':
+                       altpath = strdup(optarg);
+                       break;
+               case 'S':
+                       /*
+                        * The -S option, for "split string on spaces, with
+                        * support for some simple substitutions"...
+                        */
+                       split_spaces(optarg, &optind, &argc, &argv);
+                       break;
+               case 'u':
+                       if (env_verbosity)
+                               fprintf(stderr, "#env unset:\t%s\n", optarg);
+                       rtrn = unsetenv(optarg);
+                       if (rtrn == -1)
+                               err(EXIT_FAILURE, "unsetenv %s", optarg);
+                       break;
+               case 'v':
+                       env_verbosity++;
+                       if (env_verbosity > 1)
+                               fprintf(stderr, "#env verbosity now at %d\n",
+                                   env_verbosity);
                        break;
                case '?':
                default:
                        usage();
                }
-
-       for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
-               (void)setenv(*argv, ++p, 1);
-
+       if (want_clear) {
+               environ = cleanenv;
+               cleanenv[0] = NULL;
+               if (env_verbosity)
+                       fprintf(stderr, "#env clearing environ\n");
+       }
+       for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
+               if (env_verbosity)
+                       fprintf(stderr, "#env setenv:\t%s\n", *argv);
+               *p = '\0';
+               rtrn = setenv(*argv, p + 1, 1);
+               *p = '=';
+               if (rtrn == -1)
+                       err(EXIT_FAILURE, "setenv %s", *argv);
+       }
        if (*argv) {
-               /* return 127 if the command to be run could not be found; 126
-                  if the command was was found but could not be invoked */
-
+               if (altpath)
+                       search_paths(altpath, argv);
+               if (env_verbosity) {
+                       fprintf(stderr, "#env executing:\t%s\n", *argv);
+                       for (parg = argv, argc = 0; *parg; parg++, argc++)
+                               fprintf(stderr, "#env    arg[%d]=\t'%s'\n",
+                                   argc, *parg);
+                       if (env_verbosity > 1)
+                               sleep(1);
+               }
                execvp(*argv, argv);
-               err((errno == ENOENT) ? 127 : 126, "%s", *argv);
-               /* NOTREACHED */
+               err(errno == ENOENT ? 127 : 126, "%s", *argv);
        }
-
        for (ep = environ; *ep; ep++)
                (void)printf("%s\n", *ep);
-
        exit(0);
 }
 
 static void
-usage ()
+usage(void)
 {
-       (void) fprintf(stderr, "usage: env [-i] [name=value ...] [command]\n");
-       exit (1);
+       (void)fprintf(stderr,
+           "usage: env [-iv] [-P utilpath] [-S string] [-u name]\n"
+           "           [name=value ...] [utility [argument ...]]\n");
+       exit(1);
 }