Annotation of src/usr.bin/uname/uname.c, Revision 1.19
1.19 ! schwarze 1: /* $OpenBSD: uname.c,v 1.18 2016/10/10 02:23:54 gsoares 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.17 tb 35: #include <sys/utsname.h>
36:
37: #include <err.h>
1.1 deraadt 38: #include <stdio.h>
1.8 david 39: #include <stdlib.h>
1.1 deraadt 40: #include <unistd.h>
41:
1.19 ! schwarze 42: static void __dead 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:
1.16 deraadt 60: if (pledge("stdio", NULL) == -1)
61: err(1, "pledge");
1.1 deraadt 62:
1.10 deraadt 63: while ((c = getopt(argc, argv, "amnrsvp")) != -1 ) {
64: switch (c) {
1.1 deraadt 65: case 'a':
66: print_mask |= PRINT_ALL;
67: break;
68: case 'm':
69: print_mask |= PRINT_MACHINE;
70: break;
71: case 'n':
72: print_mask |= PRINT_NODENAME;
73: break;
1.17 tb 74: case 'p':
75: print_mask |= PRINT_MACHINE_ARCH;
76: break;
1.14 jasper 77: case 'r':
1.1 deraadt 78: print_mask |= PRINT_RELEASE;
79: break;
1.14 jasper 80: case 's':
1.1 deraadt 81: print_mask |= PRINT_SYSNAME;
82: break;
83: case 'v':
84: print_mask |= PRINT_VERSION;
85: break;
86: default:
87: usage();
88: }
89: }
1.14 jasper 90:
1.19 ! schwarze 91: if (optind != argc)
1.1 deraadt 92: usage();
93:
1.19 ! schwarze 94: if (!print_mask)
1.1 deraadt 95: print_mask = PRINT_SYSNAME;
96:
1.18 gsoares 97: if (uname(&u) == -1)
1.1 deraadt 98: err(1, NULL);
99:
100: if (print_mask & PRINT_SYSNAME) {
101: space++;
102: fputs(u.sysname, stdout);
103: }
104: if (print_mask & PRINT_NODENAME) {
1.17 tb 105: if (space++)
106: putchar(' ');
107:
1.1 deraadt 108: fputs(u.nodename, stdout);
109: }
110: if (print_mask & PRINT_RELEASE) {
1.17 tb 111: if (space++)
112: putchar(' ');
113:
1.1 deraadt 114: fputs(u.release, stdout);
115: }
116: if (print_mask & PRINT_VERSION) {
1.17 tb 117: if (space++)
118: putchar(' ');
119:
1.1 deraadt 120: fputs(u.version, stdout);
121: }
122: if (print_mask & PRINT_MACHINE) {
1.17 tb 123: if (space++)
124: putchar(' ');
125:
1.1 deraadt 126: fputs(u.machine, stdout);
127: }
1.12 ajacouto 128: if (print_mask & PRINT_MACHINE_ARCH) {
1.17 tb 129: if (space++)
130: putchar(' ');
131:
1.12 ajacouto 132: fputs(MACHINE_ARCH, stdout);
1.14 jasper 133: }
1.1 deraadt 134: putchar('\n');
135:
1.17 tb 136: return 0;
1.1 deraadt 137: }
138:
1.19 ! schwarze 139: static void __dead
1.6 deraadt 140: usage(void)
1.1 deraadt 141: {
1.4 deraadt 142: fprintf(stderr, "usage: uname [-amnprsv]\n");
1.1 deraadt 143: exit(1);
144: }