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