-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdUname.c
232 lines (206 loc) · 7.91 KB
/
cmdUname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: cmdUname.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, [email protected] **
** Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** Description: Provides fast aquisition of the information available**
** via uname. This shows a 10x improvement over having **
** to exec the actual uname program from within a **
** modulefile. **
** **
** Exports: cmdUname **
** **
** Notes: **
** **
** ************************************************************************ **
****/
/** ** Copyright *********************************************************** **
** **
** Copyright 1991-1994 by John L. Furlan. **
** see LICENSE.GPL, which must be provided, for details **
** **
** ************************************************************************ **/
static char Id[] = "@(#)$Id$";
static void *UseId[] = { &UseId, Id };
/** ************************************************************************ **/
/** HEADERS **/
/** ************************************************************************ **/
#include "modules_def.h"
#include <sys/param.h>
#ifdef HAVE_UNAME
# include <sys/utsname.h>
#else
/** ************************************************************************ **/
/** LOCAL DATATYPES **/
/** ************************************************************************ **/
typedef struct utsname {
char sysname[ NAMELEN]; /** System name **/
char nodename[ NAMELEN]; /** Node name **/
char release[ NAMELEN]; /** OS Release **/
char version[ NAMELEN]; /** OS Version **/
char machine[ NAMELEN]; /** Machine type **/
} UTS_NAME;
#endif
/** ************************************************************************ **/
/** CONSTANTS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** MACROS **/
/** ************************************************************************ **/
#define NAMELEN (8 + 1) /** 8 chars + 1 terminator **/
#define DOMAINLEN (MAXHOSTNAMELEN + 1) /** from from <sys/param.h> **/
/** ************************************************************************ **/
/** LOCAL DATA **/
/** ************************************************************************ **/
static char module_name[] = __FILE__;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
/** not applicable **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: cmdUname **
** **
** Description: Callback function for the commands 'sysname', **
** 'nodename', 'release', 'version' and 'machine' **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: ClientData client_data **
** Tcl_Interp *interp According Tcl interp.**
** int objc Number of arguments **
** Tcl_Obj *objv[] Argument array **
** **
** Result: int TCL_OK Successful completion **
** TCL_ERROR Any error **
** **
** Attached Globals: flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int cmdUname(
ClientData client_data,
Tcl_Interp * interp,
int objc,
Tcl_Obj * CONST84 objv[]
) {
#ifdef PHOSTNAME
# ifndef HAVE_GETHOSTNAME
FILE *hname;
# endif
#endif
char domain[DOMAINLEN],
/** host domain **/
*arg1; /** argument string **/
int length, /** argument string length **/
namestruct_init = 0;
/** namestruct init flag **/
*domain ='\0';
#ifdef HAVE_UNAME
struct utsname namestruct;
#else
UTS_NAME namestruct;
strncat(namestruct.sysname, _(em_unknown), NAMELEN);
strncat(namestruct.nodename, _(em_unknown), NAMELEN);
strncat(namestruct.release, _(em_unknown), NAMELEN);
strncat(namestruct.version, _(em_unknown), NAMELEN);
strncat(namestruct.machine, _(em_unknown), NAMELEN);
#endif
/**
** Parameter check. One parameter should be given providing a selector
** do differ between:
**/
if (objc != 2) {
if (OK !=
ErrorLogger(ERR_USAGE, LOC, Tcl_GetString(objv[0]),
"member", NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
#ifdef HAVE_UNAME
/**
** Process the system call
**/
if (!namestruct_init && uname(&namestruct) < 0) {
if (OK != ErrorLogger(ERR_UNAME, LOC, NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
#else /* not HAVE_UNAME */
/**
** If we do not have the uname system call, fixed values defined
** at compile time will be returned. The only differenc is the
** nodename, which may be seeked for using 'gethostname' or the
** PHOSTNAME file.
**/
# ifdef HAVE_GETHOSTNAME
if (-1 == gethostname(namestruct.nodename, NAMELEN))
if (OK != ErrorLogger(ERR_GETHOSTNAME, LOC, NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
# else /* not HAVE_GETHOSTNAME */
# ifdef PHOSTNAME
if (!(hname = popen(PHOSTNAME, "r"))) {
if (OK != ErrorLogger(ERR_POPEN, LOC, PHOSTNAME,
_(em_reading), NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
fgets(namestruct.nodename, NAMELEN, hname);
namestruct.nodename[strlen(namestruct.nodename) - 1] = '\0';
if (-1 == pclose(hname))
if (OK != ErrorLogger(ERR_PCLOSE, LOC, PHOSTNAME, NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
# endif /* not PHOSTNAME */
# endif /* not HAVE_GETHOSTNAME */
#endif /* not HAVE_UNAME */
/**
** Set up the domain name
**/
#ifdef HAVE_GETDOMAINNAME
if (!namestruct_init)
if (-1 == getdomainname(domain, DOMAINLEN))
if (OK != ErrorLogger(ERR_GETDOMAINNAME, LOC, NULL))
return (TCL_ERROR); /** -- EXIT (FAILURE) -> **/
#endif
/**
** some oses (BSD) return empty domain
**/
if (! *domain)
strncat(domain, _(em_unknown), DOMAINLEN);
/**
** Now the name structure surely IS initialized
**/
namestruct_init = 1;
/**
** Return the selected value
**/
arg1 = Tcl_GetString(objv[1]);
length = strlen(arg1);
if (!strncmp(arg1, "sysname", length)) {
Tcl_SetResult(interp, namestruct.sysname, TCL_VOLATILE);
} else if (!strncmp(arg1, "nodename", length)) {
Tcl_SetResult(interp, namestruct.nodename, TCL_VOLATILE);
} else if (!strncmp(arg1, "release", length)) {
Tcl_SetResult(interp, namestruct.release, TCL_VOLATILE);
} else if (!strncmp(arg1, "version", length)) {
Tcl_SetResult(interp, namestruct.version, TCL_VOLATILE);
} else if (!strncmp(arg1, "machine", length)) {
Tcl_SetResult(interp, namestruct.machine, TCL_VOLATILE);
} else if (!strncmp(arg1, "domain", length)) {
Tcl_SetResult(interp, domain, TCL_VOLATILE);
} else {
if (OK != ErrorLogger(ERR_USAGE, LOC, Tcl_GetString(objv[0]),
"{sysname|nodename|release|version|machine|domain}", NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
return (TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
} /** End of 'cmdUname' **/