summaryrefslogtreecommitdiff
path: root/minix/commands/sysenv/sysenv.c
blob: 58cb876c17ea5886e9853efd5d9e4e0ae535728d (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
/*	sysenv 1.0 - request system boot parameter	Author: Kees J. Bot
 *								23 Dec 2000
 */
#define nil ((void*)0)
#include <minix/type.h>
#include <sys/types.h>
#include <sys/svrctl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define NIL ((char*)0)

static void tell(int fd, ...)
{
    va_list ap;
    char *s;

    va_start(ap, fd);
    while ((s= va_arg(ap, char *)) != NIL) {
	(void) write(fd, s, strlen(s));
    }
    va_end(ap);
}

int main(int argc, char **argv)
{
    struct sysgetenv sysgetenv;
    int i;
    int ex= 0;
    char *e;
    char val[1024];

    i= 1;
    while (i < argc && argv[i][0] == '-') {
	char *opt= argv[i++]+1;

	if (opt[0] == '-' && opt[1] == 0) break;	/* -- */

	if (*opt != 0) {
	    tell(2, "Usage: sysenv [name ...]\n", NIL);
	    exit(1);
	}
    }

    do {
	if (i < argc) {
	    sysgetenv.key= argv[i];
	    sysgetenv.keylen= strlen(sysgetenv.key) + 1;
	} else {
	    sysgetenv.key= nil;
	    sysgetenv.keylen= 0;
	}
	sysgetenv.val= val;
	sysgetenv.vallen= sizeof(val);

	if (svrctl(PMGETPARAM, &sysgetenv) == -1) {
	    if (errno == ESRCH) {
		ex |= 2;
	    } else {
		ex |= 1;
		tell(2, "sysenv: ", strerror(errno), "\n", NIL);
	    }
	    continue;
	}

	e= sysgetenv.val;
	do {
	    e += strlen(e);
	    *e++ = '\n';
	} while (i == argc && *e != 0);

	if (write(1, sysgetenv.val, e - sysgetenv.val) < 0) {
	    ex |= 1;
	    tell(2, "sysenv: ", strerror(errno), "\n", NIL);
	}
    } while (++i < argc);
    return ex;
}