(noinst_HEADERS): Add nanosleep.h.
[coreutils.git] / src / uname.c
blobf199ad0656e0424320bc177307133d732d9c2b50
1 /* uname -- print system information
2 Copyright (C) 1989-1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Option Example
20 -s, --sysname SunOS
21 -n, --nodename rocky8
22 -r, --release 4.0
23 -v, --version
24 -m, --machine sun
25 -a, --all SunOS rocky8 4.0 sun
27 The default behavior is equivalent to `-s'.
29 David MacKenzie <djm@gnu.ai.mit.edu> */
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/utsname.h>
35 #include <getopt.h>
37 #if defined (HAVE_SYSINFO) && defined (HAVE_SYS_SYSTEMINFO_H)
38 # include <sys/systeminfo.h>
39 #endif
41 #include "system.h"
42 #include "error.h"
44 /* The official name of this program (e.g., no `g' prefix). */
45 #define PROGRAM_NAME "uname"
47 #define AUTHORS "David MacKenzie"
49 static void print_element PARAMS ((unsigned int mask, char *element));
51 /* Values that are bitwise or'd into `toprint'. */
52 /* Operating system name. */
53 #define PRINT_SYSNAME 1
55 /* Node name on a communications network. */
56 #define PRINT_NODENAME 2
58 /* Operating system release. */
59 #define PRINT_RELEASE 4
61 /* Operating system version. */
62 #define PRINT_VERSION 8
64 /* Machine hardware name. */
65 #define PRINT_MACHINE 16
67 /* Host processor type. */
68 #define PRINT_PROCESSOR 32
70 /* Mask indicating which elements of the name to print. */
71 static unsigned char toprint;
73 /* The name this program was run with, for error messages. */
74 char *program_name;
76 static struct option const long_options[] =
78 {"machine", no_argument, NULL, 'm'},
79 {"nodename", no_argument, NULL, 'n'},
80 {"release", no_argument, NULL, 'r'},
81 {"sysname", no_argument, NULL, 's'},
82 {"processor", no_argument, NULL, 'p'},
83 {"all", no_argument, NULL, 'a'},
84 {GETOPT_HELP_OPTION_DECL},
85 {GETOPT_VERSION_OPTION_DECL},
86 {NULL, 0, NULL, 0}
89 void
90 usage (int status)
92 if (status != 0)
93 fprintf (stderr, _("Try `%s --help' for more information.\n"),
94 program_name);
95 else
97 printf (_("Usage: %s [OPTION]...\n"), program_name);
98 printf (_("\
99 Print certain system information. With no OPTION, same as -s.\n\
101 -a, --all print all information\n\
102 -m, --machine print the machine (hardware) type\n\
103 -n, --nodename print the machine's network node hostname\n\
104 -r, --release print the operating system release\n\
105 -s, --sysname print the operating system name\n\
106 -p, --processor print the host processor type\n\
107 -v print the operating system version\n\
108 --help display this help and exit\n\
109 --version output version information and exit\n"));
110 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
112 exit (status);
116 main (int argc, char **argv)
118 struct utsname name;
119 int c;
120 char processor[256];
122 program_name = argv[0];
123 setlocale (LC_ALL, "");
124 bindtextdomain (PACKAGE, LOCALEDIR);
125 textdomain (PACKAGE);
127 toprint = 0;
129 while ((c = getopt_long (argc, argv, "snrvpma", long_options, NULL)) != -1)
131 switch (c)
133 case 0:
134 break;
136 case 's':
137 toprint |= PRINT_SYSNAME;
138 break;
140 case 'n':
141 toprint |= PRINT_NODENAME;
142 break;
144 case 'r':
145 toprint |= PRINT_RELEASE;
146 break;
148 case 'v':
149 toprint |= PRINT_VERSION;
150 break;
152 case 'm':
153 toprint |= PRINT_MACHINE;
154 break;
156 case 'p':
157 toprint |= PRINT_PROCESSOR;
158 break;
160 case 'a':
161 toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
162 PRINT_PROCESSOR | PRINT_VERSION | PRINT_MACHINE);
163 break;
165 case_GETOPT_HELP_CHAR;
167 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
169 default:
170 usage (1);
174 if (optind != argc)
175 usage (1);
177 if (toprint == 0)
178 toprint = PRINT_SYSNAME;
180 if (uname (&name) == -1)
181 error (1, errno, _("cannot get system name"));
183 #if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE)
184 if (sysinfo (SI_ARCHITECTURE, processor, sizeof (processor)) == -1)
185 error (1, errno, _("cannot get processor type"));
186 #else
187 strcpy (processor, "unknown");
188 #endif
190 print_element (PRINT_SYSNAME, name.sysname);
191 print_element (PRINT_NODENAME, name.nodename);
192 print_element (PRINT_RELEASE, name.release);
193 print_element (PRINT_VERSION, name.version);
194 print_element (PRINT_MACHINE, name.machine);
195 print_element (PRINT_PROCESSOR, processor);
197 exit (0);
200 /* If the name element set in MASK is selected for printing in `toprint',
201 print ELEMENT; then print a space unless it is the last element to
202 be printed, in which case print a newline. */
204 static void
205 print_element (unsigned int mask, char *element)
207 if (toprint & mask)
209 toprint &= ~mask;
210 printf ("%s%c", element, toprint ? ' ' : '\n');