xref: /csrg-svn/lib/libc/gen/uname.c (revision 65422)
1*65422Sbostic /*-
2*65422Sbostic  * Copyright (c) 1994
3*65422Sbostic  *	The Regents of the University of California.  All rights reserved.
4*65422Sbostic  *
5*65422Sbostic  * %sccs.include.redist.c%
6*65422Sbostic  */
7*65422Sbostic 
8*65422Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*65422Sbostic static char sccsid[] = "@(#)uname.c	8.1 (Berkeley) 01/04/94";
10*65422Sbostic #endif /* LIBC_SCCS and not lint */
11*65422Sbostic 
12*65422Sbostic #include <sys/param.h>
13*65422Sbostic #include <sys/sysctl.h>
14*65422Sbostic #include <sys/utsname.h>
15*65422Sbostic 
16*65422Sbostic int
uname(name)17*65422Sbostic uname(name)
18*65422Sbostic 	struct utsname *name;
19*65422Sbostic {
20*65422Sbostic 	int mib[2], rval;
21*65422Sbostic 	size_t len;
22*65422Sbostic 	char *p;
23*65422Sbostic 
24*65422Sbostic 	rval = 0;
25*65422Sbostic 
26*65422Sbostic 	mib[0] = CTL_KERN;
27*65422Sbostic 	mib[1] = KERN_OSTYPE;
28*65422Sbostic 	len = sizeof(name->sysname);
29*65422Sbostic 	if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1)
30*65422Sbostic 		rval = -1;
31*65422Sbostic 
32*65422Sbostic 	mib[0] = CTL_KERN;
33*65422Sbostic 	mib[1] = KERN_HOSTNAME;
34*65422Sbostic 	len = sizeof(name->nodename);
35*65422Sbostic 	if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1)
36*65422Sbostic 		rval = -1;
37*65422Sbostic 
38*65422Sbostic 	mib[0] = CTL_KERN;
39*65422Sbostic 	mib[1] = KERN_OSRELEASE;
40*65422Sbostic 	len = sizeof(name->release);
41*65422Sbostic 	if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1)
42*65422Sbostic 		rval = -1;
43*65422Sbostic 
44*65422Sbostic 	/* The version may have newlines in it, turn them into spaces. */
45*65422Sbostic 	mib[0] = CTL_KERN;
46*65422Sbostic 	mib[1] = KERN_VERSION;
47*65422Sbostic 	len = sizeof(name->version);
48*65422Sbostic 	if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1)
49*65422Sbostic 		rval = -1;
50*65422Sbostic 	else
51*65422Sbostic 		for (p = name->version; len--; ++p)
52*65422Sbostic 			if (*p == '\n' || *p == '\t')
53*65422Sbostic 				if (len > 1)
54*65422Sbostic 					*p = ' ';
55*65422Sbostic 				else
56*65422Sbostic 					*p = '\0';
57*65422Sbostic 
58*65422Sbostic 	mib[0] = CTL_HW;
59*65422Sbostic 	mib[1] = HW_MACHINE;
60*65422Sbostic 	len = sizeof(name->machine);
61*65422Sbostic 	if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1)
62*65422Sbostic 		rval = -1;
63*65422Sbostic 	return (rval);
64*65422Sbostic }
65