~emersion/mrsh

ref: 22232fa45aebd16dba5668440fcb974be9d08254 mrsh/builtin/umask.c -rw-r--r-- 4.1 KiB
22232fa4 — Ben Brown Adding handling of symbolic arguments to umask 1 year, 9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#include <mrsh/builtin.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "builtin.h"

static const char umask_usage[] = "usage: umask [-S] [mode]\n";

enum umask_symbolic_state {
	UMASK_WHO,
	UMASK_PERM,
};

static mode_t umask_current_mask(void) {
	const mode_t default_mode = 0022;

	mode_t mask = umask(default_mode);
	umask(mask);
	return mask;
}

static bool umask_update_mode(mode_t *mode, char action, mode_t *perm_mask, mode_t *who_mask) {
	switch (action) {
	case '+':
		*mode |= (*who_mask & *perm_mask);
		break;
	case '=':
		*mode = (*mode & ~(*who_mask)) | (*perm_mask & *who_mask);
		break;
	case '-':
		*mode &= (0777 & ~(*who_mask & *perm_mask));
		break;
	default:
		fprintf(stderr, "unknown action -- '%c'\n", action);
		return false;
	}

	*perm_mask = *who_mask = 0;
	return true;
}

static bool umask_mode(mode_t *mode, char *symbolic) {
	mode_t tmp_mode = 0777 & ~(umask_current_mask());
	enum umask_symbolic_state state = UMASK_WHO;
	mode_t who_mask = 0;
	mode_t perm_mask = 0;
	char action = '\0';

	for (char *c = symbolic; *c != '\0'; c++) {
		switch (state) {
		case UMASK_WHO:
			switch (*c) {
			case 'u':
				who_mask |= S_IRWXU;
				break;
			case 'g':
				who_mask |= S_IRWXG;
				break;
			case 'o':
				who_mask |= S_IRWXO;
				break;
			case 'a':
				who_mask |= (S_IRWXU | S_IRWXG | S_IRWXO);
				break;
			case '+':
			case '-':
			case '=':
				if (who_mask == 0) {
					who_mask |= (S_IRWXU | S_IRWXG | S_IRWXO);
				}
				action = *c;
				state = UMASK_PERM;
				break;
			default:
				fprintf(stderr, "Unknown who -- '%c'\n", *c);
				return false;
			}

			break;

		case UMASK_PERM:
			switch (*c) {
			case 'u':
				perm_mask = (tmp_mode & S_IRWXU) | ((tmp_mode & S_IRWXU) >> 3) | ((tmp_mode & S_IRWXU) >> 6);
				break;
			case 'g':
				perm_mask = ((tmp_mode & S_IRWXG) << 3) | (tmp_mode & S_IRWXG) | ((tmp_mode & S_IRWXG) >> 3);
				break;
			case 'o':
				perm_mask = ((tmp_mode & S_IRWXO) << 6) | ((tmp_mode & S_IRWXO) << 3) | (tmp_mode & S_IRWXO);
				break;
			case 'r':
				perm_mask |= (S_IRUSR | S_IRGRP | S_IROTH);
				break;
			case 'w':
				perm_mask |= (S_IWUSR | S_IWGRP | S_IWOTH);
				break;
			case 'x':
				perm_mask |= (S_IXUSR | S_IXGRP | S_IXOTH);
				break;
			case ',':
				state = UMASK_WHO;
				if (!umask_update_mode(&tmp_mode, action, &perm_mask, &who_mask)) {
					return false;
				}
				break;
			default:
				fprintf(stderr, "Invalid permission -- '%c'\n", *c);
				return false;
			}
			break;
		}
	}

	if (state == UMASK_PERM) {
		if (!umask_update_mode(&tmp_mode, action, &perm_mask, &who_mask)) {
			return false;
		}
	} else {
		fprintf(stderr, "Missing permission from symbolic mode\n");
		return false;
	}

	*mode = 0777 & ~tmp_mode;

	return true;
}

static void umask_modestring(char string[static 4], mode_t mode) {
	size_t i = 0;

	if (S_IROTH & mode) {
		string[i++] = 'r';
	}

	if (S_IWOTH & mode) {
		string[i++] = 'w';
	}

	if (S_IXOTH & mode) {
		string[i++] = 'x';
	}
}

static void umask_print_symbolic(mode_t mask) {
	char user[4] = {0};
	char group[4] = {0};
	char other[4] = {0};
	mode_t mode = 0777 & ~mask;

	umask_modestring(user, (mode & 0700) >> 6);
	umask_modestring(group, (mode & 0070) >> 3);
	umask_modestring(other, (mode & 0007));

	printf("u=%s,g=%s,o=%s\n", user, group, other);
}

int builtin_umask(struct mrsh_state *state, int argc, char *argv[]) {
	mode_t mode;
	bool umask_symbolic = false;

	optind = 0;
	int opt;

	while ((opt = getopt(argc, argv, ":S")) != -1) {
		switch (opt) {
		case 'S':
			umask_symbolic = true;
			break;

		default:
			fprintf(stderr, "Unknown option -- '%c'\n", optopt);
			fprintf(stderr, umask_usage);
			return EXIT_FAILURE;
		}
	}

	if (optind == argc) {
		mode = umask_current_mask();

		if (umask_symbolic) {
			umask_print_symbolic(mode);
		} else {
			printf("%04o\n", mode);
		}

		return EXIT_SUCCESS;
	}

	char *endptr;
	mode = strtol(argv[optind], &endptr, 8);

	if (*endptr != '\0') {
		if (!umask_mode(&mode, argv[optind])) {
			fprintf(stderr, umask_usage);
			return EXIT_FAILURE;
		}
	}

	umask(mode);
	return EXIT_SUCCESS;
}