.
[coreutils.git] / src / uname.c
blob9a402d06815e18582f11fcd331bbc9ade2b86a71
1 /* uname -- print system information
2 Copyright (C) 89, 90, 91, 92, 93, 94, 95, 1996 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 #include "system.h"
38 #include "error.h"
40 static void print_element __P ((unsigned int mask, char *element));
41 static void usage __P ((int status));
43 /* Values that are bitwise or'd into `toprint'. */
44 /* Operating system name. */
45 #define PRINT_SYSNAME 1
47 /* Node name on a communications network. */
48 #define PRINT_NODENAME 2
50 /* Operating system release. */
51 #define PRINT_RELEASE 4
53 /* Operating system version. */
54 #define PRINT_VERSION 8
56 /* Machine hardware name. */
57 #define PRINT_MACHINE 16
59 /* Mask indicating which elements of the name to print. */
60 static unsigned char toprint;
62 /* The name this program was run with, for error messages. */
63 char *program_name;
65 /* If nonzero, display usage information and exit. */
66 static int show_help;
68 /* If nonzero, print the version on standard output and exit. */
69 static int show_version;
71 static struct option const long_options[] =
73 {"help", no_argument, &show_help, 1},
74 {"machine", no_argument, NULL, 'm'},
75 {"nodename", no_argument, NULL, 'n'},
76 {"release", no_argument, NULL, 'r'},
77 {"sysname", no_argument, NULL, 's'},
78 {"version", no_argument, &show_version, 1},
79 {"all", no_argument, NULL, 'a'},
80 {NULL, 0, NULL, 0}
83 int
84 main (int argc, char **argv)
86 struct utsname name;
87 int c;
89 program_name = argv[0];
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEDIR);
92 textdomain (PACKAGE);
94 toprint = 0;
96 while ((c = getopt_long (argc, argv, "snrvma", long_options, (int *) 0))
97 != EOF)
99 switch (c)
101 case 0:
102 break;
104 case 's':
105 toprint |= PRINT_SYSNAME;
106 break;
108 case 'n':
109 toprint |= PRINT_NODENAME;
110 break;
112 case 'r':
113 toprint |= PRINT_RELEASE;
114 break;
116 case 'v':
117 toprint |= PRINT_VERSION;
118 break;
120 case 'm':
121 toprint |= PRINT_MACHINE;
122 break;
124 case 'a':
125 toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
126 PRINT_VERSION | PRINT_MACHINE;
127 break;
129 default:
130 usage (1);
134 if (show_version)
136 printf ("uname - %s\n", PACKAGE_VERSION);
137 exit (0);
140 if (show_help)
141 usage (0);
143 if (optind != argc)
144 usage (1);
146 if (toprint == 0)
147 toprint = PRINT_SYSNAME;
149 if (uname (&name) == -1)
150 error (1, errno, _("cannot get system name"));
152 print_element (PRINT_SYSNAME, name.sysname);
153 print_element (PRINT_NODENAME, name.nodename);
154 print_element (PRINT_RELEASE, name.release);
155 print_element (PRINT_VERSION, name.version);
156 print_element (PRINT_MACHINE, name.machine);
158 exit (0);
161 /* If the name element set in MASK is selected for printing in `toprint',
162 print ELEMENT; then print a space unless it is the last element to
163 be printed, in which case print a newline. */
165 static void
166 print_element (unsigned int mask, char *element)
168 if (toprint & mask)
170 toprint &= ~mask;
171 printf ("%s%c", element, toprint ? ' ' : '\n');
175 static void
176 usage (int status)
178 if (status != 0)
179 fprintf (stderr, _("Try `%s --help' for more information.\n"),
180 program_name);
181 else
183 printf (_("Usage: %s [OPTION]...\n"), program_name);
184 printf (_("\
185 Print certain system information. With no OPTION, same as -s.\n\
187 -a, --all print all information\n\
188 -m, --machine print the machine (hardware) type\n\
189 -n, --nodename print the machine's network node hostname\n\
190 -r, --release print the operating system release\n\
191 -s, --sysname print the operating system name\n\
192 -v print the operating system version\n\
193 --help display this help and exit\n\
194 --version output version information and exit\n"));
196 exit (status);