uname.c revision 87696
1/*- 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. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35 36__FBSDID("$FreeBSD: head/usr.bin/uname/uname.c 87696 2001-12-11 23:16:25Z markm $"); 37 38#ifndef lint 39static const char copyright[] = 40"@(#) Copyright (c) 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42#endif 43 44#ifndef lint 45static const char sccsid[] = "@(#)uname.c 8.2 (Berkeley) 5/4/95"; 46#endif 47 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 56void usage __P((void)); 57 58int 59main(argc, argv) 60 int argc; 61 char *argv[]; 62{ 63#define MFLAG 0x01 64#define NFLAG 0x02 65#define RFLAG 0x04 66#define SFLAG 0x08 67#define VFLAG 0x10 68 u_int flags; 69 int ch, mib[2]; 70 size_t len, tlen; 71 char *p, buf[1024]; 72 const char *prefix; 73 74 flags = 0; 75 while ((ch = getopt(argc, argv, "amnprsv")) != -1) 76 switch(ch) { 77 case 'a': 78 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 79 break; 80 case 'p': 81 case 'm': 82 flags |= MFLAG; 83 break; 84 case 'n': 85 flags |= NFLAG; 86 break; 87 case 'r': 88 flags |= RFLAG; 89 break; 90 case 's': 91 flags |= SFLAG; 92 break; 93 case 'v': 94 flags |= VFLAG; 95 break; 96 case '?': 97 default: 98 usage(); 99 } 100 101 argc -= optind; 102 argv += optind; 103 104 if (argc) 105 usage(); 106 107 if (!flags) 108 flags |= SFLAG; 109 110 prefix = ""; 111 112 if (flags & SFLAG) { 113 mib[0] = CTL_KERN; 114 mib[1] = KERN_OSTYPE; 115 len = sizeof(buf); 116 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 117 err(1, "sysctl"); 118 (void)printf("%s%.*s", prefix, (int)len, buf); 119 prefix = " "; 120 } 121 if (flags & NFLAG) { 122 mib[0] = CTL_KERN; 123 mib[1] = KERN_HOSTNAME; 124 len = sizeof(buf); 125 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 126 err(1, "sysctl"); 127 (void)printf("%s%.*s", prefix, (int)len, buf); 128 prefix = " "; 129 } 130 if (flags & RFLAG) { 131 mib[0] = CTL_KERN; 132 mib[1] = KERN_OSRELEASE; 133 len = sizeof(buf); 134 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 135 err(1, "sysctl"); 136 (void)printf("%s%.*s", prefix, (int)len, buf); 137 prefix = " "; 138 } 139 if (flags & VFLAG) { 140 mib[0] = CTL_KERN; 141 mib[1] = KERN_VERSION; 142 len = sizeof(buf); 143 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 144 err(1, "sysctl"); 145 for (p = buf, tlen = len; tlen--; ++p) 146 if (*p == '\n' || *p == '\t') 147 *p = ' '; 148 (void)printf("%s%.*s", prefix, (int)len, buf); 149 prefix = " "; 150 } 151 if (flags & MFLAG) { 152 mib[0] = CTL_HW; 153 mib[1] = HW_MACHINE; 154 len = sizeof(buf); 155 if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 156 err(1, "sysctl"); 157 (void)printf("%s%.*s", prefix, (int)len, buf); 158 prefix = " "; 159 } 160 (void)printf("\n"); 161 exit (0); 162} 163 164void 165usage() 166{ 167 (void)fprintf(stderr, "usage: uname [-amnrsv]\n"); 168 exit(1); 169} 170