1 /*
2  * system.c
3  * System definitions and capabilities.
4  * $Id: system.c,v 1.5 2004/11/14 02:45:51 cpressey Exp $
5  */
6 
7 #include <sys/param.h>
8 #include <sys/sysctl.h>
9 
10 #include <err.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sysexits.h>
14 
15 #include "dfui.h"
16 #include "system.h"
17 
18 char *
19 ostype(void)
20 {
21 	int mib[2];
22 	size_t len;
23 	char *p;
24 
25 	mib[0] = CTL_KERN;
26 	mib[1] = KERN_OSTYPE;
27 	sysctl(mib, 2, NULL, &len, NULL, 0);
28 	p = malloc(len);
29 	sysctl(mib, 2, p, &len, NULL, 0);
30 	return p;
31 }
32 
33 int
34 has_npipe(void)
35 {
36 #ifdef HAS_NPIPE
37 	return 1;
38 #else
39 	return 0;
40 #endif
41 }
42 
43 int
44 has_tcp(void)
45 {
46 #ifdef HAS_TCP
47 	return 1;
48 #else
49 	return 0;
50 #endif
51 }
52 
53 /*
54  * Get transport from transport name.
55  *
56  * return(0) if transport is not supported.
57  * retirn(-1) if transport unknown.
58  */
59 int
60 get_transport(const char *transport_name)
61 {
62 	if (strcmp(transport_name, "npipe") == 0) {
63 		if (has_npipe())
64 			return DFUI_TRANSPORT_NPIPE;
65 		return(0);
66 	} else if (strcmp(transport_name, "tcp") == 0) {
67 		if (has_tcp())
68 			return DFUI_TRANSPORT_TCP;
69 		return(0);
70 	}
71 	return(-1);
72 }
73 
74 /*
75  * Get transport upon user request
76  *
77  * Print appropriate error message to stderr
78  * and exit if transport not supported or unknown.
79  */
80 int
81 user_get_transport(const char *transport_name)
82 {
83 	int transport;
84 
85 	transport = get_transport(transport_name);
86 
87 	if (transport == 0) {
88 		errx(EX_UNAVAILABLE, "Transport is not supported: ``%s''.\n",
89 		    transport_name);
90 	} else if (transport < 0) {
91 		errx(EX_CONFIG, "Wrong transport name: ``%s''.\n",
92 		    transport_name);
93 	}
94 
95 	return(transport);
96 }
97