summaryrefslogtreecommitdiff
path: root/usr.bin/getcap/getcap.c
blob: 4e90e0ec9ad87b54bb86f4c77d0559dc838da168 (plain)
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
/*	$OpenBSD: getcap.c,v 1.1 2005/02/19 22:15:41 millert Exp $	*/

/*
 * Copyright (c) 2005 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef lint
static const char rcsid[] = "$OpenBSD: getcap.c,v 1.1 2005/02/19 22:15:41 millert Exp $";
#endif /* not lint */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum captype {
	boolean,
	number,
	string,
	raw
};

void lookup_cap(char *, char *, enum captype, int);
__dead void usage(void);

int
main(int argc, char *argv[])
{
	int ch, aflag;
	enum captype type;
	char *cp, *buf, *cap = NULL, **pathvec = NULL;
	size_t n;

	aflag = type = 0;
	while ((ch = getopt(argc, argv, "ab:c:f:n:s:")) != -1) {
		switch (ch) {
		case 'a':
			aflag = 1;
			break;
		case 'b':
			if (*optarg == '\0')
				usage();
			cap = optarg;
			type = boolean;
			break;
		case 'n':
			if (*optarg == '\0')
				usage();
			cap = optarg;
			type = number;
			break;
		case 's':
			if (*optarg == '\0')
				usage();
			cap = optarg;
			type = string;
			break;
		case 'c':
			if (*optarg == '\0')
				usage();
			cap = optarg;
			type = raw;
			break;
		case 'f':
			if (pathvec != NULL)
				errx(1, "only one -f option may be specified");
			for (n = 1, cp = optarg; (cp = strchr(cp, ':')); n++)
				continue;
			pathvec = calloc(n + 1, sizeof(char *));
			for (n = 0; (pathvec[n] = strsep(&optarg, ":"));) {
				if (*pathvec[n] != '\0')
					n++;
			}
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (pathvec == NULL) {
		warnx("no path specified");
		usage();
	}
	if (!aflag && !argc) {
		warnx("must specify -a or a record name");
		usage();
	}

	if (aflag) {
		while (cgetnext(&buf, pathvec) > 0) {
			lookup_cap(buf, cap, type, 1);
			free(buf);
		}
	} else {
		while (*argv != NULL) {
		    if (cgetent(&buf, pathvec, *argv) != 0)
			    errx(1, "unable to lookup %s", *argv); /* XXX */
		    lookup_cap(buf, cap, type, argc > 1);
		    free(buf);
		    argv++;
		}
	}
	exit(0);
}

void
lookup_cap(char *buf, char *cap, enum captype type, int useprefix)
{
	char *cp, *endp;
	long l;
	int ch, n, prefixlen;

	if (cap == NULL) {
		puts(buf);
		return;
	}

	prefixlen = useprefix ? strcspn(buf, "|:") : 0;

	switch (type) {
	case boolean:
		if (cgetcap(buf, cap, ':') == NULL)
			return;
		printf("%.*s%s%s\n", prefixlen, buf,
		    useprefix ? ": " : "", cap);
		break;
	case number:
		if (cgetnum(buf, cap, &l) == -1)
			return;
		printf("%.*s%s%ld\n", prefixlen, buf,
		    useprefix ? ": " : "", l);
		break;
	case string:
		if ((n = cgetstr(buf, cap, &cp)) == -1)
			return;
		else if (n == -2)
			err(1, NULL);	/* ENOMEM */
		if (cgetstr(buf, cap, &cp) < 0)
			return;
		printf("%.*s%s%s\n", prefixlen, buf,
		    useprefix ? ": " : "", cp);
		break;
	case raw:
		n = strlen(cap) - 1;
		ch = cap[n];
		cap[n] = '\0';
		cp = cgetcap(buf, cap, ch);
		cap[n] = ch;
		if (cp != NULL) {
			if ((endp = strchr(cp, ':')) != NULL)
				printf("%.*s%s%.*s\n", prefixlen, buf,
				    useprefix ? ": " : "", endp - cp, cp);
			else
				printf("%.*s%s%s\n", prefixlen, buf,
				    useprefix ? ": " : "", cp);
		}
		break;
	}
}

__dead void
usage(void)
{
	extern char *__progname;

	fprintf(stderr, "usage: %s [-b boolean | -c capability | -n number | -s string] -a -f path\n"
	    "       %s [-b boolean | -c capability | -n number | -s string] -f path record ...\n",
	    __progname, __progname);
	exit(1);
}