GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/uname/uname.c Lines: 38 56 67.9 %
Date: 2016-12-06 Branches: 27 52 51.9 %

Line Branch Exec Source
1
/*	$OpenBSD: uname.c,v 1.17 2015/12/24 15:01:24 tb 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/param.h>	/* MACHINE_ARCH */
35
#include <sys/utsname.h>
36
37
#include <err.h>
38
#include <locale.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <unistd.h>
42
43
static void usage(void);
44
45
#define	PRINT_SYSNAME		0x01
46
#define	PRINT_NODENAME		0x02
47
#define	PRINT_RELEASE		0x04
48
#define	PRINT_VERSION		0x08
49
#define	PRINT_MACHINE		0x10
50
#define	PRINT_ALL		0x1f
51
#define PRINT_MACHINE_ARCH	0x20
52
53
int
54
main(int argc, char *argv[])
55
14
{
56
	struct utsname u;
57
	int c;
58
14
	int space = 0;
59
14
	int print_mask = 0;
60
61
14
	setlocale(LC_ALL, "");
62
63
14
	if (pledge("stdio wpath cpath rpath", NULL) == -1)
64
		err(1, "pledge");
65
66
28
	while ((c = getopt(argc, argv, "amnrsvp")) != -1 ) {
67


14
		switch (c) {
68
		case 'a':
69
			print_mask |= PRINT_ALL;
70
			break;
71
		case 'm':
72
3
			print_mask |= PRINT_MACHINE;
73
3
			break;
74
		case 'n':
75
			print_mask |= PRINT_NODENAME;
76
			break;
77
		case 'p':
78
2
			print_mask |= PRINT_MACHINE_ARCH;
79
2
			break;
80
		case 'r':
81
3
			print_mask |= PRINT_RELEASE;
82
3
			break;
83
		case 's':
84
3
			print_mask |= PRINT_SYSNAME;
85
3
			break;
86
		case 'v':
87
3
			print_mask |= PRINT_VERSION;
88
3
			break;
89
		default:
90
			usage();
91
			/* NOTREACHED */
92
		}
93
	}
94
95
14
	if (optind != argc) {
96
		usage();
97
		/* NOTREACHED */
98
	}
99
100
14
	if (!print_mask) {
101
		print_mask = PRINT_SYSNAME;
102
	}
103
104
14
	if (uname(&u))
105
		err(1, NULL);
106
107
14
	if (print_mask & PRINT_SYSNAME) {
108
3
		space++;
109
3
		fputs(u.sysname, stdout);
110
	}
111
14
	if (print_mask & PRINT_NODENAME) {
112
		if (space++)
113
			putchar(' ');
114
115
		fputs(u.nodename, stdout);
116
	}
117
14
	if (print_mask & PRINT_RELEASE) {
118
3
		if (space++)
119
			putchar(' ');
120
121
3
		fputs(u.release, stdout);
122
	}
123
14
	if (print_mask & PRINT_VERSION) {
124
3
		if (space++)
125
			putchar(' ');
126
127
3
		fputs(u.version, stdout);
128
	}
129
14
	if (print_mask & PRINT_MACHINE) {
130
3
		if (space++)
131
			putchar(' ');
132
133
3
		fputs(u.machine, stdout);
134
	}
135
14
	if (print_mask & PRINT_MACHINE_ARCH) {
136
2
		if (space++)
137
			putchar(' ');
138
139
2
		fputs(MACHINE_ARCH, stdout);
140
	}
141
14
	putchar('\n');
142
143
14
	return 0;
144
}
145
146
static void
147
usage(void)
148
{
149
	fprintf(stderr, "usage: uname [-amnprsv]\n");
150
	exit(1);
151
}