121c1c48aSSascha Wildner /*
221c1c48aSSascha Wildner  * system.c
321c1c48aSSascha Wildner  * System definitions and capabilities.
421c1c48aSSascha Wildner  * $Id: system.c,v 1.5 2004/11/14 02:45:51 cpressey Exp $
521c1c48aSSascha Wildner  */
621c1c48aSSascha Wildner 
721c1c48aSSascha Wildner #include <sys/param.h>
821c1c48aSSascha Wildner #include <sys/sysctl.h>
921c1c48aSSascha Wildner 
1021c1c48aSSascha Wildner #include <err.h>
1121c1c48aSSascha Wildner #include <stdio.h>
1221c1c48aSSascha Wildner #include <string.h>
1321c1c48aSSascha Wildner #include <sysexits.h>
1421c1c48aSSascha Wildner 
1521c1c48aSSascha Wildner #include "dfui.h"
1621c1c48aSSascha Wildner #include "system.h"
1721c1c48aSSascha Wildner 
1821c1c48aSSascha Wildner char *
ostype(void)19a9879eb2SSascha Wildner ostype(void)
2021c1c48aSSascha Wildner {
2121c1c48aSSascha Wildner 	int mib[2];
2221c1c48aSSascha Wildner 	size_t len;
2321c1c48aSSascha Wildner 	char *p;
2421c1c48aSSascha Wildner 
2521c1c48aSSascha Wildner 	mib[0] = CTL_KERN;
2621c1c48aSSascha Wildner 	mib[1] = KERN_OSTYPE;
2721c1c48aSSascha Wildner 	sysctl(mib, 2, NULL, &len, NULL, 0);
2821c1c48aSSascha Wildner 	p = malloc(len);
2921c1c48aSSascha Wildner 	sysctl(mib, 2, p, &len, NULL, 0);
3021c1c48aSSascha Wildner 	return p;
3121c1c48aSSascha Wildner }
3221c1c48aSSascha Wildner 
3321c1c48aSSascha Wildner int
has_npipe(void)34a9879eb2SSascha Wildner has_npipe(void)
3521c1c48aSSascha Wildner {
3621c1c48aSSascha Wildner #ifdef HAS_NPIPE
3721c1c48aSSascha Wildner 	return 1;
3821c1c48aSSascha Wildner #else
3921c1c48aSSascha Wildner 	return 0;
4021c1c48aSSascha Wildner #endif
4121c1c48aSSascha Wildner }
4221c1c48aSSascha Wildner 
4321c1c48aSSascha Wildner int
has_tcp(void)44a9879eb2SSascha Wildner has_tcp(void)
4521c1c48aSSascha Wildner {
4621c1c48aSSascha Wildner #ifdef HAS_TCP
4721c1c48aSSascha Wildner 	return 1;
4821c1c48aSSascha Wildner #else
4921c1c48aSSascha Wildner 	return 0;
5021c1c48aSSascha Wildner #endif
5121c1c48aSSascha Wildner }
5221c1c48aSSascha Wildner 
5321c1c48aSSascha Wildner /*
5421c1c48aSSascha Wildner  * Get transport from transport name.
5521c1c48aSSascha Wildner  *
5621c1c48aSSascha Wildner  * return(0) if transport is not supported.
5721c1c48aSSascha Wildner  * retirn(-1) if transport unknown.
5821c1c48aSSascha Wildner  */
5921c1c48aSSascha Wildner int
get_transport(const char * transport_name)6021c1c48aSSascha Wildner get_transport(const char *transport_name)
6121c1c48aSSascha Wildner {
62*8f8e1dafSSascha Wildner 	if (strcmp(transport_name, "npipe") == 0) {
6321c1c48aSSascha Wildner 		if (has_npipe())
6421c1c48aSSascha Wildner 			return DFUI_TRANSPORT_NPIPE;
6521c1c48aSSascha Wildner 		return(0);
6621c1c48aSSascha Wildner 	} else if (strcmp(transport_name, "tcp") == 0) {
6721c1c48aSSascha Wildner 		if (has_tcp())
6821c1c48aSSascha Wildner 			return DFUI_TRANSPORT_TCP;
6921c1c48aSSascha Wildner 		return(0);
7021c1c48aSSascha Wildner 	}
7121c1c48aSSascha Wildner 	return(-1);
7221c1c48aSSascha Wildner }
7321c1c48aSSascha Wildner 
7421c1c48aSSascha Wildner /*
7521c1c48aSSascha Wildner  * Get transport upon user request
7621c1c48aSSascha Wildner  *
7721c1c48aSSascha Wildner  * Print appropriate error message to stderr
7821c1c48aSSascha Wildner  * and exit if transport not supported or unknown.
7921c1c48aSSascha Wildner  */
8021c1c48aSSascha Wildner int
user_get_transport(const char * transport_name)8121c1c48aSSascha Wildner user_get_transport(const char *transport_name)
8221c1c48aSSascha Wildner {
8321c1c48aSSascha Wildner 	int transport;
8421c1c48aSSascha Wildner 
8521c1c48aSSascha Wildner 	transport = get_transport(transport_name);
8621c1c48aSSascha Wildner 
8721c1c48aSSascha Wildner 	if (transport == 0) {
8821c1c48aSSascha Wildner 		errx(EX_UNAVAILABLE, "Transport is not supported: ``%s''.\n",
8921c1c48aSSascha Wildner 		    transport_name);
9021c1c48aSSascha Wildner 	} else if (transport < 0) {
9121c1c48aSSascha Wildner 		errx(EX_CONFIG, "Wrong transport name: ``%s''.\n",
9221c1c48aSSascha Wildner 		    transport_name);
9321c1c48aSSascha Wildner 	}
9421c1c48aSSascha Wildner 
9521c1c48aSSascha Wildner 	return(transport);
9621c1c48aSSascha Wildner }
97