Annotation of src/usr.bin/uname/uname.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: uname.c,v 1.14 2015/07/21 16:22:59 jasper Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1994 Winning Strategies, Inc.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by Winning Strategies, Inc.
1.14 jasper 18: * 4. The name of Winning Strategies, Inc. may not be used to endorse or
1.1 deraadt 19: * promote products derived from this software without specific prior
20: * written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
1.13 deraadt 34: #include <sys/param.h> /* MACHINE_ARCH */
1.1 deraadt 35: #include <stdio.h>
1.8 david 36: #include <stdlib.h>
1.1 deraadt 37: #include <locale.h>
38: #include <unistd.h>
39: #include <sys/utsname.h>
40: #include <err.h>
41:
1.7 deraadt 42: static void usage(void);
1.1 deraadt 43:
1.12 ajacouto 44: #define PRINT_SYSNAME 0x01
45: #define PRINT_NODENAME 0x02
46: #define PRINT_RELEASE 0x04
47: #define PRINT_VERSION 0x08
48: #define PRINT_MACHINE 0x10
49: #define PRINT_ALL 0x1f
50: #define PRINT_MACHINE_ARCH 0x20
1.1 deraadt 51:
52: int
1.6 deraadt 53: main(int argc, char *argv[])
1.1 deraadt 54: {
55: struct utsname u;
56: int c;
57: int space = 0;
58: int print_mask = 0;
59:
60: setlocale(LC_ALL, "");
1.15 ! deraadt 61:
! 62: if (tame("stdio", NULL) == -1)
! 63: err(1, "tame");
1.1 deraadt 64:
1.10 deraadt 65: while ((c = getopt(argc, argv, "amnrsvp")) != -1 ) {
66: switch (c) {
1.1 deraadt 67: case 'a':
68: print_mask |= PRINT_ALL;
69: break;
70: case 'm':
71: print_mask |= PRINT_MACHINE;
72: break;
73: case 'n':
74: print_mask |= PRINT_NODENAME;
75: break;
1.14 jasper 76: case 'r':
1.1 deraadt 77: print_mask |= PRINT_RELEASE;
78: break;
1.14 jasper 79: case 's':
1.1 deraadt 80: print_mask |= PRINT_SYSNAME;
81: break;
82: case 'v':
83: print_mask |= PRINT_VERSION;
84: break;
1.3 deraadt 85: case 'p':
1.12 ajacouto 86: print_mask |= PRINT_MACHINE_ARCH;
1.3 deraadt 87: break;
1.1 deraadt 88: default:
89: usage();
90: /* NOTREACHED */
91: }
92: }
1.14 jasper 93:
1.1 deraadt 94: if (optind != argc) {
95: usage();
96: /* NOTREACHED */
97: }
98:
99: if (!print_mask) {
100: print_mask = PRINT_SYSNAME;
101: }
102:
103: if (uname(&u)) {
104: err(1, NULL);
105: /* NOTREACHED */
106: }
107:
108: if (print_mask & PRINT_SYSNAME) {
109: space++;
110: fputs(u.sysname, stdout);
111: }
112: if (print_mask & PRINT_NODENAME) {
113: if (space++) putchar(' ');
114: fputs(u.nodename, stdout);
115: }
116: if (print_mask & PRINT_RELEASE) {
117: if (space++) putchar(' ');
118: fputs(u.release, stdout);
119: }
120: if (print_mask & PRINT_VERSION) {
121: if (space++) putchar(' ');
122: fputs(u.version, stdout);
123: }
124: if (print_mask & PRINT_MACHINE) {
125: if (space++) putchar(' ');
126: fputs(u.machine, stdout);
127: }
1.12 ajacouto 128: if (print_mask & PRINT_MACHINE_ARCH) {
1.3 deraadt 129: if (space++) putchar(' ');
1.12 ajacouto 130: fputs(MACHINE_ARCH, stdout);
1.14 jasper 131: }
1.1 deraadt 132: putchar('\n');
133:
134: exit(0);
135: /* NOTREACHED */
136: }
137:
138: static void
1.6 deraadt 139: usage(void)
1.1 deraadt 140: {
1.4 deraadt 141: fprintf(stderr, "usage: uname [-amnprsv]\n");
1.1 deraadt 142: exit(1);
143: }