1 /*- 2 * Copyright (c) 2002 Juli Mallett. 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved. 35 * @(#)uname.c 8.2 (Berkeley) 5/4/95 36 * $FreeBSD: src/usr.bin/uname/uname.c,v 1.4.6.2 2002/10/17 07:47:29 jmallett Exp $ 37 * $DragonFly: src/usr.bin/uname/uname.c,v 1.6 2007/01/19 07:23:43 dillon Exp $ 38 */ 39 40 #include <sys/param.h> 41 #include <sys/sysctl.h> 42 43 #include <err.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 48 #define MFLAG 0x01 49 #define NFLAG 0x02 50 #define PFLAG 0x04 51 #define RFLAG 0x08 52 #define SFLAG 0x10 53 #define VFLAG 0x20 54 #define IFLAG 0x40 55 56 typedef void (*get_t)(void); 57 get_t get_ident, get_machine, get_hostname, get_arch, get_release, get_sysname, get_version; 58 59 void native_ident(void); 60 void native_machine(void); 61 void native_hostname(void); 62 void native_arch(void); 63 void native_release(void); 64 void native_sysname(void); 65 void native_version(void); 66 void print_uname(u_int); 67 void setup_get(void); 68 void usage(void); 69 70 char *ident, *machine, *hostname, *arch, *release, *sysname, *version; 71 int space; 72 73 int 74 main(int argc, char *argv[]) 75 { 76 u_int flags; 77 int ch; 78 79 setup_get(); 80 flags = 0; 81 82 while ((ch = getopt(argc, argv, "aimnprsv")) != -1) 83 switch(ch) { 84 case 'a': 85 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 86 break; 87 case 'i': 88 flags |= IFLAG; 89 break; 90 case 'm': 91 flags |= MFLAG; 92 break; 93 case 'n': 94 flags |= NFLAG; 95 break; 96 case 'p': 97 flags |= PFLAG; 98 break; 99 case 'r': 100 flags |= RFLAG; 101 break; 102 case 's': 103 flags |= SFLAG; 104 break; 105 case 'v': 106 flags |= VFLAG; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 113 argc -= optind; 114 argv += optind; 115 116 if (argc) 117 usage(); 118 119 if (!flags) 120 flags |= SFLAG; 121 122 print_uname(flags); 123 exit(0); 124 } 125 126 #define CHECK_ENV(opt,var) \ 127 do { \ 128 if ((var = getenv("UNAME_" opt)) == NULL) { \ 129 get_##var = native_##var; \ 130 } else { \ 131 get_##var = (get_t)NULL; \ 132 } \ 133 } while (0) 134 135 void 136 setup_get(void) 137 { 138 CHECK_ENV("s", sysname); 139 CHECK_ENV("n", hostname); 140 CHECK_ENV("r", release); 141 CHECK_ENV("v", version); 142 CHECK_ENV("m", machine); 143 CHECK_ENV("p", arch); 144 CHECK_ENV("i", ident); 145 } 146 147 #define PRINT_FLAG(flags,flag,var) \ 148 if ((flags & flag) == flag) { \ 149 if (space) \ 150 printf(" "); \ 151 else \ 152 space++; \ 153 if (get_##var != NULL) \ 154 (*get_##var)(); \ 155 printf("%s", var); \ 156 } 157 158 void 159 print_uname(u_int flags) 160 { 161 PRINT_FLAG(flags, SFLAG, sysname); 162 PRINT_FLAG(flags, NFLAG, hostname); 163 PRINT_FLAG(flags, RFLAG, release); 164 PRINT_FLAG(flags, VFLAG, version); 165 PRINT_FLAG(flags, MFLAG, machine); 166 PRINT_FLAG(flags, PFLAG, arch); 167 PRINT_FLAG(flags, IFLAG, ident); 168 printf("\n"); 169 } 170 171 #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \ 172 void \ 173 native_##var(void) \ 174 { \ 175 int mib[] = { (mib0), (mib1) }; \ 176 size_t len; \ 177 static char buf[1024]; \ 178 char **varp = &(var); \ 179 \ 180 len = sizeof buf; \ 181 if (sysctl(mib, sizeof mib / sizeof mib[0], \ 182 &buf, &len, NULL, 0) == -1) \ 183 err(1, "sysctl"); 184 185 #define NATIVE_SYSCTLNAME_GET(var,name) \ 186 void \ 187 native_##var(void) \ 188 { \ 189 size_t len; \ 190 static char buf[1024]; \ 191 char **varp = &(var); \ 192 \ 193 len = sizeof buf; \ 194 if (sysctlbyname(name, &buf, &len, NULL,\ 195 0) == -1) \ 196 err(1, "sysctlbyname"); 197 198 #define NATIVE_SET \ 199 *varp = buf; \ 200 return; \ 201 } struct __hack 202 203 #define NATIVE_BUFFER (buf) 204 #define NATIVE_LENGTH (len) 205 206 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) { 207 } NATIVE_SET; 208 209 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) { 210 } NATIVE_SET; 211 212 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) { 213 } NATIVE_SET; 214 215 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) { 216 size_t n; 217 char *p; 218 219 p = NATIVE_BUFFER; 220 n = NATIVE_LENGTH; 221 for (; n--; ++p) 222 if (*p == '\n' || *p == '\t') 223 *p = ' '; 224 } NATIVE_SET; 225 226 NATIVE_SYSCTL2_GET(machine, CTL_HW, HW_MACHINE) { 227 } NATIVE_SET; 228 229 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) { 230 } NATIVE_SET; 231 232 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") { 233 } NATIVE_SET; 234 235 void 236 usage(void) 237 { 238 fprintf(stderr, "usage: uname [-aimnprsv]\n"); 239 exit(1); 240 } 241