update win32 port : use fulpath instead of a function, use correct temp dir value and correct open function according to the compiler

SVN revision: 32881
This commit is contained in:
doursse 2007-11-25 11:38:49 +00:00 committed by doursse
parent bc6caea1f9
commit 4317fe9155
2 changed files with 41 additions and 148 deletions

View File

@ -17,22 +17,24 @@
#include <ctype.h>
#include <time.h>
#include <dirent.h>
#ifdef WIN32
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# include <stdlib.h>
# include <stdio.h>
# include <errno.h>
#else
#include <dlfcn.h> /* dlopen,dlclose,etc */
#include <pwd.h>
#include <grp.h>
#include <glob.h>
#endif /* WIN32 */
# include <dlfcn.h> /* dlopen,dlclose,etc */
# include <pwd.h>
# include <grp.h>
# include <glob.h>
#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);

View File

@ -36,11 +36,12 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef WIN32
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#endif /* WIN32 */
#ifdef _WIN32
# include <fcntl.h>
# include <io.h>
# include <share.h>
# include <sys/stat.h>
#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);