xref: /minix3/minix/commands/sysenv/sysenv.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 /*	sysenv 1.0 - request system boot parameter	Author: Kees J. Bot
2  *								23 Dec 2000
3  */
4 #define nil ((void*)0)
5 #include <minix/type.h>
6 #include <sys/types.h>
7 #include <sys/svrctl.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <string.h>
13 
14 #define NIL ((char*)0)
15 
tell(int fd,...)16 static void tell(int fd, ...)
17 {
18     va_list ap;
19     char *s;
20 
21     va_start(ap, fd);
22     while ((s= va_arg(ap, char *)) != NIL) {
23 	(void) write(fd, s, strlen(s));
24     }
25     va_end(ap);
26 }
27 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30     struct sysgetenv sysgetenv;
31     int i;
32     int ex= 0;
33     char *e;
34     char val[1024];
35 
36     i= 1;
37     while (i < argc && argv[i][0] == '-') {
38 	char *opt= argv[i++]+1;
39 
40 	if (opt[0] == '-' && opt[1] == 0) break;	/* -- */
41 
42 	if (*opt != 0) {
43 	    tell(2, "Usage: sysenv [name ...]\n", NIL);
44 	    exit(1);
45 	}
46     }
47 
48     do {
49 	if (i < argc) {
50 	    sysgetenv.key= argv[i];
51 	    sysgetenv.keylen= strlen(sysgetenv.key) + 1;
52 	} else {
53 	    sysgetenv.key= nil;
54 	    sysgetenv.keylen= 0;
55 	}
56 	sysgetenv.val= val;
57 	sysgetenv.vallen= sizeof(val);
58 
59 	if (svrctl(PMGETPARAM, &sysgetenv) == -1) {
60 	    if (errno == ESRCH) {
61 		ex |= 2;
62 	    } else {
63 		ex |= 1;
64 		tell(2, "sysenv: ", strerror(errno), "\n", NIL);
65 	    }
66 	    continue;
67 	}
68 
69 	e= sysgetenv.val;
70 	do {
71 	    e += strlen(e);
72 	    *e++ = '\n';
73 	} while (i == argc && *e != 0);
74 
75 	if (write(1, sysgetenv.val, e - sysgetenv.val) < 0) {
76 	    ex |= 1;
77 	    tell(2, "sysenv: ", strerror(errno), "\n", NIL);
78 	}
79     } while (++i < argc);
80     return ex;
81 }
82