diff --git a/legacy/embryo/src/bin/embryo_cc_prefix.c b/legacy/embryo/src/bin/embryo_cc_prefix.c index b25d4fbba4..ae2f3b9cd3 100644 --- a/legacy/embryo/src/bin/embryo_cc_prefix.c +++ b/legacy/embryo/src/bin/embryo_cc_prefix.c @@ -17,22 +17,24 @@ #include #include #include -#ifdef WIN32 -#include -#include -#include -#include +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +# undef WIN32_LEAN_AND_MEAN +# include +# include +# include #else -#include /* dlopen,dlclose,etc */ -#include -#include -#include -#endif /* WIN32 */ +# include /* dlopen,dlclose,etc */ +# include +# include +# include +#endif /* _WIN32 */ #include "embryo_cc_prefix.h" /* FIXME: that hack is a temporary one. That code will be in MinGW soon */ -#ifdef WIN32 +#ifdef _WIN32 #define RTLD_LAZY 1 /* lazy function call binding */ #define RTLD_NOW 2 /* immediate function call binding */ @@ -98,130 +100,9 @@ char *dlerror (void) } } -char *realpath(const char *path, char resolved_path[PATH_MAX]) -{ - char *return_path = 0; +#define realpath(file_name, resolved_name) _fullpath((resolved_name), (file_name), PATH_MAX) - if (path) //Else EINVAL - { - if (resolved_path) - { - return_path = resolved_path; - } - else - { - //Non standard extension that glibc uses - return_path = malloc(PATH_MAX); - } - - if (return_path) //Else EINVAL - { - //This is a Win32 API function similar to what realpath() is supposed to do - size_t size = GetFullPathNameA(path, PATH_MAX, return_path, 0); - - //GetFullPathNameA() returns a size larger than buffer if buffer is too small - if (size > PATH_MAX) - { - if (return_path != resolved_path) //Malloc'd buffer - Unstandard extension retry - { - size_t new_size; - - free(return_path); - return_path = malloc(size); - - if (return_path) - { - new_size = GetFullPathNameA(path, size, return_path, 0); //Try again - - if (new_size > size) //If it's still too large, we have a problem, don't try again - { - free(return_path); - return_path = 0; - errno = ENAMETOOLONG; - } - else - { - size = new_size; - } - } - else - { - //I wasn't sure what to return here, but the standard does say to return EINVAL - //if resolved_path is null, and in this case we couldn't malloc large enough buffer - errno = EINVAL; - } - } - else //resolved_path buffer isn't big enough - { - return_path = 0; - errno = ENAMETOOLONG; - } - } - - //GetFullPathNameA() returns 0 if some path resolve problem occured - if (!size) - { - if (return_path != resolved_path) //Malloc'd buffer - { - free(return_path); - } - - return_path = 0; - - //Convert MS errors into standard errors - switch (GetLastError()) - { - case ERROR_FILE_NOT_FOUND: - errno = ENOENT; - break; - - case ERROR_PATH_NOT_FOUND: case ERROR_INVALID_DRIVE: - errno = ENOTDIR; - break; - - case ERROR_ACCESS_DENIED: - errno = EACCES; - break; - - default: //Unknown Error - errno = EIO; - break; - } - } - - //If we get to here with a valid return_path, we're still doing good - if (return_path) - { - struct stat stat_buffer; - - //Make sure path exists, stat() returns 0 on success - if (stat(return_path, &stat_buffer)) - { - if (return_path != resolved_path) - { - free(return_path); - } - - return_path = 0; - //stat() will set the correct errno for us - } - //else we succeeded! - } - } - else - { - errno = EINVAL; - } - } - else - { - errno = EINVAL; - } - - return return_path; -} - -#endif /* WIN32 */ +#endif /* _WIN32 */ /* local subsystem functions */ static int _e_prefix_share_hunt(void); diff --git a/legacy/embryo/src/bin/embryo_cc_sc1.c b/legacy/embryo/src/bin/embryo_cc_sc1.c index a2d28872a4..1f2073cbc8 100644 --- a/legacy/embryo/src/bin/embryo_cc_sc1.c +++ b/legacy/embryo/src/bin/embryo_cc_sc1.c @@ -36,11 +36,12 @@ #include #include #include -#ifdef WIN32 -#include -#include -#include -#endif /* WIN32 */ +#ifdef _WIN32 +# include +# include +# include +# include +#endif /* _WIN32 */ #include "embryo_cc_osdefs.h" #include "embryo_cc_sc.h" @@ -291,7 +292,7 @@ sc_compile(int argc, char *argv[]) void *inpfmark; char lcl_ctrlchar; int lcl_packstr, lcl_needsemicolon, lcl_tabsize; - char *tmpdir; + char *tmpdir; /* set global variables to their initial value */ binf = NULL; @@ -316,20 +317,31 @@ sc_compile(int argc, char *argv[]) setopt(argc, argv, inpfname, binfname, incfname, reportname); /* open the output file */ + +#ifndef _WIN32 tmpdir = getenv("TMPDIR"); if (!tmpdir) tmpdir = "/tmp"; snprintf(outfname, _MAX_PATH, "%s/embryo_cc.asm-tmp-XXXXXX", tmpdir); -#ifndef WIN32 fd_out = mkstemp(outfname); #else - if (mktemp (outfname)) - do - fd_out = open (outfname, O_CREAT | O_EXCL, S_IREAD | S_IWRITE); - while (!(fd_out == -1 && errno == EEXIST) && mktemp (outfname)); - else - fd_out = -1; -#endif /* WIN32 */ + tmpdir = getenv("TMP"); + if (!tmpdir) tmpdir = getenv("TEMP"); + if (!tmpdir) tmpdir = getenv("USERPROFILE"); + if (!tmpdir) tmpdir = getenv("WINDIR"); + if (!tmpdir) error(101, "embryo_cc.asm-tmp-XXXXXX (unable to get a valid temp path)"); + + snprintf(outfname, _MAX_PATH, "%s/embryo_cc.asm-tmp-XXXXXX", tmpdir); +# ifdef __MINGW32__ + if (!mktemp(outfname)) + error(101, outfname); + fd_out = _sopen(outfname, _O_RDWR | _O_BINARY | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); +# else + if (_mktemp_s(outfname, _MAX_PATH)) + error(101, outfname); + _sopen_s(&fd_out, outfname, _O_RDWR | _O_BINARY | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); +# endif /* __MINGW32__ */ +#endif /* _WIN32 */ if (fd_out < 0) error(101, outfname);