b640867 |
Jamozed |
2020-07-09 21:48:14 |
0
|
// env.c, version 1.0.2 |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
1
|
// OMKOV coreutils implementation of POSIX env |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
4
|
|
b181413 |
Jamozed |
2022-02-05 22:32:00 |
5
|
#include "util/error.h" |
b181413 |
Jamozed |
2022-02-05 22:32:00 |
6
|
#include "util/optget.h" |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
7
|
|
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
8
|
#include <unistd.h> |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
9
|
|
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
10
|
#include <errno.h> |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
11
|
#include <stdio.h> |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
12
|
#include <stdlib.h> |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
13
|
#include <string.h> |
39afcc4 |
Jamozed |
2020-07-06 22:31:57 |
14
|
|
b640867 |
Jamozed |
2020-07-09 21:48:14 |
15
|
#define VERSION "1.0.2" |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
16
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
20
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
21
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
|
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
23
|
extern char **environ; |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
24
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
25
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
26
|
static void ver(void); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
27
|
|
b640867 |
Jamozed |
2020-07-09 21:48:14 |
28
|
int main(int ac, char *av[]) { (void)ac; A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
29
|
struct opt opt = OPTGET_INIT; opt.str = "i"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
30
|
for (int o; (o = optget(&opt, av, 0)) != -1;) switch (o) { |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
31
|
case 'i': { environ = NULL; break; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
32
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
33
|
case 257: { ver(); return 0; } |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
34
|
default: { return 1; } |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
35
|
} |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
36
|
|
39afcc4 |
Jamozed |
2020-07-06 22:31:57 |
37
|
register char **p = &av[opt.ind]; |
b640867 |
Jamozed |
2020-07-09 21:48:14 |
38
|
for (; *p && strchr(*p, '='); ++p) { putenv(*p); } |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
39
|
|
b640867 |
Jamozed |
2020-07-09 21:48:14 |
40
|
if (!*p) { |
b640867 |
Jamozed |
2020-07-09 21:48:14 |
41
|
if (environ) for (char **v = environ; *v; ++v) { puts(*v); } |
b640867 |
Jamozed |
2020-07-09 21:48:14 |
42
|
return 0; |
b640867 |
Jamozed |
2020-07-09 21:48:14 |
43
|
} |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
44
|
|
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
45
|
execvp(*p, &*p); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
46
|
|
39afcc4 |
Jamozed |
2020-07-06 22:31:57 |
47
|
warn("%s: %s", *p, serr()); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
48
|
return errno = ENOENT ? 127 : 126; |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
49
|
} |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
50
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
51
|
static void hlp(void) { |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
52
|
puts("env - execute with an altered enviroment\n"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
53
|
puts("usage: env [-i] [name=value]... [command [argument...]]\n"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
54
|
puts("options:"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
55
|
puts(" -i Ignore inherited enviroment"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
56
|
puts(" --help Display help information"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
57
|
puts(" --version Display version information"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
58
|
} |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
59
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
60
|
static void ver(void) { |
39afcc4 |
Jamozed |
2020-07-06 22:31:57 |
61
|
puts("OMKOV coreutils env, version " VERSION); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
62
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
63
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
e2c696c |
Jamozed |
2020-06-27 01:05:32 |
64
|
} |
|
|
|
65
|
|