env.c

Go to the documentation of this file.
00001 /*      env 1.1 - Set environment for command           Author: Kees J. Bot
00002  *                                                              17 Dec 1997
00003  */
00004 #define nil 0
00005 #include <sys/types.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <unistd.h>
00010 #include <errno.h>
00011 
00012 int main(int argc, char **argv)
00013 {
00014         int i;
00015         int iflag= 0;
00016         int aflag= 0;
00017         extern char **environ;
00018 
00019         i= 1;
00020         while (i < argc && argv[i][0] == '-') {
00021                 char *opt= argv[i++] + 1;
00022 
00023                 if (opt[0] == '-' && opt[1] == 0) break;        /* -- */
00024 
00025                 if (opt[0] == 0) iflag= 1;                      /* - */
00026 
00027                 while (*opt != 0) switch (*opt++) {
00028                 case 'i':
00029                         iflag= 1;       /* Clear environment. */
00030                         break;
00031                 case 'a':               /* Specify arg 0 separately. */
00032                         aflag= 1;
00033                         break;
00034                 default:
00035                         fprintf(stderr,
00036                 "Usage: env [-ia] [name=value] ... [utility [argument ...]]\n");
00037                         exit(1);
00038                 }
00039         }
00040 
00041         /* Clear the environment if -i. */
00042         if (iflag) *environ= nil;
00043 
00044         /* Set the new environment strings. */
00045         while (i < argc && strchr(argv[i], '=') != nil) {
00046                 if (putenv(argv[i]) != 0) {
00047                         fprintf(stderr, "env: Setting '%s' failed: %s\n",
00048                                 argv[i], strerror(errno));
00049                         exit(1);
00050                 }
00051                 i++;
00052         }
00053 
00054         /* Environment settings and command may be separated with '--'.
00055          * This is for compatibility with other envs, we don't advertise it.
00056          */
00057         if (i < argc && strcmp(argv[i], "--") == 0) i++;
00058 
00059         if (i >= argc) {
00060                 /* No utility given; print environment. */
00061                 char **ep;
00062 
00063                 for (ep= environ; *ep != nil; ep++) {
00064                         if (puts(*ep) == EOF) {
00065                                 fprintf(stderr, "env: %s\n", strerror(errno));
00066                                 exit(1);
00067                         }
00068                 }
00069                 return 0;
00070         } else {
00071                 char *util, **args;
00072                 int err;
00073 
00074                 util= argv[i];
00075                 args= argv + i;
00076                 if (aflag) args++;
00077                 (void) execvp(util, args);
00078                 err= errno;
00079                 fprintf(stderr, "env: Can't execute %s: %s\n",
00080                         util, strerror(err));
00081                 return err == ENOENT ? 127 : 126;
00082         }
00083 }

Generated on Fri Apr 14 22:57:06 2006 for minix by  doxygen 1.4.6