blob: 14a9bbb2f3db206e58421a01ae1e5439cadf66af [file] [log] [blame]
Jari Aaltob72432f1999-02-19 17:11:39 +00001/*
2 * uname - print system information
3 *
4 * usage: uname [-amnrsv]
5 *
6 */
7
Jari Aalto31859422009-01-12 13:36:28 +00008/*
9 Copyright (C) 1999-2009 Free Software Foundation, Inc.
10
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.
16
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.
21
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/>.
24*/
25
Jari Aaltob72432f1999-02-19 17:11:39 +000026#include <config.h>
27#include <stdio.h>
28
29#include "bashtypes.h"
30
31#if defined (HAVE_UNAME)
32# include <sys/utsname.h>
33#else
34struct utsname {
35 char sysname[32];
36 char nodename[32];
37 char release[32];
38 char version[32];
39 char machine[32];
40};
41#endif
42
43#include <errno.h>
44
45#include "builtins.h"
46#include "shell.h"
47#include "bashgetopt.h"
Jari Aalto31859422009-01-12 13:36:28 +000048#include "common.h"
Jari Aaltob72432f1999-02-19 17:11:39 +000049
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 */
55
56#define FLAG_ALL 0x1f
57
58#ifndef errno
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010059#include <errno.h>
Jari Aaltob72432f1999-02-19 17:11:39 +000060#endif
61
62static void uprint();
63
64static int uname_flags;
65
66uname_builtin (list)
67 WORD_LIST *list;
68{
69 int opt, r;
70 struct utsname uninfo;
71
72 uname_flags = 0;
73 reset_internal_getopt ();
74 while ((opt = internal_getopt (list, "amnprsv")) != -1)
75 {
76 switch (opt)
77 {
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);
100 }
101 }
102 list = loptend;
103
104 if (list)
105 {
106 builtin_usage ();
107 return (EX_USAGE);
108 }
109
110 if (uname_flags == 0)
111 uname_flags = FLAG_SYSNAME;
112
113 /* Only ancient systems will not have uname(2). */
114#ifdef HAVE_UNAME
115 if (uname (&uninfo) < 0)
116 {
117 builtin_error ("cannot get system name: %s", strerror (errno));
118 return (EXECUTION_FAILURE);
119 }
120#else
121 builtin_error ("cannot get system information: uname(2) not available");
122 return (EXECUTION_FAILURE);
123#endif
124
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);
130
131 return (EXECUTION_SUCCESS);
132}
133
134static void
135uprint (flag, info)
136 int flag;
137 char *info;
138{
139 if (uname_flags & flag)
140 {
141 uname_flags &= ~flag;
142 printf ("%s%c", info, uname_flags ? ' ' : '\n');
143 }
144}
145
146char *uname_doc[] = {
Jari Aalto31859422009-01-12 13:36:28 +0000147 "Display system information.",
148 "",
149 "Display information about the system hardware and OS.",
Jari Aaltob72432f1999-02-19 17:11:39 +0000150 (char *)NULL
151};
152
153struct builtin uname_struct = {
154 "uname",
155 uname_builtin,
156 BUILTIN_ENABLED,
157 uname_doc,
158 "uname [-amnrsv]",
159 0
160};