Annotation of src/usr.bin/getcap/getcap.c, Revision 1.1
1.1 ! millert 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2005 Todd C. Miller <Todd.Miller@courtesan.com>
! 5: *
! 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 17: */
! 18:
! 19: #ifndef lint
! 20: static const char rcsid[] = "$OpenBSD$";
! 21: #endif /* not lint */
! 22:
! 23: #include <err.h>
! 24: #include <stdio.h>
! 25: #include <stdlib.h>
! 26: #include <string.h>
! 27:
! 28: enum captype {
! 29: boolean,
! 30: number,
! 31: string,
! 32: raw
! 33: };
! 34:
! 35: void lookup_cap(char *, char *, enum captype, int);
! 36: __dead void usage(void);
! 37:
! 38: int
! 39: main(int argc, char *argv[])
! 40: {
! 41: int ch, aflag;
! 42: enum captype type;
! 43: char *cp, *buf, *cap = NULL, **pathvec = NULL;
! 44: size_t n;
! 45:
! 46: aflag = type = 0;
! 47: while ((ch = getopt(argc, argv, "ab:c:f:n:s:")) != -1) {
! 48: switch (ch) {
! 49: case 'a':
! 50: aflag = 1;
! 51: break;
! 52: case 'b':
! 53: if (*optarg == '\0')
! 54: usage();
! 55: cap = optarg;
! 56: type = boolean;
! 57: break;
! 58: case 'n':
! 59: if (*optarg == '\0')
! 60: usage();
! 61: cap = optarg;
! 62: type = number;
! 63: break;
! 64: case 's':
! 65: if (*optarg == '\0')
! 66: usage();
! 67: cap = optarg;
! 68: type = string;
! 69: break;
! 70: case 'c':
! 71: if (*optarg == '\0')
! 72: usage();
! 73: cap = optarg;
! 74: type = raw;
! 75: break;
! 76: case 'f':
! 77: if (pathvec != NULL)
! 78: errx(1, "only one -f option may be specified");
! 79: for (n = 1, cp = optarg; (cp = strchr(cp, ':')); n++)
! 80: continue;
! 81: pathvec = calloc(n + 1, sizeof(char *));
! 82: for (n = 0; (pathvec[n] = strsep(&optarg, ":"));) {
! 83: if (*pathvec[n] != '\0')
! 84: n++;
! 85: }
! 86: break;
! 87: default:
! 88: usage();
! 89: }
! 90: }
! 91: argc -= optind;
! 92: argv += optind;
! 93:
! 94: if (pathvec == NULL) {
! 95: warnx("no path specified");
! 96: usage();
! 97: }
! 98: if (!aflag && !argc) {
! 99: warnx("must specify -a or a record name");
! 100: usage();
! 101: }
! 102:
! 103: if (aflag) {
! 104: while (cgetnext(&buf, pathvec) > 0) {
! 105: lookup_cap(buf, cap, type, 1);
! 106: free(buf);
! 107: }
! 108: } else {
! 109: while (*argv != NULL) {
! 110: if (cgetent(&buf, pathvec, *argv) != 0)
! 111: errx(1, "unable to lookup %s", *argv); /* XXX */
! 112: lookup_cap(buf, cap, type, argc > 1);
! 113: free(buf);
! 114: argv++;
! 115: }
! 116: }
! 117: exit(0);
! 118: }
! 119:
! 120: void
! 121: lookup_cap(char *buf, char *cap, enum captype type, int useprefix)
! 122: {
! 123: char *cp, *endp;
! 124: long l;
! 125: int ch, n, prefixlen;
! 126:
! 127: if (cap == NULL) {
! 128: puts(buf);
! 129: return;
! 130: }
! 131:
! 132: prefixlen = useprefix ? strcspn(buf, "|:") : 0;
! 133:
! 134: switch (type) {
! 135: case boolean:
! 136: if (cgetcap(buf, cap, ':') == NULL)
! 137: return;
! 138: printf("%.*s%s%s\n", prefixlen, buf,
! 139: useprefix ? ": " : "", cap);
! 140: break;
! 141: case number:
! 142: if (cgetnum(buf, cap, &l) == -1)
! 143: return;
! 144: printf("%.*s%s%ld\n", prefixlen, buf,
! 145: useprefix ? ": " : "", l);
! 146: break;
! 147: case string:
! 148: if ((n = cgetstr(buf, cap, &cp)) == -1)
! 149: return;
! 150: else if (n == -2)
! 151: err(1, NULL); /* ENOMEM */
! 152: if (cgetstr(buf, cap, &cp) < 0)
! 153: return;
! 154: printf("%.*s%s%s\n", prefixlen, buf,
! 155: useprefix ? ": " : "", cp);
! 156: break;
! 157: case raw:
! 158: n = strlen(cap) - 1;
! 159: ch = cap[n];
! 160: cap[n] = '\0';
! 161: cp = cgetcap(buf, cap, ch);
! 162: cap[n] = ch;
! 163: if (cp != NULL) {
! 164: if ((endp = strchr(cp, ':')) != NULL)
! 165: printf("%.*s%s%.*s\n", prefixlen, buf,
! 166: useprefix ? ": " : "", endp - cp, cp);
! 167: else
! 168: printf("%.*s%s%s\n", prefixlen, buf,
! 169: useprefix ? ": " : "", cp);
! 170: }
! 171: break;
! 172: }
! 173: }
! 174:
! 175: __dead void
! 176: usage(void)
! 177: {
! 178: extern char *__progname;
! 179:
! 180: fprintf(stderr, "usage: %s [-b boolean | -c capability | -n number | -s string] -a -f path\n"
! 181: " %s [-b boolean | -c capability | -n number | -s string] -f path record ...\n",
! 182: __progname, __progname);
! 183: exit(1);
! 184: }