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