Move start_stop_daemon to debianutils.
[oweals/busybox.git] / coreutils / env.c
index 56577b611038ba58eee36a3f51f3d62fe24c8966..a07bd324cd986be80be5e1a080bb6959ad629b60 100644 (file)
  *
  * Original copyright notice is retained at the end of this file.
  *
- * Modified for BusyBox by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ * Modified for BusyBox by Erik Andersen <andersen@codepoet.org>
+ */
+
+/* BB_AUDIT SUSv3 compliant */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/env.html */
+
+/* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
+ *
+ * Fixed bug involving exit return codes if execvp fails.  Also added
+ * output error checking.
  */
 
 #include <stdio.h>
 #include <string.h>
-#include <getopt.h>
 #include <stdlib.h>
+#include <errno.h>
 #include <unistd.h>
 #include "busybox.h"
 
 extern int env_main(int argc, char** argv)
 {
        char **ep, *p;
-       char *cleanenv[1];
-       int ch;
+       char *cleanenv[1] = { NULL };
+       unsigned long opt;
 
-       while ((ch = getopt(argc, argv, "-iu:")) != -1)
-               switch(ch) {
-               case '-':
-               case 'i':
+       opt = bb_getopt_ulflags(argc, argv, "iu:", &p);
+       if(opt & 1)
                        environ = cleanenv;
-                       cleanenv[0] = NULL;
-                       break;
-               case 'u':
-                       unsetenv(optarg);
-                       break;
-               default:
-                       show_usage();
+       if(opt & 2)
+               unsetenv(p);
+
+       argv += optind;
+
+       if (*argv && (argv[0][0] == '-') && !argv[0][1]) {
+               environ = cleanenv;
+               ++argv;
+       }
+
+       while (*argv && ((p = strchr(*argv, '=')) != NULL)) {
+               if (putenv(*argv) < 0) {
+                       bb_perror_msg_and_die("putenv");
                }
-       for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
-               setenv(*argv, ++p, 1);
+               ++argv;
+       }
+
        if (*argv) {
                execvp(*argv, argv);
-               perror_msg_and_die("%s", *argv);
+               bb_perror_msg("%s", *argv);     /* Avoid multibyte problems. */
+               return (errno == ENOENT) ? 127 : 126;   /* SUSv3-mandated exit codes. */
        }
-       for (ep = environ; *ep; ep++)
-               printf("%s\n", *ep);
-       exit(EXIT_SUCCESS);
+
+       for (ep = environ; *ep; ep++) {
+               puts(*ep);
+       }
+
+       bb_fflush_stdout_and_exit(0);
 }
 
 /*