~emersion/mrsh

ref: bda92abbcf206e6374593ee7e1962ba6341be98e mrsh/builtin/unset.c -rw-r--r-- 1.2 KiB View raw
bda92abbemersion Fix error handling in basic frontend 1 year, 4 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
#define _POSIX_C_SOURCE 200809L
#include <mrsh/builtin.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "builtin.h"

static const char unset_usage[] = "usage: unset [-fv] name...\n";

int builtin_unset(struct mrsh_state *state, int argc, char *argv[]) {
	bool funcs = false;

	optind = 1;
	int opt;
	while ((opt = getopt(argc, argv, ":fv")) != -1) {
		switch (opt) {
		case 'f':
			funcs = true;
			break;
		case 'v':
			funcs = false;
			break;
		default:
			fprintf(stderr, "unset: unknown option -- %c\n", optopt);
			fprintf(stderr, unset_usage);
			return EXIT_FAILURE;
		}
	}
	if (optind >= argc) {
		fprintf(stderr, unset_usage);
		return EXIT_FAILURE;
	}
	for (int i = optind; i < argc; ++i) {
		if (!funcs) {
			uint32_t prev_attribs = 0;
			if (mrsh_env_get(state, argv[i], &prev_attribs)) {
				if ((prev_attribs & MRSH_VAR_ATTRIB_READONLY)) {
					fprintf(stderr,
						"unset: cannot modify readonly variable %s\n", argv[i]);
					return EXIT_FAILURE;
				}
				mrsh_env_unset(state, argv[i]);
			}
		} else {
			struct mrsh_function *oldfn =
				mrsh_hashtable_del(&state->functions, argv[i]);
			mrsh_function_destroy(oldfn);
		}
	}
	return EXIT_SUCCESS;
}