1 /* $NetBSD: uname.c,v 1.10 1998/11/09 13:24:05 kleink Exp $ */ 2 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. 18 * 4. The name of Winning Strategies, Inc. may not be used to endorse or 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 34 #include <sys/cdefs.h> 35 #ifndef lint 36 __RCSID("$NetBSD: uname.c,v 1.10 1998/11/09 13:24:05 kleink Exp $"); 37 #endif /* not lint */ 38 39 #include <sys/param.h> 40 #include <limits.h> 41 #include <locale.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 #include <err.h> 46 47 #include <sys/sysctl.h> 48 #include <sys/utsname.h> 49 50 int main __P((int, char **)); 51 static void usage __P((void)); 52 53 /* Note that PRINT_MACHINE_ARCH is excluded from PRINT_ALL! */ 54 #define PRINT_SYSNAME 0x01 55 #define PRINT_NODENAME 0x02 56 #define PRINT_RELEASE 0x04 57 #define PRINT_VERSION 0x08 58 #define PRINT_MACHINE 0x10 59 #define PRINT_MACHINE_ARCH 0x20 60 #define PRINT_ALL \ 61 (PRINT_SYSNAME|PRINT_NODENAME|PRINT_RELEASE|PRINT_VERSION|PRINT_MACHINE) 62 63 int 64 main(argc, argv) 65 int argc; 66 char **argv; 67 { 68 struct utsname u; 69 char machine_arch[SYS_NMLN]; 70 int c; 71 int space = 0; 72 int print_mask = 0; 73 74 (void)setlocale(LC_ALL, ""); 75 76 while ((c = getopt(argc,argv,"amnprsv")) != -1) { 77 switch (c) { 78 case 'a': 79 print_mask |= PRINT_ALL; 80 break; 81 case 'm': 82 print_mask |= PRINT_MACHINE; 83 break; 84 case 'n': 85 print_mask |= PRINT_NODENAME; 86 break; 87 case 'p': 88 print_mask |= PRINT_MACHINE_ARCH; 89 break; 90 case 'r': 91 print_mask |= PRINT_RELEASE; 92 break; 93 case 's': 94 print_mask |= PRINT_SYSNAME; 95 break; 96 case 'v': 97 print_mask |= PRINT_VERSION; 98 break; 99 default: 100 usage(); 101 /* NOTREACHED */ 102 } 103 } 104 105 if (optind != argc) { 106 usage(); 107 /* NOTREACHED */ 108 } 109 110 if (!print_mask) { 111 print_mask = PRINT_SYSNAME; 112 } 113 114 if (uname(&u) != 0) { 115 err(EXIT_FAILURE, "uname"); 116 /* NOTREACHED */ 117 } 118 if (print_mask & PRINT_MACHINE_ARCH) { 119 int mib[2] = { CTL_HW, HW_MACHINE_ARCH }; 120 size_t len = sizeof (machine_arch); 121 122 if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch, 123 &len, NULL, 0) < 0) 124 err(EXIT_FAILURE, "sysctl"); 125 } 126 127 if (print_mask & PRINT_SYSNAME) { 128 space++; 129 fputs(u.sysname, stdout); 130 } 131 if (print_mask & PRINT_NODENAME) { 132 if (space++) putchar(' '); 133 fputs(u.nodename, stdout); 134 } 135 if (print_mask & PRINT_RELEASE) { 136 if (space++) putchar(' '); 137 fputs(u.release, stdout); 138 } 139 if (print_mask & PRINT_VERSION) { 140 if (space++) putchar(' '); 141 fputs(u.version, stdout); 142 } 143 if (print_mask & PRINT_MACHINE) { 144 if (space++) putchar(' '); 145 fputs(u.machine, stdout); 146 } 147 if (print_mask & PRINT_MACHINE_ARCH) { 148 if (space++) putchar(' '); 149 fputs(machine_arch, stdout); 150 } 151 putchar('\n'); 152 153 exit(EXIT_SUCCESS); 154 /* NOTREACHED */ 155 } 156 157 static void 158 usage() 159 { 160 fprintf(stderr, "usage: uname [-amnprsv]\n"); 161 exit(EXIT_FAILURE); 162 } 163