uname.c (f4ac32def255b5968bdd8150057b9c0d14595689) | uname.c (bed4636e02fb59590f9da06d2a5f4d284d1bf578) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2002 Juli Mallett. |
|
2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 38 unchanged lines hidden (view full) --- 48#include <sys/param.h> 49#include <sys/sysctl.h> 50 51#include <err.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <unistd.h> 55 | 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. --- 38 unchanged lines hidden (view full) --- 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 |
56void usage(void); 57 58int 59main(int argc, char *argv[]) 60{ | |
61#define MFLAG 0x01 62#define NFLAG 0x02 63#define PFLAG 0x04 64#define RFLAG 0x08 65#define SFLAG 0x10 66#define VFLAG 0x20 | 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 64typedef void (*get_t)(void); 65get_t get_platform, get_hostname, get_arch, get_release, get_sysname, get_version; 66 67void native_platform(void); 68void native_hostname(void); 69void native_arch(void); 70void native_release(void); 71void native_sysname(void); 72void native_version(void); 73void print_uname(u_int); 74void setup_get(void); 75void usage(void); 76 77char *platform, *hostname, *arch, *release, *sysname, *version; 78const char *prefix; 79 80int 81main(int argc, char *argv[]) 82{ |
|
67 u_int flags; | 83 u_int flags; |
68 int ch, mib[2]; 69 size_t len, tlen; 70 char *p, buf[1024]; 71 const char *prefix; | 84 int ch; |
72 | 85 |
86 prefix = ""; 87 88 setup_get(); 89 |
|
73 flags = 0; 74 while ((ch = getopt(argc, argv, "amnprsv")) != -1) 75 switch(ch) { 76 case 'a': 77 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 78 break; 79 case 'm': 80 flags |= MFLAG; --- 22 unchanged lines hidden (view full) --- 103 argv += optind; 104 105 if (argc) 106 usage(); 107 108 if (!flags) 109 flags |= SFLAG; 110 | 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; --- 22 unchanged lines hidden (view full) --- 120 argv += optind; 121 122 if (argc) 123 usage(); 124 125 if (!flags) 126 flags |= SFLAG; 127 |
111 prefix = ""; | 128 print_uname(flags); 129 exit(0); 130} |
112 | 131 |
113 if (flags & SFLAG) { 114 mib[0] = CTL_KERN; 115 mib[1] = KERN_OSTYPE; 116 len = sizeof(buf); 117 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 118 err(1, "sysctl"); 119 (void)printf("%s%.*s", prefix, (int)len, buf); 120 prefix = " "; | 132#define CHECK_ENV(opt,var) \ 133do { \ 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 141void 142setup_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 = " "; \ |
121 } | 158 } |
122 if (flags & NFLAG) { 123 mib[0] = CTL_KERN; 124 mib[1] = KERN_HOSTNAME; 125 len = sizeof(buf); 126 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 127 err(1, "sysctl"); 128 (void)printf("%s%.*s", prefix, (int)len, buf); 129 prefix = " "; 130 } 131 if (flags & RFLAG) { 132 mib[0] = CTL_KERN; 133 mib[1] = KERN_OSRELEASE; 134 len = sizeof(buf); 135 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 136 err(1, "sysctl"); 137 (void)printf("%s%.*s", prefix, (int)len, buf); 138 prefix = " "; 139 } 140 if (flags & VFLAG) { 141 mib[0] = CTL_KERN; 142 mib[1] = KERN_VERSION; 143 len = sizeof(buf); 144 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 145 err(1, "sysctl"); 146 for (p = buf, tlen = len; tlen--; ++p) 147 if (*p == '\n' || *p == '\t') 148 *p = ' '; 149 (void)printf("%s%.*s", prefix, (int)len, buf); 150 prefix = " "; 151 } 152 if (flags & MFLAG) { 153 mib[0] = CTL_HW; 154 mib[1] = HW_MACHINE; 155 len = sizeof(buf); 156 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 157 err(1, "sysctl"); 158 (void)printf("%s%.*s", prefix, (int)len, buf); 159 prefix = " "; 160 } 161 if (flags & PFLAG) { 162 mib[0] = CTL_HW; 163 mib[1] = HW_MACHINE_ARCH; 164 len = sizeof(buf); 165 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 166 err(1, "sysctl"); 167 (void)printf("%s%.*s", prefix, (int)len, buf); 168 prefix = " "; 169 } 170 (void)printf("\n"); 171 exit (0); | 159 160void 161print_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"); |
172} 173 174void | 170} 171 172void |
173native_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 187void 188native_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 202void 203native_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 217void 218native_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 236void 237native_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 251void 252native_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 266void |
|
175usage(void) 176{ | 267usage(void) 268{ |
177 (void)fprintf(stderr, "usage: uname [-amnprsv]\n"); | 269 fprintf(stderr, "usage: uname [-amnprsv]\n"); |
178 exit(1); 179} | 270 exit(1); 271} |