]> pd.if.org Git - pdutils/commitdiff
implemented env
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 25 Apr 2015 20:49:56 +0000 (20:49 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Thu, 30 Apr 2015 02:28:43 +0000 (02:28 +0000)
utils/env/env.c [new file with mode: 0644]

diff --git a/utils/env/env.c b/utils/env/env.c
new file mode 100644 (file)
index 0000000..f3ec296
--- /dev/null
@@ -0,0 +1,61 @@
+#define _POSIX_C_SOURCE 200809L
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Public domain by Nathan Wagner */
+
+extern char **environ;
+
+static void printenv() {
+       char **env;
+       for (env = environ; *env; env++) {
+               printf("%s\n", *env);
+       }
+}
+
+static void clearenv() {
+       char var[1024]; /* TODO how long can the name be ? */
+       char *s;
+       int i;
+
+       while (*environ) {
+               i = 0;
+               s = *environ;
+               while (*s && *s != '=') {
+                       var[i++] = *s++;
+               }
+               var[i] = 0;
+               unsetenv(var);
+       }
+}
+
+int main(int ac, char *av[]) {
+       int arg;
+       arg = 1;
+       if (arg < ac && !strcmp(av[arg], "-i")) {
+               clearenv();
+               arg++;
+       }
+       while (arg < ac) {
+               char *eq;
+               eq = strchr(av[arg], '=');
+               if (eq) {
+                       *eq = 0;
+                       setenv(av[arg], eq+1, 1);
+                       *eq = '=';
+               } else {
+                       break;
+               }
+               arg++;
+       }
+
+       if (arg >= ac) {
+               printenv();
+               return 0;
+       }
+
+       execvp(av[arg], &av[arg]);
+       return 0;
+}