(add_tabstop): Give correct size when reallocating tab_list buffer.
[platform/upstream/coreutils.git] / src / uname.c
1 /* uname -- print system information
2    Copyright (C) 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3
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.
8
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.
13
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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Option               Example
19
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
26
27    The default behavior is equivalent to `-s'.
28
29    David MacKenzie <djm@gnu.ai.mit.edu> */
30
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/utsname.h>
35 #include <getopt.h>
36
37 #include "system.h"
38 #include "version.h"
39 #include "error.h"
40
41 static void print_element ();
42 static void usage ();
43
44 /* Values that are bitwise or'd into `toprint'. */
45 /* Operating system name. */
46 #define PRINT_SYSNAME 1
47
48 /* Node name on a communications network. */
49 #define PRINT_NODENAME 2
50
51 /* Operating system release. */
52 #define PRINT_RELEASE 4
53
54 /* Operating system version. */
55 #define PRINT_VERSION 8
56
57 /* Machine hardware name. */
58 #define PRINT_MACHINE 16
59
60 /* Mask indicating which elements of the name to print. */
61 static unsigned char toprint;
62
63 /* The name this program was run with, for error messages. */
64 char *program_name;
65
66 /* If non-zero, display usage information and exit.  */
67 static int show_help;
68
69 /* If non-zero, print the version on standard output and exit.  */
70 static int show_version;
71
72 static struct option const long_options[] =
73 {
74   {"help", no_argument, &show_help, 1},
75   {"machine", no_argument, NULL, 'm'},
76   {"nodename", no_argument, NULL, 'n'},
77   {"release", no_argument, NULL, 'r'},
78   {"sysname", no_argument, NULL, 's'},
79   {"version", no_argument, &show_version, 1},
80   {"all", no_argument, NULL, 'a'},
81   {NULL, 0, NULL, 0}
82 };
83
84 void
85 main (argc, argv)
86      int argc;
87      char **argv;
88 {
89   struct utsname name;
90   int c;
91
92   program_name = argv[0];
93   toprint = 0;
94
95   while ((c = getopt_long (argc, argv, "snrvma", long_options, (int *) 0))
96          != EOF)
97     {
98       switch (c)
99         {
100         case 0:
101           break;
102
103         case 's':
104           toprint |= PRINT_SYSNAME;
105           break;
106
107         case 'n':
108           toprint |= PRINT_NODENAME;
109           break;
110
111         case 'r':
112           toprint |= PRINT_RELEASE;
113           break;
114
115         case 'v':
116           toprint |= PRINT_VERSION;
117           break;
118
119         case 'm':
120           toprint |= PRINT_MACHINE;
121           break;
122
123         case 'a':
124           toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
125             PRINT_VERSION | PRINT_MACHINE;
126           break;
127
128         default:
129           usage (1);
130         }
131     }
132
133   if (show_version)
134     {
135       printf ("uname - %s\n", version_string);
136       exit (0);
137     }
138
139   if (show_help)
140     usage (0);
141
142   if (optind != argc)
143     usage (1);
144
145   if (toprint == 0)
146     toprint = PRINT_SYSNAME;
147
148   if (uname (&name) == -1)
149     error (1, errno, "cannot get system name");
150
151   print_element (PRINT_SYSNAME, name.sysname);
152   print_element (PRINT_NODENAME, name.nodename);
153   print_element (PRINT_RELEASE, name.release);
154   print_element (PRINT_VERSION, name.version);
155   print_element (PRINT_MACHINE, name.machine);
156
157   exit (0);
158 }
159
160 /* If the name element set in MASK is selected for printing in `toprint',
161    print ELEMENT; then print a space unless it is the last element to
162    be printed, in which case print a newline. */
163
164 static void
165 print_element (mask, element)
166      unsigned char mask;
167      char *element;
168 {
169   if (toprint & mask)
170     {
171       toprint &= ~mask;
172       printf ("%s%c", element, toprint ? ' ' : '\n');
173     }
174 }
175
176 static void
177 usage (status)
178      int status;
179 {
180   if (status != 0)
181     fprintf (stderr, "Try `%s --help' for more information.\n",
182              program_name);
183   else
184     {
185       printf ("Usage: %s [OPTION]...\n", program_name);
186       printf ("\
187 \n\
188   -a, --all        print all information\n\
189   -m, --machine    print the machine (hardware) type\n\
190   -n, --nodename   print the machine's network node hostname\n\
191   -r, --release    print the operating system release\n\
192   -s, --sysname    print the operating system name\n\
193   -v               print the operating system version\n\
194       --help       display this help and exit\n\
195       --version    output version information and exit\n\
196 \n\
197 Without any OPTION, assume -s.\n\
198 ");
199     }
200   exit (status);
201 }