Skip to content

Commit 04ab7e6

Browse files
committed
fixed bugs and errors
dln_search_exe_r now automatically appends .exe if the program searched for doesn't end in .exe while searching spawn now properly uses the provided module rather than executing everything in a cmd/sh module fixed piping of modules not called via cmd/sh
1 parent 2ff87e3 commit 04ab7e6

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

src/dln.c

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,31 @@
3636
static char *dln_find_1(const char *fname, const char *path, char *buf, size_t size, int exe_flag);
3737
char* dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size);
3838
char* dln_find_file_r(const char *fname, const char *path, char *buf, size_t size);
39+
char * stringReplace(char *search, char *replace, char *string);
40+
3941

4042
char*
4143
dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size)
4244
{
4345
char *envpath = 0;
46+
char working_copy[80];
47+
48+
49+
/* TODO This is a workaround to stop dln_fin_exe_r from returning
50+
e.g. "echo %MYVAR% > tmp/spawn.txt" AS ABSOLUTE PATH
51+
TO EXECUTABLE "echo %MYVAR% > tmp/spawn.txt" because that's total nonsense
52+
and a cause for errors
53+
On second thought, this would also eliminate any path with included whitespaces*/
54+
// if(strrchr(fname, ' '))
55+
// return NULL;
56+
57+
strcpy(working_copy,fname);
58+
59+
60+
#if !defined(__APPLE__) || !defined(__linux__)
61+
if(!strstr(working_copy, ".exe"))
62+
strcat(working_copy, ".exe");
63+
#endif
4464

4565
if (!path) {
4666
path = getenv(PATH_ENV);
@@ -57,11 +77,15 @@ dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size)
5777
".";
5878
}
5979

60-
buf = dln_find_1(fname, path, buf, size, 1);
80+
buf = dln_find_1(working_copy, path, buf, size, 1);
6181

6282
if (envpath)
6383
free(envpath);
64-
84+
// printf("first buf %s\n", buf);
85+
// #if !defined(__APPLE__) && !defined(__linux__)
86+
// stringReplace("/", "\\", buf);
87+
// #endif
88+
// printf("secon buf %s\n", buf);
6589
return buf;
6690
}
6791

@@ -255,3 +279,42 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size, int exe
255279
/* otherwise try the next component in the search path */
256280
}
257281
}
282+
283+
284+
285+
char * stringReplace(char *search, char *replace, char *string) {
286+
char *tempString, *searchStart;
287+
int len=0;
288+
289+
290+
// preuefe ob Such-String vorhanden ist
291+
searchStart = strstr(string, search);
292+
if(searchStart == NULL) {
293+
return string;
294+
}
295+
296+
// Speicher reservieren
297+
tempString = (char*) malloc(strlen(string) * sizeof(char));
298+
if(tempString == NULL) {
299+
return NULL;
300+
}
301+
302+
// temporaere Kopie anlegen
303+
strcpy(tempString, string);
304+
305+
// ersten Abschnitt in String setzen
306+
len = searchStart - string;
307+
string[len] = '\0';
308+
309+
// zweiten Abschnitt anhaengen
310+
strcat(string, replace);
311+
312+
// dritten Abschnitt anhaengen
313+
len += strlen(search);
314+
strcat(string, (char*)tempString+len);
315+
316+
// Speicher freigeben
317+
free(tempString);
318+
319+
return string;
320+
}

src/internal.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mrb_execarg_fill(mrb_state *mrb, mrb_value env, mrb_value *argv, mrb_int argc, m
8585
char **result;
8686
char *shell;
8787
const char *tCmd, *fCmd;
88-
char buf[80];
88+
char buf[80];
8989
mrb_value argv0 = mrb_nil_value();
9090

9191
ai = mrb_gc_arena_save(mrb);
@@ -101,12 +101,17 @@ mrb_execarg_fill(mrb_state *mrb, mrb_value env, mrb_value *argv, mrb_int argc, m
101101
tCmd = mrb_string_value_ptr(mrb, argv[0]);
102102
fCmd = dln_find_exe_r(tCmd, 0, buf, sizeof(buf));
103103

104+
104105
do_exit = !fCmd && strncmp("exit", tCmd, 4) == 0;
105106
use_cmd = (!strrchr(tCmd, ' ') && (fCmd || (!do_exit && argc > 1))) ? 1 : 0;
106107

108+
use_cmd = use_cmd && fCmd;
109+
107110
if (use_cmd) {
108111
result = (char **)mrb_malloc(mrb, sizeof(char *) * (argc + 1));
109112
mrb_execarg_argv_to_strv(mrb, argv, argc, result);
113+
// result[0] = fCmd;
114+
// result[1] =
110115
} else {
111116
argc = 3;
112117
result = (char **)mrb_malloc(mrb, sizeof(char *) * (argc + 1));
@@ -190,7 +195,6 @@ mrb_execarg_argv_to_strv(mrb_state *mrb, mrb_value *argv, mrb_int len, char **re
190195

191196
for (i = 0; i < len; i++) {
192197
buf = (char *)mrb_string_value_cstr(mrb, &argv[i]);
193-
194198
*result = buf;
195199
result++;
196200
}

src/win32.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,12 @@ spawnv(const char *shell, char *const argv[], mrb_value in, mrb_value out, mrb_v
372372
WCHAR *wcmd, *wshell;
373373
pid_t ret = -1;
374374
char *cmd = argv_to_str(argv);
375-
char tCmd[strlen(cmd)];
376375
char tShell[strlen(shell)];
377376
HANDLE input, output, error;
378377

379-
strcpy(tCmd,cmd);
380378
strcpy(tShell,shell);
381379

380+
382381
if(mrb_cptr_p(in)){
383382
input = mrb_cptr(in);
384383
}
@@ -393,7 +392,8 @@ spawnv(const char *shell, char *const argv[], mrb_value in, mrb_value out, mrb_v
393392

394393

395394
wshell = str_to_wstr(tShell, strlen(tShell));
396-
wcmd = str_to_wstr(tCmd, strlen(tCmd));
395+
wcmd = str_to_wstr(cmd, strlen(cmd));
396+
397397

398398
ret = child_result(CreateChild(wshell, wcmd, NULL, input, output, error, 0, NULL), P_NOWAIT);
399399

@@ -489,7 +489,7 @@ CreateChild(const WCHAR *shell, const WCHAR *cmd, SECURITY_ATTRIBUTES *psa,
489489
return NULL;
490490
}
491491

492-
fRet = CreateProcessW(shell, (WCHAR*) cmd, psa, psa,
492+
fRet = CreateProcessW(shell, cmd, psa, psa,
493493
psa->bInheritHandle, dwCreationFlags, env, NULL,
494494
&aStartupInfo, &aProcessInformation);
495495

test/process.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ def assert_windows(*args, &block)
151151
wait_for_pid(pid)
152152

153153
assert_equal var * 2, read('tmp/pipe.txt')
154+
155+
pid = spawn("readelf", "-v", out: pip)
156+
157+
wait_for_pid(pid)
158+
159+
assert_include read('tmp/pipe.txt'), 'GNU readelf (GNU Binutils)'
160+
161+
# assert_equal var * 2, read('tmp/pipe.txt')
154162
ensure
155163
IO._sysclose(pip) if OS.posix?
156164
end

0 commit comments

Comments
 (0)