Patch-ID: bash32-032
[bash.git] / examples / loadables / uname.c
blob9f450cdad1c39d2ff683db9bb97f59175192dd11
1 /*
2 * uname - print system information
4 * usage: uname [-amnrsv]
6 */
8 #include <config.h>
9 #include <stdio.h>
11 #include "bashtypes.h"
13 #if defined (HAVE_UNAME)
14 # include <sys/utsname.h>
15 #else
16 struct utsname {
17 char sysname[32];
18 char nodename[32];
19 char release[32];
20 char version[32];
21 char machine[32];
23 #endif
25 #include <errno.h>
27 #include "builtins.h"
28 #include "shell.h"
29 #include "bashgetopt.h"
31 #define FLAG_SYSNAME 0x01 /* -s */
32 #define FLAG_NODENAME 0x02 /* -n */
33 #define FLAG_RELEASE 0x04 /* -r */
34 #define FLAG_VERSION 0x08 /* -v */
35 #define FLAG_MACHINE 0x10 /* -m, -p */
37 #define FLAG_ALL 0x1f
39 #ifndef errno
40 extern int errno;
41 #endif
43 static void uprint();
45 static int uname_flags;
47 uname_builtin (list)
48 WORD_LIST *list;
50 int opt, r;
51 struct utsname uninfo;
53 uname_flags = 0;
54 reset_internal_getopt ();
55 while ((opt = internal_getopt (list, "amnprsv")) != -1)
57 switch (opt)
59 case 'a':
60 uname_flags |= FLAG_ALL;
61 break;
62 case 'm':
63 case 'p':
64 uname_flags |= FLAG_MACHINE;
65 break;
66 case 'n':
67 uname_flags |= FLAG_NODENAME;
68 break;
69 case 'r':
70 uname_flags |= FLAG_RELEASE;
71 break;
72 case 's':
73 uname_flags |= FLAG_SYSNAME;
74 break;
75 case 'v':
76 uname_flags |= FLAG_VERSION;
77 break;
78 default:
79 builtin_usage ();
80 return (EX_USAGE);
83 list = loptend;
85 if (list)
87 builtin_usage ();
88 return (EX_USAGE);
91 if (uname_flags == 0)
92 uname_flags = FLAG_SYSNAME;
94 /* Only ancient systems will not have uname(2). */
95 #ifdef HAVE_UNAME
96 if (uname (&uninfo) < 0)
98 builtin_error ("cannot get system name: %s", strerror (errno));
99 return (EXECUTION_FAILURE);
101 #else
102 builtin_error ("cannot get system information: uname(2) not available");
103 return (EXECUTION_FAILURE);
104 #endif
106 uprint (FLAG_SYSNAME, uninfo.sysname);
107 uprint (FLAG_NODENAME, uninfo.nodename);
108 uprint (FLAG_RELEASE, uninfo.release);
109 uprint (FLAG_VERSION, uninfo.version);
110 uprint (FLAG_MACHINE, uninfo.machine);
112 return (EXECUTION_SUCCESS);
115 static void
116 uprint (flag, info)
117 int flag;
118 char *info;
120 if (uname_flags & flag)
122 uname_flags &= ~flag;
123 printf ("%s%c", info, uname_flags ? ' ' : '\n');
127 char *uname_doc[] = {
128 "display information about the system",
129 (char *)NULL
132 struct builtin uname_struct = {
133 "uname",
134 uname_builtin,
135 BUILTIN_ENABLED,
136 uname_doc,
137 "uname [-amnrsv]",