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