X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fruntime%2Fwrap.c;h=5f1873861ff765d228566564503db4107298affd;hb=07ab1e4811ab16f95a9a5e8d767426a0787f22c0;hp=a510702172e95c46b9ca34e416843d1c15e484b1;hpb=8a632c14b592472873cfb214239c9387bc1a1ced;p=sbcl.git diff --git a/src/runtime/wrap.c b/src/runtime/wrap.c index a510702..5f18738 100644 --- a/src/runtime/wrap.c +++ b/src/runtime/wrap.c @@ -34,6 +34,8 @@ #include #include #include +#include + #ifndef LISP_FEATURE_WIN32 #include #include @@ -43,7 +45,6 @@ #if defined(LISP_FEATURE_WIN32) #define WIN32_LEAN_AND_MEAN -#include #include #endif @@ -282,17 +283,33 @@ fstat_wrapper(int filedes, struct stat_wrapper *buf) return ret; } -/* A wrapper for mkstemp(3), which seems not to exist on Windows. */ -int sb_mkstemp (char *template) { +/* A wrapper for mkstemp(3), for two reasons: (1) mkstemp does not + exist on Windows; (2) by passing down a mode_t, we don't need a + binding to chmod in SB-UNIX, and need not concern ourselves with + umask issues if we want to use mkstemp to make new files in + OPEN. */ +int sb_mkstemp (char *template, mode_t mode) { #ifdef LISP_FEATURE_WIN32 +#define PATHNAME_BUFFER_SIZE MAX_PATH +#define MKTEMP _mktemp +#else +#define PATHNAME_BUFFER_SIZE PATH_MAX +#define MKTEMP mktemp +#endif int fd; - char buf[MAX_PATH]; + char buf[PATHNAME_BUFFER_SIZE]; while (1) { - strcpy((char*)&buf, template); - if (_mktemp((char*)&buf)) { - if ((fd=open((char*)&buf, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR))!=-1) { - strcpy(template, (char*)&buf); + /* Fruit fallen from the tree: for people who like + microoptimizations, we might not need to copy the whole + template on every loop, but only the last several characters. + But I didn't feel like testing the boundary cases in Windows's + _mktemp. */ + strncpy(buf, template, PATHNAME_BUFFER_SIZE); + buf[PATHNAME_BUFFER_SIZE-1]=0; /* force NULL-termination */ + if (MKTEMP(buf)) { + if ((fd=open(buf, O_CREAT|O_EXCL|O_RDWR, mode))!=-1) { + strcpy(template, buf); return (fd); } else if (errno != EEXIST) @@ -300,9 +317,8 @@ int sb_mkstemp (char *template) { } else return (-1); } -#else - return(mkstemp(template)); -#endif +#undef MKTEMP +#undef PATHNAME_BUFFER_SIZE } @@ -343,7 +359,7 @@ uid_homedir(uid_t uid) } else { char *result = malloc(len + 2); if (result) { - int nchars = sprintf(result,"%s/",p->pw_dir); + unsigned int nchars = sprintf(result,"%s/",p->pw_dir); if (nchars == len + 1) { return result; } else {