2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
22 #define VERSION "0.01"
24 extern char **environ
;
26 int main(int argc
, char* argv
[])
30 for (arg
= 1; arg
< argc
; arg
++)
32 if (strcmp("--help", argv
[arg
]) == 0 || strcmp("-h", argv
[arg
]) == 0) {
33 printf("Usage: %s [name=value ...] [command]\n", argv
[0]);
35 "If no command is given, all environment variables are listed.\n"
37 " --help Print this message.\n"
38 " --version Show version info.\n");
40 } else if (strcmp("--version", argv
[arg
]) == 0 || strcmp("-v", argv
[arg
]) == 0) {
41 printf("env (mutos) v"VERSION
"\n");
49 // parse the key-value pairs
50 for ( ; arg
< argc
; arg
++)
53 char* equals
= strchr(argv
[arg
], '=');
54 // if there's no equals, it must be a command
60 char* name
= argv
[arg
];
61 // the value string starts right after the equals
62 char* value
= equals
+ 1;
63 setenv(name
, value
, 1);
68 for (int i
= arg
; i
< argc
; i
++)
70 total_len
+= strlen(argv
[i
]) + 1;
75 command
= calloc(total_len
, sizeof(char));
78 for (int i
= arg
; i
< argc
; i
++)
80 strcat(command
, argv
[i
]);
87 command
[total_len
- 1] = '\0';
93 for (char **env
= environ
; *env
; ++env
)