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_caps(void)
35 {
36 #ifdef HAS_CAPS
37 	return 1;
38 #endif
39 	return 0;
40 }
41 
42 int
43 has_npipe(void)
44 {
45 #ifdef HAS_NPIPE
46 	return 1;
47 #else
48 	return 0;
49 #endif
50 }
51 
52 int
53 has_tcp(void)
54 {
55 #ifdef HAS_TCP
56 	return 1;
57 #else
58 	return 0;
59 #endif
60 }
61 
62 /*
63  * Get transport from transport name.
64  *
65  * return(0) if transport is not supported.
66  * retirn(-1) if transport unknown.
67  */
68 int
69 get_transport(const char *transport_name)
70 {
71 	if (strcmp(transport_name, "caps") == 0) {
72 		if (has_caps())
73 			return DFUI_TRANSPORT_CAPS;
74 		return(0);
75 	} else if (strcmp(transport_name, "npipe") == 0) {
76 		if (has_npipe())
77 			return DFUI_TRANSPORT_NPIPE;
78 		return(0);
79 	} else if (strcmp(transport_name, "tcp") == 0) {
80 		if (has_tcp())
81 			return DFUI_TRANSPORT_TCP;
82 		return(0);
83 	}
84 	return(-1);
85 }
86 
87 /*
88  * Get transport upon user request
89  *
90  * Print appropriate error message to stderr
91  * and exit if transport not supported or unknown.
92  */
93 int
94 user_get_transport(const char *transport_name)
95 {
96 	int transport;
97 
98 	transport = get_transport(transport_name);
99 
100 	if (transport == 0) {
101 		errx(EX_UNAVAILABLE, "Transport is not supported: ``%s''.\n",
102 		    transport_name);
103 	} else if (transport < 0) {
104 		errx(EX_CONFIG, "Wrong transport name: ``%s''.\n",
105 		    transport_name);
106 	}
107 
108 	return(transport);
109 }
110