getconf.c (2036B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <errno.h> 3 #include <unistd.h> 4 #include <limits.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "util.h" 9 10 struct var { 11 const char *k; 12 long v; 13 }; 14 15 #include "getconf.h" 16 17 void 18 usage(void) 19 { 20 eprintf("usage: %s [-v spec] var [path]\n", argv0); 21 } 22 23 int 24 main(int argc, char *argv[]) 25 { 26 size_t len; 27 long res; 28 int i; 29 char *str; 30 31 ARGBEGIN { 32 case 'v': 33 /* ignore */ 34 EARGF(usage()); 35 break; 36 default: 37 usage(); 38 break; 39 } ARGEND 40 41 if (argc == 1) { 42 /* sysconf */ 43 for (i = 0; i < LEN(sysconf_l); i++) { 44 if (strcmp(argv[0], sysconf_l[i].k)) 45 continue; 46 errno = 0; 47 if ((res = sysconf(sysconf_l[i].v)) < 0) { 48 if (errno) 49 eprintf("sysconf %ld:", sysconf_l[i].v); 50 puts("undefined"); 51 } else { 52 printf("%ld\n", res); 53 } 54 return fshut(stdout, "<stdout>"); 55 } 56 /* confstr */ 57 for (i = 0; i < LEN(confstr_l); i++) { 58 if (strcmp(argv[0], confstr_l[i].k)) 59 continue; 60 errno = 0; 61 if (!(len = confstr(confstr_l[i].v, NULL, 0))) { 62 if (errno) 63 eprintf("confstr %ld:", confstr_l[i].v); 64 puts("undefined"); 65 } else { 66 str = emalloc(len); 67 errno = 0; 68 if (!confstr(confstr_l[i].v, str, len)) { 69 if (errno) 70 eprintf("confstr %ld:", confstr_l[i].v); 71 puts("undefined"); 72 } else { 73 puts(str); 74 } 75 free(str); 76 } 77 return fshut(stdout, "<stdout>"); 78 } 79 /* limits */ 80 for (i = 0; i < LEN(limits_l); i++) { 81 if (strcmp(argv[0], limits_l[i].k)) 82 continue; 83 printf("%ld\n", limits_l[i].v); 84 return fshut(stdout, "<stdout>"); 85 } 86 } else if (argc == 2) { 87 /* pathconf */ 88 for (i = 0; i < LEN(pathconf_l); i++) { 89 if (strcmp(argv[0], pathconf_l[i].k)) 90 continue; 91 errno = 0; 92 if ((res = pathconf(argv[1], pathconf_l[i].v)) < 0) { 93 if (errno) 94 eprintf("pathconf %ld:", pathconf_l[i].v); 95 puts("undefined"); 96 } else { 97 printf("%ld\n", res); 98 } 99 return fshut(stdout, "<stdout>"); 100 } 101 } else { 102 usage(); 103 } 104 105 eprintf("invalid variable: %s\n", argv[0]); 106 }