tcc: don't use pstrcpy, fix win32 spanwn quoting

- we're now exporting tcc_prefixed symbols from libtcc only

- On windows, the msvcrt startup code would remove backslashes
  from commandline arguments such as
      -DFOO=\"foo\"
  which would appear in argv as
      -DFOO="foo"
  Therefor before passing these to spawnvp, we need to restore
  the backslashes.
This commit is contained in:
grischka 2017-02-08 19:49:28 +01:00
parent 68666eee2a
commit 90316c7c26
3 changed files with 57 additions and 25 deletions

View File

@ -133,7 +133,7 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
/********************************************************/ /********************************************************/
/* copy a string and truncate it. */ /* copy a string and truncate it. */
PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s) ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
{ {
char *q, *q_end; char *q, *q_end;
int c; int c;
@ -153,7 +153,7 @@ PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s)
} }
/* strcat and truncate. */ /* strcat and truncate. */
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s) ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
{ {
int len; int len;
len = strlen(buf); len = strlen(buf);
@ -162,7 +162,7 @@ PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s)
return buf; return buf;
} }
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num) ST_FUNC char *pstrncpy(char *out, const char *in, size_t num)
{ {
memcpy(out, in, num); memcpy(out, in, num);
out[num] = '\0'; out[num] = '\0';
@ -1157,7 +1157,7 @@ PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
{ {
int ret = tcc_add_library(s, libname); int ret = tcc_add_library(s, libname);
if (ret < 0) if (ret < 0)
tcc_error_noabort("library 'lib%s' not found", libname); tcc_error_noabort("library '%s' not found", libname);
return ret; return ret;
} }

68
tcc.c
View File

@ -153,9 +153,39 @@ static void help(void)
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
#ifdef _WIN32 #ifdef _WIN32
#include <process.h> #include <process.h>
static char *str_replace(const char *str, const char *p, const char *r)
{
const char *s, *s0;
char *d, *d0;
int sl, pl, rl;
sl = strlen(str);
pl = strlen(p);
rl = strlen(r);
for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
if (d) {
memcpy(d, s0, sl = s - s0), d += sl;
memcpy(d, r, rl), d += rl;
} else
sl += rl - pl;
}
if (d) {
strcpy(d, s0);
return d0;
}
}
}
static int execvp_win32(const char *prog, char **argv) static int execvp_win32(const char *prog, char **argv)
{ {
int ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv); int ret; char **p;
/* replace all " by \" */
for (p = argv; *p; ++p)
if (strchr(*p, '"'))
*p = str_replace(*p, "\"", "\\\"");
ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
if (-1 == ret) if (-1 == ret)
return ret; return ret;
cwait(&ret, ret, WAIT_CHILD); cwait(&ret, ret, WAIT_CHILD);
@ -165,28 +195,33 @@ static int execvp_win32(const char *prog, char **argv)
#endif #endif
static void exec_other_tcc(TCCState *s, char **argv, int option) static void exec_other_tcc(TCCState *s, char **argv, int option)
{ {
char child_path[4096], *child_name; const char *target; char child_path[4096], *a0 = argv[0]; const char *target;
int l;
switch (option) { switch (option) {
#ifdef TCC_TARGET_I386 #ifdef TCC_TARGET_I386
case 32: break; case 32: break;
case 64: target = "x86_64"; case 64: target = "x86_64";
#else #else
case 64: break; case 64: break;
case 32: target = "i386"; case 32: target = "i386";
#endif #endif
pstrcpy(child_path, sizeof child_path - 40, argv[0]); l = tcc_basename(a0) - a0;
child_name = tcc_basename(child_path); snprintf(child_path, sizeof child_path,
strcpy(child_name, target);
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
strcat(child_name, "-win32"); "%.*s%s-win32-tcc"
#else
"%.*s%s-tcc"
#endif #endif
strcat(child_name, "-tcc"); , l, a0, target);
if (strcmp(argv[0], child_path)) { if (strcmp(a0, child_path)) {
if (s->verbose > 0) if (s->verbose > 0)
printf("tcc: using '%s'\n", child_name), fflush(stdout); printf("tcc: using '%s'\n", child_path + l), fflush(stdout);
execvp(argv[0] = child_path, argv); execvp(argv[0] = child_path, argv);
} }
tcc_error("'%s' not found", child_name); tcc_error("'%s' not found", child_path + l);
} }
} }
#else #else
@ -196,15 +231,13 @@ static void exec_other_tcc(TCCState *s, char **argv, int option)
static void gen_makedeps(TCCState *s, const char *target, const char *filename) static void gen_makedeps(TCCState *s, const char *target, const char *filename)
{ {
FILE *depout; FILE *depout;
char buf[1024], *ext; char buf[1024];
int i; int i;
if (!filename) { if (!filename) {
/* compute filename automatically /* compute filename automatically: dir/file.o -> dir/file.d */
* dir/file.o -> dir/file.d */ snprintf(buf, sizeof buf, "%.*s.d",
pstrcpy(buf, sizeof(buf), target); (int)(tcc_fileextension(target) - target), target);
ext = tcc_fileextension(buf);
pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
filename = buf; filename = buf;
} }
@ -231,7 +264,7 @@ static char *default_outputfile(TCCState *s, const char *first_file)
if (first_file && strcmp(first_file, "-")) if (first_file && strcmp(first_file, "-"))
name = tcc_basename(first_file); name = tcc_basename(first_file);
pstrcpy(buf, sizeof(buf), name); snprintf(buf, sizeof(buf), "%s", name);
ext = tcc_fileextension(buf); ext = tcc_fileextension(buf);
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
if (s->output_type == TCC_OUTPUT_DLL) if (s->output_type == TCC_OUTPUT_DLL)
@ -245,7 +278,6 @@ static char *default_outputfile(TCCState *s, const char *first_file)
strcpy(ext, ".o"); strcpy(ext, ".o");
else else
strcpy(buf, "a.out"); strcpy(buf, "a.out");
return tcc_strdup(buf); return tcc_strdup(buf);
} }

6
tcc.h
View File

@ -1060,9 +1060,9 @@ ST_DATA int tcc_ext;
ST_DATA struct TCCState *tcc_state; ST_DATA struct TCCState *tcc_state;
/* public functions currently used by the tcc main function */ /* public functions currently used by the tcc main function */
PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s); ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s); ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num); ST_FUNC char *pstrncpy(char *out, const char *in, size_t num);
PUB_FUNC char *tcc_basename(const char *name); PUB_FUNC char *tcc_basename(const char *name);
PUB_FUNC char *tcc_fileextension (const char *name); PUB_FUNC char *tcc_fileextension (const char *name);