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