Patch-ID: bash40-021
[bash.git] / examples / loadables / uname.c
bloba1bddd54368932f247e00a6b57651e462a331252
1 /*
2 * uname - print system information
4 * usage: uname [-amnrsv]
6 */
8 /*
9 Copyright (C) 1999-2009 Free Software Foundation, Inc.
11 This file is part of GNU Bash.
12 Bash is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Bash is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Bash. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
27 #include <stdio.h>
29 #include "bashtypes.h"
31 #if defined (HAVE_UNAME)
32 # include <sys/utsname.h>
33 #else
34 struct utsname {
35 char sysname[32];
36 char nodename[32];
37 char release[32];
38 char version[32];
39 char machine[32];
41 #endif
43 #include <errno.h>
45 #include "builtins.h"
46 #include "shell.h"
47 #include "bashgetopt.h"
48 #include "common.h"
50 #define FLAG_SYSNAME 0x01 /* -s */
51 #define FLAG_NODENAME 0x02 /* -n */
52 #define FLAG_RELEASE 0x04 /* -r */
53 #define FLAG_VERSION 0x08 /* -v */
54 #define FLAG_MACHINE 0x10 /* -m, -p */
56 #define FLAG_ALL 0x1f
58 #ifndef errno
59 extern int errno;
60 #endif
62 static void uprint();
64 static int uname_flags;
66 uname_builtin (list)
67 WORD_LIST *list;
69 int opt, r;
70 struct utsname uninfo;
72 uname_flags = 0;
73 reset_internal_getopt ();
74 while ((opt = internal_getopt (list, "amnprsv")) != -1)
76 switch (opt)
78 case 'a':
79 uname_flags |= FLAG_ALL;
80 break;
81 case 'm':
82 case 'p':
83 uname_flags |= FLAG_MACHINE;
84 break;
85 case 'n':
86 uname_flags |= FLAG_NODENAME;
87 break;
88 case 'r':
89 uname_flags |= FLAG_RELEASE;
90 break;
91 case 's':
92 uname_flags |= FLAG_SYSNAME;
93 break;
94 case 'v':
95 uname_flags |= FLAG_VERSION;
96 break;
97 default:
98 builtin_usage ();
99 return (EX_USAGE);
102 list = loptend;
104 if (list)
106 builtin_usage ();
107 return (EX_USAGE);
110 if (uname_flags == 0)
111 uname_flags = FLAG_SYSNAME;
113 /* Only ancient systems will not have uname(2). */
114 #ifdef HAVE_UNAME
115 if (uname (&uninfo) < 0)
117 builtin_error ("cannot get system name: %s", strerror (errno));
118 return (EXECUTION_FAILURE);
120 #else
121 builtin_error ("cannot get system information: uname(2) not available");
122 return (EXECUTION_FAILURE);
123 #endif
125 uprint (FLAG_SYSNAME, uninfo.sysname);
126 uprint (FLAG_NODENAME, uninfo.nodename);
127 uprint (FLAG_RELEASE, uninfo.release);
128 uprint (FLAG_VERSION, uninfo.version);
129 uprint (FLAG_MACHINE, uninfo.machine);
131 return (EXECUTION_SUCCESS);
134 static void
135 uprint (flag, info)
136 int flag;
137 char *info;
139 if (uname_flags & flag)
141 uname_flags &= ~flag;
142 printf ("%s%c", info, uname_flags ? ' ' : '\n');
146 char *uname_doc[] = {
147 "Display system information.",
149 "Display information about the system hardware and OS.",
150 (char *)NULL
153 struct builtin uname_struct = {
154 "uname",
155 uname_builtin,
156 BUILTIN_ENABLED,
157 uname_doc,
158 "uname [-amnrsv]",