xref: /original-bsd/usr.bin/uname/uname.c (revision a423a4a9)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)uname.c	8.2 (Berkeley) 05/04/95";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/sysctl.h>
20 
21 #include <err.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 
26 void usage __P((void));
27 
28 int
main(argc,argv)29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 #define	MFLAG	0x01
34 #define	NFLAG	0x02
35 #define	RFLAG	0x04
36 #define	SFLAG	0x08
37 #define	VFLAG	0x10
38 	u_int flags;
39 	int ch, mib[2];
40 	size_t len, tlen;
41 	char *p, *prefix, buf[1024];
42 
43 	flags = 0;
44 	while ((ch = getopt(argc, argv, "amnrsv")) != EOF)
45 		switch(ch) {
46 		case 'a':
47 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
48 			break;
49 		case 'm':
50 			flags |= MFLAG;
51 			break;
52 		case 'n':
53 			flags |= NFLAG;
54 			break;
55 		case 'r':
56 			flags |= RFLAG;
57 			break;
58 		case 's':
59 			flags |= SFLAG;
60 			break;
61 		case 'v':
62 			flags |= VFLAG;
63 			break;
64 		case '?':
65 		default:
66 			usage();
67 		}
68 
69 	argc -= optind;
70 	argv += optind;
71 
72 	if (argc)
73 		usage();
74 
75 	if (!flags)
76 		flags |= SFLAG;
77 
78 	prefix = "";
79 
80 	if (flags & SFLAG) {
81 		mib[0] = CTL_KERN;
82 		mib[1] = KERN_OSTYPE;
83 		len = sizeof(buf);
84 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
85 			err(1, "sysctl");
86 		(void)printf("%s%.*s", prefix, len, buf);
87 		prefix = " ";
88 	}
89 	if (flags & NFLAG) {
90 		mib[0] = CTL_KERN;
91 		mib[1] = KERN_HOSTNAME;
92 		len = sizeof(buf);
93 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
94 			err(1, "sysctl");
95 		(void)printf("%s%.*s", prefix, len, buf);
96 		prefix = " ";
97 	}
98 	if (flags & RFLAG) {
99 		mib[0] = CTL_KERN;
100 		mib[1] = KERN_OSRELEASE;
101 		len = sizeof(buf);
102 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
103 			err(1, "sysctl");
104 		(void)printf("%s%.*s", prefix, len, buf);
105 		prefix = " ";
106 	}
107 	if (flags & VFLAG) {
108 		mib[0] = CTL_KERN;
109 		mib[1] = KERN_VERSION;
110 		len = sizeof(buf);
111 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
112 			err(1, "sysctl");
113 		for (p = buf, tlen = len; tlen--; ++p)
114 			if (*p == '\n' || *p == '\t')
115 				*p = ' ';
116 		(void)printf("%s%.*s", prefix, len, buf);
117 		prefix = " ";
118 	}
119 	if (flags & MFLAG) {
120 		mib[0] = CTL_HW;
121 		mib[1] = HW_MACHINE;
122 		len = sizeof(buf);
123 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
124 			err(1, "sysctl");
125 		(void)printf("%s%.*s", prefix, len, buf);
126 		prefix = " ";
127 	}
128 	(void)printf("\n");
129 	exit (0);
130 }
131 
132 void
usage()133 usage()
134 {
135 	(void)fprintf(stderr, "usage: uname [-amnrsv]\n");
136 	exit(1);
137 }
138