morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

env.c (721B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <unistd.h>
      7 
      8 #include "util.h"
      9 
     10 extern char **environ;
     11 
     12 static void
     13 usage(void)
     14 {
     15 	eprintf("usage: env [-i] [-u name]... [name=value]... [cmd [arg...]]\n");
     16 }
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21 	ARGBEGIN {
     22 	case 'i':
     23 		if(environ)
     24 			*environ = NULL;
     25 		break;
     26 	case 'u':
     27 		unsetenv(EARGF(usage()));
     28 		break;
     29 	default:
     30 		usage();
     31 	} ARGEND;
     32 
     33 	for (; *argv && strchr(*argv, '='); argv++)
     34 		putenv(*argv);
     35 
     36 	if (*argv) {
     37 		execvp(*argv, argv);
     38 		enprintf(127 - (errno != EEXIST), "env: '%s':", *argv);
     39 	}
     40 
     41 	while (environ && *environ)
     42 		printf("%s\n", *environ++);
     43 
     44 	return 0;
     45 }