1.0.13.18: Revived OpenBSD support, contributed by Josh Elsasser
[sbcl.git] / src / runtime / wrap.c
index 1d17e9a..a510702 100644 (file)
@@ -32,6 +32,8 @@
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
+#include <errno.h>
+#include <limits.h>
 #ifndef LISP_FEATURE_WIN32
 #include <pwd.h>
 #include <sys/wait.h>
 #endif
 #include <stdio.h>
 
+#if defined(LISP_FEATURE_WIN32)
+#define WIN32_LEAN_AND_MEAN
+#include <fcntl.h>
+#include <errno.h>
+#endif
+
 #include "runtime.h"
 #include "util.h"
 #include "wrap.h"
@@ -152,6 +160,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
  */
 
@@ -239,6 +282,30 @@ fstat_wrapper(int filedes, struct stat_wrapper *buf)
     return ret;
 }
 \f
+/* A wrapper for mkstemp(3), which seems not to exist on Windows. */
+int sb_mkstemp (char *template) {
+#ifdef LISP_FEATURE_WIN32
+  int fd;
+  char buf[MAX_PATH];
+
+  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);
+        return (fd);
+      } else
+        if (errno != EEXIST)
+          return (-1);
+    } else
+      return (-1);
+  }
+#else
+  return(mkstemp(template));
+#endif
+}
+
+\f
 /*
  * getpwuid() stuff
  */
@@ -307,7 +374,6 @@ wrapped_environ()
 }
 
 #ifdef LISP_FEATURE_WIN32
-#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <time.h>
 /*