1.0.22.15: 16 byte align assembly routines on x86 and x86-64
[sbcl.git] / src / runtime / wrap.c
index 8daf076..5f18738 100644 (file)
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
+#include <errno.h>
+#include <limits.h>
+#include <fcntl.h>
+
 #ifndef LISP_FEATURE_WIN32
 #include <pwd.h>
 #include <sys/wait.h>
@@ -41,6 +45,7 @@
 
 #if defined(LISP_FEATURE_WIN32)
 #define WIN32_LEAN_AND_MEAN
+#include <errno.h>
 #endif
 
 #include "runtime.h"
@@ -156,6 +161,41 @@ wrapped_readlink(char *path)
 #endif
 \f
 /*
+ * realpath(3), including a wrapper for Windows.
+ */
+char * sb_realpath (char *path)
+{
+#ifndef LISP_FEATURE_WIN32
+    char *ret;
+    int errnum;
+
+    if ((ret = calloc(PATH_MAX, sizeof(char))) == NULL)
+        return NULL;
+    if (realpath(path, ret) == NULL) {
+        errnum = errno;
+        free(ret);
+        errno = errnum;
+        return NULL;
+    }
+    return(ret);
+#else
+    char *ret;
+    char *cp;
+    int errnum;
+
+    if ((ret = calloc(MAX_PATH, sizeof(char))) == NULL)
+        return NULL;
+    if (GetFullPathName(path, MAX_PATH, ret, cp) == 0) {
+        errnum = errno;
+        free(ret);
+        errno = errnum;
+        return NULL;
+    }
+    return(ret);
+#endif
+}
+\f
+/*
  * stat(2) stuff
  */
 
@@ -243,6 +283,45 @@ fstat_wrapper(int filedes, struct stat_wrapper *buf)
     return ret;
 }
 \f
+/* 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[PATHNAME_BUFFER_SIZE];
+
+  while (1) {
+    /* 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)
+          return (-1);
+    } else
+      return (-1);
+  }
+#undef MKTEMP
+#undef PATHNAME_BUFFER_SIZE
+}
+
+\f
 /*
  * getpwuid() stuff
  */
@@ -280,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 {