Lines 1-303
Link Here
|
1 |
/* |
1 |
/* |
2 |
* Sun Public License Notice |
2 |
* Sun Public License Notice |
3 |
* |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
7 |
* http://www.sun.com/ |
8 |
* |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
12 |
*/ |
13 |
|
13 |
|
14 |
/* $Id: rmid_wrapper.cpp,v 1.2 2001/02/19 14:24:33 mryzl Exp $ */ |
14 |
/* $Id: rmid_wrapper.cpp,v 1.3 2002/07/18 13:07:49 tzezula Exp $ */ |
15 |
|
15 |
|
16 |
#include <windows.h> |
16 |
#include "stdafx.h" |
17 |
#include <stdio.h> |
17 |
|
18 |
#include <stdlib.h> |
18 |
#include <windows.h> |
19 |
#include <io.h> |
19 |
#include <stdio.h> |
20 |
#include <fcntl.h> |
20 |
#include <stdlib.h> |
21 |
#include <process.h> |
21 |
#include <io.h> |
22 |
#include <commdlg.h> |
22 |
#include <fcntl.h> |
23 |
#include <signal.h> |
23 |
#include <process.h> |
24 |
|
24 |
#include <commdlg.h> |
25 |
#define RMID_MAIN_CLASS "org.netbeans.rmi.RMIDWrapperImpl" |
25 |
#include <signal.h> |
26 |
#define JDK_KEY "Software\\JavaSoft\\Java Development Kit" |
26 |
|
27 |
//#define IDE_KEY "Software\\netbeans.org\\NetBeans IDE" |
27 |
#define RMID_MAIN_CLASS "org.netbeans.rmi.RMIDWrapperImpl" |
28 |
#define IDE_KEY "Software\\Sun Microsystems, Inc.\\Forte for Java" |
28 |
#define JDK_KEY "Software\\JavaSoft\\Java Development Kit" |
29 |
|
29 |
//#define IDE_KEY "Software\\netbeans.org\\NetBeans IDE" |
30 |
// #define DEBUG |
30 |
#define IDE_KEY "Software\\Sun Microsystems, Inc.\\Forte for Java" |
31 |
|
31 |
#define ESCAPE '\\' |
32 |
static char jdkhome[MAX_PATH]; |
32 |
#define DQUOTE '\"' |
33 |
static char idehome[MAX_PATH]; |
33 |
#define QUOTE '\'' |
34 |
static char classpath[1024 * 16]; |
34 |
#define SPACE ' ' |
35 |
|
35 |
|
36 |
static void addToClassPath(const char *pathprefix, const char *path); |
36 |
// #define DEBUG |
37 |
static int findJdkFromRegistry(const char* keyname, char jdkhome[]); |
37 |
|
38 |
static void setIDEHome(char idehome[]); |
38 |
static char jdkhome[MAX_PATH]; |
39 |
static int processOptions(int argc, char **argv, char ***cmd); |
39 |
static char idehome[MAX_PATH]; |
40 |
static void UserHandler(int sig); |
40 |
static char classpath[1024 * 16]; |
41 |
|
41 |
|
42 |
|
42 |
static void addToClassPath(const char *pathprefix, const char *path); |
43 |
static void sysSignal(int sig, void (*func) ( int sig )) { |
43 |
static int findJdkFromRegistry(const char* keyname, char jdkhome[]); |
44 |
signal(sig, (void (*)(int)) func); |
44 |
static void setIDEHome(char idehome[]); |
45 |
} |
45 |
static int processOptions(int argc, char **argv, char ***cmd); |
46 |
|
46 |
static void UserHandler(int sig); |
47 |
|
47 |
static unsigned int getLength (char* str); |
48 |
/** |
48 |
static char* createCmd (char* cmd, char** params); |
49 |
* CTRL+Break handler |
49 |
|
50 |
*/ |
50 |
|
51 |
static void UserHandler(int sig) |
51 |
static void sysSignal(int sig, void (*func) ( int sig )) { |
52 |
{ |
52 |
signal(sig, (void (*)(int)) func); |
53 |
// ignore it |
53 |
} |
54 |
/* We need to reinstate the signal handler each time... */ |
54 |
|
55 |
sysSignal(sig, UserHandler); |
55 |
|
56 |
} |
56 |
/** |
57 |
|
57 |
* CTRL+Break handler |
58 |
int main(int argc, char *argv[]) { |
58 |
*/ |
59 |
|
59 |
static void UserHandler(int sig) |
60 |
int status; |
60 |
{ |
61 |
char buf[1024]; |
61 |
// ignore it |
62 |
char **cmd; |
62 |
/* We need to reinstate the signal handler each time... */ |
63 |
|
63 |
sysSignal (sig, UserHandler); |
64 |
sysSignal(SIGBREAK, UserHandler); |
64 |
} |
65 |
|
65 |
|
66 |
setIDEHome(idehome); |
66 |
int main(int argc, char *argv[]) { |
67 |
findJdkFromRegistry(JDK_KEY, jdkhome); |
67 |
long status; |
68 |
|
68 |
char buf[1024]; |
69 |
// printf("%s, %s\n", idehome, jdkhome); |
69 |
char **cmd; |
70 |
processOptions(argc, argv, &cmd); |
70 |
STARTUPINFO start; |
71 |
strcpy(buf, jdkhome); |
71 |
PROCESS_INFORMATION child; |
72 |
strcat(buf, "\\bin\\java.exe"); |
72 |
ZeroMemory( &start, sizeof(start) ); |
73 |
|
73 |
start.cb = sizeof(start); |
74 |
#ifdef DEBUG |
74 |
ZeroMemory( &child, sizeof(child) ); |
75 |
printf("process: %s\n", buf); |
75 |
|
76 |
printf("arguments: "); |
76 |
sysSignal(SIGBREAK, UserHandler); |
77 |
for(int i = 0; cmd[i] != NULL; i++) { |
77 |
|
78 |
if (i != 0) printf(", "); |
78 |
setIDEHome(idehome); |
79 |
printf("%s", cmd[i]); |
79 |
findJdkFromRegistry(JDK_KEY, jdkhome); |
80 |
} |
80 |
|
81 |
printf("\n"); |
81 |
// printf("%s, %s\n", idehome, jdkhome); |
82 |
#endif DEBUG |
82 |
processOptions(argc, argv, &cmd); |
83 |
|
83 |
strcpy(buf, jdkhome); |
84 |
status = spawnv(_P_WAIT, buf, cmd); |
84 |
strcat(buf, "\\bin\\java.exe"); |
85 |
// execv(buf, cmd); |
85 |
|
86 |
if (status == -1) { |
86 |
#ifdef DEBUG |
87 |
perror(buf); |
87 |
fprintf(stderr, "process: %s\n", buf); |
88 |
} |
88 |
fprintf(stderr, "arguments: "); |
89 |
return status; |
89 |
for(int i = 0; cmd[i] != NULL; i++) { |
90 |
} |
90 |
if (i != 0) fprintf(stderr, ", "); |
91 |
|
91 |
fprintf(stderr, "%s", cmd[i]); |
92 |
|
92 |
} |
93 |
/* |
93 |
printf("\n"); |
94 |
* Returns string data for the specified registry value name, or |
94 |
#endif DEBUG |
95 |
* NULL if not found. |
95 |
char* cmdLine = createCmd (buf, cmd); |
96 |
*/ |
96 |
#ifdef DEBUG |
97 |
static char * GetStringValue(HKEY key, const char *name) |
97 |
fprintf (stderr, "commandLine: %s", cmdLine); |
98 |
{ |
98 |
#endif // DEBUG |
99 |
DWORD type, size; |
99 |
status = CreateProcess (NULL,cmdLine,NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &start, &child); |
100 |
char *value = 0; |
100 |
WaitForSingleObject (child.hProcess, INFINITE); |
101 |
|
101 |
// execv(buf, cmd); |
102 |
if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 && type == REG_SZ) { |
102 |
if (status == -1) { |
103 |
value = (char*) malloc(size); |
103 |
perror(buf); |
104 |
if (RegQueryValueEx(key, name, 0, 0, (unsigned char*)value, &size) != 0) { |
104 |
} |
105 |
free(value); |
105 |
free ((void*) cmdLine); |
106 |
value = 0; |
106 |
return status; |
107 |
} |
107 |
} |
108 |
} |
108 |
|
109 |
return value; |
109 |
/* |
110 |
} |
110 |
* Returns string data for the specified registry value name, or |
111 |
|
111 |
* NULL if not found. |
112 |
static int findJdkFromRegistry(const char* keyname, char jdkhome[]) |
112 |
*/ |
113 |
{ |
113 |
static char * GetStringValue(HKEY key, const char *name) |
114 |
HKEY hkey = NULL, subkey = NULL; |
114 |
{ |
115 |
char *ver = NULL; |
115 |
DWORD type, size; |
116 |
int rc = 1; |
116 |
char *value = 0; |
117 |
|
117 |
|
118 |
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &hkey) == 0) { |
118 |
if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 && type == REG_SZ) { |
119 |
ver = GetStringValue(hkey, "CurrentVersion"); |
119 |
value = (char*) malloc(size); |
120 |
if (ver == NULL) |
120 |
if (RegQueryValueEx(key, name, 0, 0, (unsigned char*)value, &size) != 0) { |
121 |
goto quit; |
121 |
free(value); |
122 |
|
122 |
value = 0; |
123 |
if (RegOpenKeyEx(hkey, ver, 0, KEY_READ, &subkey) == 0) { |
123 |
} |
124 |
char *home = GetStringValue(subkey, "JavaHome"); |
124 |
} |
125 |
if (home == NULL) |
125 |
return value; |
126 |
goto quit; |
126 |
} |
127 |
strcpy(jdkhome, home); |
127 |
|
128 |
free(home); |
128 |
static int findJdkFromRegistry(const char* keyname, char jdkhome[]) |
129 |
rc = 0; |
129 |
{ |
130 |
} |
130 |
HKEY hkey = NULL, subkey = NULL; |
131 |
} |
131 |
char *ver = NULL; |
132 |
|
132 |
int rc = 1; |
133 |
quit: |
133 |
|
134 |
if (ver != NULL) |
134 |
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &hkey) == 0) { |
135 |
free(ver); |
135 |
ver = GetStringValue(hkey, "CurrentVersion"); |
136 |
if (subkey != NULL) |
136 |
if (ver == NULL) |
137 |
RegCloseKey(subkey); |
137 |
goto quit; |
138 |
if (hkey != NULL) |
138 |
|
139 |
RegCloseKey(hkey); |
139 |
if (RegOpenKeyEx(hkey, ver, 0, KEY_READ, &subkey) == 0) { |
140 |
return rc; |
140 |
char *home = GetStringValue(subkey, "JavaHome"); |
141 |
} |
141 |
if (home == NULL) |
142 |
|
142 |
goto quit; |
143 |
void addToClassPath(const char *pathprefix, const char *path) { |
143 |
strcpy(jdkhome, home); |
144 |
char buf[1024]; |
144 |
free(home); |
145 |
|
145 |
rc = 0; |
146 |
strcpy(buf, pathprefix); |
146 |
} |
147 |
if (path != NULL) |
147 |
} |
148 |
strcat(strcat(buf, "\\"), path); |
148 |
|
149 |
|
149 |
quit: |
150 |
if (classpath[0] != '\0') |
150 |
if (ver != NULL) |
151 |
strcat(classpath, ";"); |
151 |
free(ver); |
152 |
strcat(classpath, buf); |
152 |
if (subkey != NULL) |
153 |
} |
153 |
RegCloseKey(subkey); |
154 |
|
154 |
if (hkey != NULL) |
155 |
/** Set IDE home. Get the whole process name and strip |
155 |
RegCloseKey(hkey); |
156 |
* the trailig \bin\<name> |
156 |
return rc; |
157 |
*/ |
157 |
} |
158 |
static void setIDEHome(char idehome[]) { |
158 |
|
159 |
char exepath[1024 * 4]; |
159 |
void addToClassPath(const char *pathprefix, const char *path) { |
160 |
char buf[1024 * 4], *pc; |
160 |
char buf[1024]; |
161 |
|
161 |
|
162 |
GetModuleFileName(0, buf, sizeof buf); |
162 |
strcpy(buf, pathprefix); |
163 |
strcpy(exepath, buf); |
163 |
if (path != NULL) |
164 |
|
164 |
strcat(strcat(buf, "\\"), path); |
165 |
pc = strrchr(buf, '\\'); |
165 |
|
166 |
if (pc != NULL) { // always holds |
166 |
if (classpath[0] != '\0') |
167 |
*pc = '\0'; // remove .exe filename |
167 |
strcat(classpath, ";"); |
168 |
} |
168 |
strcat(classpath, buf); |
169 |
|
169 |
} |
170 |
// remove \bin |
170 |
|
171 |
pc = strrchr(buf, '\\'); |
171 |
/** Set IDE home. Get the whole process name and strip |
172 |
if (pc != NULL && 0 == stricmp("\\bin", pc)) { |
172 |
* the trailig \bin\<name> |
173 |
*pc = '\0'; |
173 |
*/ |
174 |
} |
174 |
static void setIDEHome(char idehome[]) { |
175 |
strcpy(idehome, buf); |
175 |
char exepath[1024 * 4]; |
176 |
} |
176 |
char buf[1024 * 4], *pc; |
177 |
|
177 |
|
178 |
|
178 |
GetModuleFileName(0, buf, sizeof buf); |
179 |
/** Process arguments. |
179 |
strcpy(exepath, buf); |
180 |
|
180 |
|
181 |
-J options will be passed to java |
181 |
pc = strrchr(buf, '\\'); |
182 |
the rest will remain and will processed by rmid |
182 |
if (pc != NULL) { // always holds |
183 |
|
183 |
*pc = '\0'; // remove .exe filename |
184 |
some exceptions |
184 |
} |
185 |
|
185 |
|
186 |
-Xms8m - |
186 |
// remove \bin |
187 |
-jdkhome - obtain programatically and delete from list of options |
187 |
pc = strrchr(buf, '\\'); |
188 |
-J-Djava.security.policy= - rmid.policy |
188 |
if (pc != NULL && 0 == stricmp("\\bin", pc)) { |
189 |
// -log - ../tmp/ |
189 |
*pc = '\0'; |
190 |
// -J-Dsun.rmi.server.activation.debugExec - default to true |
190 |
} |
191 |
-J-classpath - add library to the classpath |
191 |
strcpy(idehome, buf); |
192 |
|
192 |
} |
193 |
|
193 |
|
194 |
*/ |
194 |
|
195 |
static int processOptions(int argc, char **argv, char ***cmd) { |
195 |
/** Process arguments. |
196 |
int jac = 0, rac = 0, i, j; |
196 |
|
197 |
char *jav[64], *rav[64], *arg, **cmd2; |
197 |
-J options will be passed to java |
198 |
int cpi = -1, spi = -1, nbi = -1, jdkhomeFlag = 0, cpFlag = 0; |
198 |
the rest will remain and will processed by rmid |
199 |
char buf[1024]; |
199 |
|
200 |
|
200 |
some exceptions |
201 |
jav[jac++] = "-ms8m"; |
201 |
|
202 |
jav[jac++] = "-Dsun.rmi.server.activation.debugExec=true"; |
202 |
-Xms8m - |
203 |
|
203 |
-jdkhome - obtain programatically and delete from list of options |
204 |
for(i = 1; i < argc; i++) { |
204 |
-J-Djava.security.policy= - rmid.policy |
205 |
arg = argv[i]; |
205 |
// -log - ../tmp/ |
206 |
if ((arg[0] == '-') && (arg[1] == 'J')) { |
206 |
// -J-Dsun.rmi.server.activation.debugExec - default to true |
207 |
arg += 2; |
207 |
-J-classpath - add library to the classpath |
208 |
jav[jac] = arg; |
208 |
|
209 |
|
209 |
|
210 |
if (cpFlag) { |
210 |
*/ |
211 |
cpFlag = 0; |
211 |
static int processOptions(int argc, char **argv, char ***cmd) { |
212 |
} |
212 |
int jac = 0, rac = 0, i, j; |
213 |
|
213 |
char *jav[64], *rav[64], *arg, **cmd2; |
214 |
if (strncmp(arg, "-Djava.security.policy=", 23) == 0) { |
214 |
int cpi = -1, spi = -1, nbi = -1, jdkhomeFlag = 0, cpFlag = 0; |
215 |
spi = jac; |
215 |
char buf[1024]; |
216 |
} |
216 |
|
217 |
|
217 |
jav[jac++] = "-ms8m"; |
218 |
if (strncmp(arg, "-Dnetbeans.home=", 16) == 0) { |
218 |
jav[jac++] = "-Dsun.rmi.server.activation.debugExec=true"; |
219 |
nbi = jac; |
219 |
|
220 |
} |
220 |
for(i = 1; i < argc; i++) { |
221 |
|
221 |
arg = argv[i]; |
222 |
if (strcmp(arg, "-classpath") == 0) { |
222 |
if ((arg[0] == '-') && (arg[1] == 'J')) { |
223 |
cpi = jac; |
223 |
arg += 2; |
224 |
cpFlag = 1; |
224 |
jav[jac] = arg; |
225 |
} |
225 |
|
226 |
|
226 |
if (cpFlag) { |
227 |
jac++; |
227 |
cpFlag = 0; |
228 |
} else { |
228 |
} |
229 |
|
229 |
|
230 |
if (jdkhomeFlag) { |
230 |
if (strncmp(arg, "-Djava.security.policy=", 23) == 0) { |
231 |
strcpy(jdkhome, arg); |
231 |
spi = jac; |
232 |
jdkhomeFlag = 0; |
232 |
} |
233 |
continue; |
233 |
|
234 |
} |
234 |
if (strncmp(arg, "-Dnetbeans.home=", 16) == 0) { |
235 |
|
235 |
nbi = jac; |
236 |
if (strcmp(arg, "-jdkhome") == 0) { |
236 |
} |
237 |
jdkhomeFlag = 1; |
237 |
|
238 |
} else { |
238 |
if (strcmp(arg, "-classpath") == 0) { |
239 |
rav[rac++] = arg; |
239 |
cpi = jac; |
240 |
} |
240 |
cpFlag = 1; |
241 |
} |
241 |
} |
242 |
} |
242 |
|
243 |
|
243 |
jac++; |
244 |
if (jdkhomeFlag) { |
244 |
} else { |
245 |
jdkhome[0] = 0; |
245 |
|
246 |
} |
246 |
if (jdkhomeFlag) { |
247 |
|
247 |
strcpy(jdkhome, arg); |
248 |
// add policy file |
248 |
jdkhomeFlag = 0; |
249 |
if (spi == -1) { |
249 |
continue; |
250 |
jav[jac++] = "-Djava.security.policy=rmid.policy"; |
250 |
} |
251 |
} |
251 |
|
252 |
|
252 |
if (strcmp(arg, "-jdkhome") == 0) { |
253 |
if (nbi == -1) { |
253 |
jdkhomeFlag = 1; |
254 |
strcpy(buf, "-Dnetbeans.home="); |
254 |
} else { |
255 |
strcat(buf, idehome); |
255 |
rav[rac++] = arg; |
256 |
jav[jac++] = strdup(buf); |
256 |
} |
257 |
} else { |
257 |
} |
258 |
char *p = strchr(jav[nbi], '='); |
258 |
} |
259 |
if (p != NULL) { |
259 |
|
260 |
strcpy(idehome, p + 1); |
260 |
if (jdkhomeFlag) { |
261 |
} |
261 |
jdkhome[0] = 0; |
262 |
} |
262 |
} |
263 |
|
263 |
|
264 |
// update classpath |
264 |
// add policy file |
265 |
if (cpi == -1) { |
265 |
if (spi == -1) { |
266 |
jav[jac++] = "-classpath"; |
266 |
jav[jac++] = "-Djava.security.policy=rmid.policy"; |
267 |
strcpy(buf, idehome); |
267 |
} |
268 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
268 |
|
269 |
jav[jac++] = strdup(buf); |
269 |
if (nbi == -1) { |
270 |
} else if (cpFlag) { |
270 |
strcpy(buf, "-Dnetbeans.home="); |
271 |
strcpy(buf, idehome); |
271 |
strcat(buf, idehome); |
272 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
272 |
jav[jac++] = strdup(buf); |
273 |
jav[jac++] = strdup(buf); |
273 |
} else { |
274 |
} else { |
274 |
char *p = strchr(jav[nbi], '='); |
275 |
strcpy(buf, jav[cpi + 1]); |
275 |
if (p != NULL) { |
276 |
strcat(buf, ";"); |
276 |
strcpy(idehome, p + 1); |
277 |
strcat(buf, idehome); |
277 |
} |
278 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
278 |
} |
279 |
jav[cpi + 1] = strdup(buf); |
279 |
|
280 |
} |
280 |
// update classpath |
281 |
|
281 |
if (cpi == -1) { |
282 |
// add ide.home |
282 |
jav[jac++] = "-classpath"; |
283 |
|
283 |
strcpy(buf, idehome); |
284 |
argc = jac + rac + 3; // process name, class, NULL |
284 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
285 |
cmd2 = *cmd = (char **) malloc(argc * sizeof(char *)); |
285 |
jav[jac++] = strdup(buf); |
286 |
|
286 |
} else if (cpFlag) { |
287 |
cmd2[0] = argv[0]; |
287 |
strcpy(buf, idehome); |
288 |
j = 1; |
288 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
289 |
for(i = 0; i < jac; i++) { |
289 |
jav[jac++] = strdup(buf); |
290 |
cmd2[j++] = jav[i]; |
290 |
} else { |
291 |
} |
291 |
strcpy(buf, jav[cpi + 1]); |
292 |
cmd2[j++] = RMID_MAIN_CLASS; |
292 |
strcat(buf, ";"); |
293 |
for(i = 0; i < rac; i++) { |
293 |
strcat(buf, idehome); |
294 |
cmd2[j++] = rav[i]; |
294 |
strcat(buf, "\\lib\\ext\\rmi-ext.jar"); |
295 |
} |
295 |
jav[cpi + 1] = strdup(buf); |
296 |
cmd2[j] = NULL; |
296 |
} |
297 |
|
297 |
|
298 |
// test |
298 |
// add ide.home |
299 |
// for(i = 0; i < argc; i++) printf("%s\n", cmd2[i]); |
299 |
|
300 |
|
300 |
argc = jac + rac + 3; // process name, class, NULL |
301 |
return 0; |
301 |
cmd2 = *cmd = (char **) malloc(argc * sizeof(char *)); |
302 |
} |
302 |
|
303 |
|
303 |
cmd2[0] = argv[0]; |
304 |
-- |
304 |
j = 1; |
|
|
305 |
for(i = 0; i < jac; i++) { |
306 |
cmd2[j++] = jav[i]; |
307 |
} |
308 |
cmd2[j++] = RMID_MAIN_CLASS; |
309 |
for(i = 0; i < rac; i++) { |
310 |
cmd2[j++] = rav[i]; |
311 |
} |
312 |
cmd2[j] = NULL; |
313 |
|
314 |
// test |
315 |
// for(i = 0; i < argc; i++) printf("%s\n", cmd2[i]); |
316 |
|
317 |
return 0; |
318 |
} |
319 |
|
320 |
static char* createCmd (char* cmd, char** params) { |
321 |
int length = 1; // Ending NULL |
322 |
int i,j,k; |
323 |
char* arg; |
324 |
length += (getLength (cmd)-1); |
325 |
for (i=0; params[i]; i++) |
326 |
length += getLength (params[i]); |
327 |
char* cmdLine = (char*) malloc (length); |
328 |
k=0; |
329 |
cmdLine[k++] = DQUOTE; |
330 |
for (j=0; cmd[j]; j++) { |
331 |
cmdLine[k++] = cmd[j]; |
332 |
} |
333 |
cmdLine[k++] = DQUOTE; |
334 |
for (i=1; params[i]; i++) { |
335 |
arg = params[i]; |
336 |
cmdLine[k++] = SPACE; |
337 |
cmdLine[k++] = DQUOTE; |
338 |
for (j=0; arg[j]; j++) { |
339 |
cmdLine[k++] = arg[j]; |
340 |
} |
341 |
cmdLine[k++] = DQUOTE; |
342 |
} |
343 |
cmdLine[k] = 0x0; |
344 |
return cmdLine; |
345 |
} |
346 |
|
347 |
static unsigned int getLength (char* str) { |
348 |
unsigned int length = 3 + strlen (str); // twice " and SPACE |
349 |
return length; |
350 |
} |
351 |
|
352 |
|