~sircmpwn/ctools

ctools/src/env.c -rw-r--r-- 1.2 KiB
fbf17d92 — Gabor Koszegi tee: Fix missing output file truncation 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern char **environ;

static void
usage(void)
{
	fputs("env [-i] [name=value]... [utility [argument...]]\n", stderr);
}

int
main(int argc, char *argv[])
{
	if (argc > 1 && strcmp(argv[1], "-") == 0) {
		usage();
		return 1;
	}

	char opt;
	bool clear = false;
	while ((opt = getopt(argc, argv, "i")) != -1) {
		switch (opt) {
		case 'i':
			clear = true;
			break;
		default:
			usage();
			return 1;
		}
	}

	if (clear) {
		environ = NULL;
	}

	argc = argc - optind;
	argv = &argv[optind];

	int cmd_idx = 0;
	for (; cmd_idx < argc; cmd_idx++) {
		if (!strchr(argv[cmd_idx], '=')) {
			break;
		}
	}

	for (int i = 0; i < cmd_idx; i++) {
		if (putenv(argv[i]) != 0) {
			perror("env");
			return 1;
		}
	}

	/* No command, print the env */
	if (cmd_idx == argc) {
		for (int i = 0; environ && environ[i]; i++) {
			puts(environ[i]);
		}
		return 0;
	}

	argv = &argv[cmd_idx];

	execvp(argv[0], argv);

	/* if exec returns, it's errored */
	fprintf(stderr, "env: '%s': %s\n", argv[0], strerror(errno));
	switch (errno) {
	case ENOENT:
		return 127;
	default:
		return 126;
	}
}