-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdMisc.c
More file actions
169 lines (144 loc) · 5.53 KB
/
cmdMisc.c
File metadata and controls
169 lines (144 loc) · 5.53 KB
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
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: cmdMisc.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, jlf@behere.com **
** Jens Hamisch, jens@Strawberry.COM **
** **
** Description: The Tcl 'system' command **
** **
** Exports: cmdSystem **
** **
** Notes: The Following code was written by Don Libes, NIST **
** It was taken from his tool, 'expect' ... **
** I have edited parts of it... **
** **
** ************************************************************************ **
****/
/** ** 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"
/** ************************************************************************ **/
/** LOCAL DATATYPES **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** CONSTANTS **/
/** ************************************************************************ **/
#define MAX_ARGLIST 10240
/** ************************************************************************ **/
/** MACROS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** LOCAL DATA **/
/** ************************************************************************ **/
static char module_name[] = __FILE__;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
/** not applicable **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: cmdSystem **
** **
** Description: Callback function for 'system' **
** **
** 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: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int cmdSystem(
ClientData clientData,
Tcl_Interp * interp,
int objc,
Tcl_Obj * CONST84 objv[]
) {
int saved_stdout, /** save the stdout fd **/
total_len = 0, /** total string length **/
arg_len, /** each arg string length **/
i; /** loop counter **/
char buf[MAX_ARGLIST], /** buffer for string to int **/
*bufp = buf; /** buffer ptr **/
/**
** Whatis mode
**/
if (g_flags & (M_WHATIS | M_HELP))
return (TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
/**
** Display mode?
**/
if (g_flags & M_DISPLAY) {
fprintf(stderr, "%s\t\t ", Tcl_GetString(objv[0]));
for (i = 1; i < objc; i++)
fprintf(stderr, "%s ", Tcl_GetString(objv[i]));
fprintf(stderr, "\n");
return (TCL_OK); /** -------- EXIT PROCEDURE -------> **/
}
/**
** Prepare a buffer to hold the complete 'system' call
** Watch over the commands complete length while copying ...
**/
for (i = 1; i < objc; i++) {
total_len += (1 + (arg_len = strlen(Tcl_GetString(objv[i]))));
if (total_len > MAX_ARGLIST) {
if (OK != ErrorLogger(ERR_ARGSTOLONG, LOC,
Tcl_GetString(objv[0]),
(sprintf(buf, "%d", total_len), buf), NULL))
return (TCL_ERROR); /** -- EXIT (FAILURE) -> **/
}
/**
** Copy the argument on the buffer and put a space at the end
**/
memcpy(bufp, Tcl_GetString(objv[i]), arg_len);
bufp += arg_len;
memcpy(bufp, " ", 1);
bufp += 1;
}
/**
** For Modules, stdout must be directed to stderr so it
** isn't parsed by the evaluating shell. We also must save it here so it
** can be restored after this command has been executed.
**/
saved_stdout = TieStdout();
*(bufp - 1) = '\0';
i = system(buf);
/**
** Following the style of Tcl_ExecCmd, we can just return the
** raw result (appropriately shifted and masked) to Tcl
**/
sprintf(buf, "%d", (0xff & (i >> 8)));
Tcl_SetResult(interp, buf, TCL_VOLATILE);
/*
* Restore stdout.
*/
UnTieStdout(saved_stdout);
return (TCL_OK);
} /** End of 'cmdSystem' **/