init version.
[bush.git] / examples / loadables / uname.c
blob74770905679bfa49538621fd93af28501dfcce27
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 Bush.
12 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
27 #include <stdio.h>
29 #include "bushtypes.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 "bushgetopt.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 int
67 uname_builtin (list)
68 WORD_LIST *list;
70 int opt, r;
71 struct utsname uninfo;
73 uname_flags = 0;
74 reset_internal_getopt ();
75 while ((opt = internal_getopt (list, "amnprsv")) != -1)
77 switch (opt)
79 case 'a':
80 uname_flags |= FLAG_ALL;
81 break;
82 case 'm':
83 case 'p':
84 uname_flags |= FLAG_MACHINE;
85 break;
86 case 'n':
87 uname_flags |= FLAG_NODENAME;
88 break;
89 case 'r':
90 uname_flags |= FLAG_RELEASE;
91 break;
92 case 's':
93 uname_flags |= FLAG_SYSNAME;
94 break;
95 case 'v':
96 uname_flags |= FLAG_VERSION;
97 break;
98 CASE_HELPOPT;
99 default:
100 builtin_usage ();
101 return (EX_USAGE);
104 list = loptend;
106 if (list)
108 builtin_usage ();
109 return (EX_USAGE);
112 if (uname_flags == 0)
113 uname_flags = FLAG_SYSNAME;
115 /* Only ancient systems will not have uname(2). */
116 #ifdef HAVE_UNAME
117 if (uname (&uninfo) < 0)
119 builtin_error ("cannot get system name: %s", strerror (errno));
120 return (EXECUTION_FAILURE);
122 #else
123 builtin_error ("cannot get system information: uname(2) not available");
124 return (EXECUTION_FAILURE);
125 #endif
127 uprint (FLAG_SYSNAME, uninfo.sysname);
128 uprint (FLAG_NODENAME, uninfo.nodename);
129 uprint (FLAG_RELEASE, uninfo.release);
130 uprint (FLAG_VERSION, uninfo.version);
131 uprint (FLAG_MACHINE, uninfo.machine);
133 return (EXECUTION_SUCCESS);
136 static void
137 uprint (flag, info)
138 int flag;
139 char *info;
141 if (uname_flags & flag)
143 uname_flags &= ~flag;
144 printf ("%s%c", info, uname_flags ? ' ' : '\n');
148 char *uname_doc[] = {
149 "Display system information.",
151 "Display information about the system hardware and OS.",
152 (char *)NULL
155 struct builtin uname_struct = {
156 "uname",
157 uname_builtin,
158 BUILTIN_ENABLED,
159 uname_doc,
160 "uname [-amnrsv]",