sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

env.c (819B)


      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: %s [-i] [-u var] ... [var=value] ... [cmd [arg ...]]\n", argv0);
     16 }
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21 	int savederrno;
     22 
     23 	ARGBEGIN {
     24 	case 'i':
     25 		*environ = NULL;
     26 		break;
     27 	case 'u':
     28 		if (unsetenv(EARGF(usage())) < 0)
     29 			eprintf("unsetenv:");
     30 		break;
     31 	default:
     32 		usage();
     33 	} ARGEND
     34 
     35 	for (; *argv && strchr(*argv, '='); argc--, argv++)
     36 		putenv(*argv);
     37 
     38 	if (*argv) {
     39 		execvp(*argv, argv);
     40 		savederrno = errno;
     41 		weprintf("execvp %s:", *argv);
     42 		_exit(126 + (savederrno == ENOENT));
     43 	}
     44 
     45 	for (; *environ; environ++)
     46 		puts(*environ);
     47 
     48 	return fshut(stdout, "<stdout>");
     49 }