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 35 #include <sys/cdefs.h> 36 37 __FBSDID("$FreeBSD$"); 38 39 #ifndef lint 40 static const char copyright[] = 41 "@(#) Copyright (c) 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"; 43 #endif 44 45 #ifndef lint 46 static const char sccsid[] = "@(#)uname.c 8.2 (Berkeley) 5/4/95"; 47 #endif 48 49 #include <sys/param.h> 50 #include <sys/sysctl.h> 51 52 #include <err.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <unistd.h> 56 57 #define MFLAG 0x01 58 #define NFLAG 0x02 59 #define PFLAG 0x04 60 #define RFLAG 0x08 61 #define SFLAG 0x10 62 #define VFLAG 0x20 63 64 typedef void (*get_t)(void); 65 get_t get_platform, get_hostname, get_arch, get_release, get_sysname, get_version; 66 67 void native_platform(void); 68 void native_hostname(void); 69 void native_arch(void); 70 void native_release(void); 71 void native_sysname(void); 72 void native_version(void); 73 void print_uname(u_int); 74 void setup_get(void); 75 void usage(void); 76 77 char *platform, *hostname, *arch, *release, *sysname, *version; 78 const char *prefix; 79 80 int 81 main(int argc, char *argv[]) 82 { 83 u_int flags; 84 int ch; 85 86 prefix = ""; 87 88 setup_get(); 89 90 flags = 0; 91 while ((ch = getopt(argc, argv, "amnprsv")) != -1) 92 switch(ch) { 93 case 'a': 94 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 95 break; 96 case 'm': 97 flags |= MFLAG; 98 break; 99 case 'n': 100 flags |= NFLAG; 101 break; 102 case 'p': 103 flags |= PFLAG; 104 break; 105 case 'r': 106 flags |= RFLAG; 107 break; 108 case 's': 109 flags |= SFLAG; 110 break; 111 case 'v': 112 flags |= VFLAG; 113 break; 114 case '?': 115 default: 116 usage(); 117 } 118 119 argc -= optind; 120 argv += optind; 121 122 if (argc) 123 usage(); 124 125 if (!flags) 126 flags |= SFLAG; 127 128 print_uname(flags); 129 exit(0); 130 } 131 132 #define CHECK_ENV(opt,var) \ 133 do { \ 134 if ((var = getenv("UNAME_" opt)) == NULL) { \ 135 get_##var = native_##var; \ 136 } else { \ 137 get_##var = (get_t)NULL; \ 138 } \ 139 } while (0) 140 141 void 142 setup_get(void) 143 { 144 CHECK_ENV("s", sysname); 145 CHECK_ENV("n", hostname); 146 CHECK_ENV("r", release); 147 CHECK_ENV("v", version); 148 CHECK_ENV("m", platform); 149 CHECK_ENV("p", arch); 150 } 151 152 #define PRINT_FLAG(flags,flag,var) \ 153 if ((flags & flag) == flag) { \ 154 if (get_##var != NULL) \ 155 (*get_##var)(); \ 156 printf("%s%s", prefix, var); \ 157 prefix = " "; \ 158 } 159 160 void 161 print_uname(u_int flags) 162 { 163 PRINT_FLAG(flags, SFLAG, sysname); 164 PRINT_FLAG(flags, NFLAG, hostname); 165 PRINT_FLAG(flags, RFLAG, release); 166 PRINT_FLAG(flags, VFLAG, version); 167 PRINT_FLAG(flags, MFLAG, platform); 168 PRINT_FLAG(flags, PFLAG, arch); 169 printf("\n"); 170 } 171 172 void 173 native_sysname(void) 174 { 175 int mib[2]; 176 size_t len; 177 static char buf[1024]; 178 179 mib[0] = CTL_KERN; 180 mib[1] = KERN_OSTYPE; 181 len = sizeof(buf); 182 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 183 err(1, "sysctl"); 184 sysname = buf; 185 } 186 187 void 188 native_hostname(void) 189 { 190 int mib[2]; 191 size_t len; 192 static char buf[1024]; 193 194 mib[0] = CTL_KERN; 195 mib[1] = KERN_HOSTNAME; 196 len = sizeof(buf); 197 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 198 err(1, "sysctl"); 199 hostname = buf; 200 } 201 202 void 203 native_release(void) 204 { 205 int mib[2]; 206 size_t len; 207 static char buf[1024]; 208 209 mib[0] = CTL_KERN; 210 mib[1] = KERN_OSRELEASE; 211 len = sizeof(buf); 212 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 213 err(1, "sysctl"); 214 release = buf; 215 } 216 217 void 218 native_version(void) 219 { 220 int mib[2]; 221 size_t len, tlen; 222 char *p; 223 static char buf[1024]; 224 225 mib[0] = CTL_KERN; 226 mib[1] = KERN_VERSION; 227 len = sizeof(buf); 228 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 229 err(1, "sysctl"); 230 for (p = buf, tlen = len; tlen--; ++p) 231 if (*p == '\n' || *p == '\t') 232 *p = ' '; 233 version = buf; 234 } 235 236 void 237 native_platform(void) 238 { 239 int mib[2]; 240 size_t len; 241 static char buf[1024]; 242 243 mib[0] = CTL_HW; 244 mib[1] = HW_MACHINE; 245 len = sizeof(buf); 246 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 247 err(1, "sysctl"); 248 platform = buf; 249 } 250 251 void 252 native_arch(void) 253 { 254 int mib[2]; 255 size_t len; 256 static char buf[1024]; 257 258 mib[0] = CTL_HW; 259 mib[1] = HW_MACHINE_ARCH; 260 len = sizeof(buf); 261 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 262 err(1, "sysctl"); 263 arch = buf; 264 } 265 266 void 267 usage(void) 268 { 269 fprintf(stderr, "usage: uname [-amnprsv]\n"); 270 exit(1); 271 } 272