Simplify (and robustify) regular PACKing
[sbcl.git] / src / runtime / wrap.c
index af8b30e..7c88568 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 <time.h>
 #include <sys/wait.h>
+#include <sys/resource.h>
 #include <netdb.h>
 #endif
 #include <stdio.h>
 
 #if defined(LISP_FEATURE_WIN32)
 #define WIN32_LEAN_AND_MEAN
-#include <fcntl.h>
 #include <errno.h>
 #endif
 
@@ -59,73 +64,6 @@ extern char **environ;
  * stuff needed by CL:DIRECTORY and other Lisp directory operations
  */
 
-/* Unix directory operations think of "." and ".." as filenames, but
- * Lisp directory operations do not. */
-int
-is_lispy_filename(const char *filename)
-{
-    return strcmp(filename, ".") && strcmp(filename, "..");
-}
-
-/* Return a zero-terminated array of strings holding the Lispy filenames
- * (i.e. excluding the Unix magic "." and "..") in the named directory. */
-char**
-alloc_directory_lispy_filenames(const char *directory_name)
-{
-    DIR *dir_ptr = opendir(directory_name);
-    char **result = 0;
-
-    if (dir_ptr) { /* if opendir success */
-
-        struct voidacc va;
-
-        if (0 == voidacc_ctor(&va)) { /* if voidacc_ctor success */
-            struct dirent *dirent_ptr;
-
-            while ( (dirent_ptr = readdir(dir_ptr)) ) { /* until end of data */
-                char* original_name = dirent_ptr->d_name;
-                if (is_lispy_filename(original_name)) {
-                    /* strdup(3) is in Linux and *BSD. If you port
-                     * somewhere else that doesn't have it, it's easy
-                     * to reimplement. */
-                    char* dup_name = strdup(original_name);
-                    if (!dup_name) { /* if strdup failure */
-                        goto dtors;
-                    }
-                    if (voidacc_acc(&va, dup_name)) { /* if acc failure */
-                        goto dtors;
-                    }
-                }
-            }
-            result = (char**)voidacc_give_away_result(&va);
-        }
-
-    dtors:
-        voidacc_dtor(&va);
-        /* ignoring closedir(3) return code, since what could we do?
-         *
-         * "Never ask questions you don't want to know the answer to."
-         * -- William Irving Zumwalt (Rich Cook, _The Wizardry Quested_) */
-        closedir(dir_ptr);
-    }
-
-    return result;
-}
-
-/* Free a result returned by alloc_directory_lispy_filenames(). */
-void
-free_directory_lispy_filenames(char** directory_lispy_filenames)
-{
-    char** p;
-
-    /* Free the strings. */
-    for (p = directory_lispy_filenames; *p; ++p) {
-        free(*p);
-    }
-
-    /* Free the table of strings. */
-    free(directory_lispy_filenames);
-}
 \f
 /*
  * readlink(2) stuff
@@ -158,6 +96,77 @@ 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
+/* readdir, closedir, and dirent name accessor. The first three are not strictly
+ * necessary, but should save us some #!+netbsd in the build, and this also allows
+ * building Windows versions using the non-ANSI variants of FindFirstFile &co
+ * under the same API. (Use a structure that appends the handle to the WIN32_FIND_DATA
+ * as the return value from sb_opendir, on sb_readdir grab the name from the previous
+ * call and save the new one.) Nikodemus thought he would have to do that to support
+ * DIRECTORY on UNC paths, but turns out opendir &co do TRT on Windows already -- so
+ * leaving that bit of tedium for a later date, once we figure out the whole *A vs. *W
+ * issue out properly. ...FIXME, obviously, as per above.
+ *
+ * Once that is done, the lisp side functions are best named OS-OPENDIR, etc.
+ */
+extern DIR *
+sb_opendir(char * name)
+{
+    return opendir(name);
+}
+
+extern struct dirent *
+sb_readdir(DIR * dirp)
+{
+    return readdir(dirp);
+}
+
+extern int
+sb_closedir(DIR * dirp)
+{
+    return closedir(dirp);
+}
+
+extern char *
+sb_dirent_name(struct dirent * ent)
+{
+    return ent->d_name;
+}
+\f
+/*
  * stat(2) stuff
  */
 
@@ -245,17 +254,33 @@ 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) {
+/* 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)
@@ -263,9 +288,8 @@ int sb_mkstemp (char *template) {
     } else
       return (-1);
   }
-#else
-  return(mkstemp(template));
-#endif
+#undef MKTEMP
+#undef PATHNAME_BUFFER_SIZE
 }
 
 \f
@@ -295,10 +319,9 @@ uid_username(int uid)
 }
 
 char *
-uid_homedir(uid_t uid)
+passwd_homedir(struct passwd *p)
 {
-    struct passwd *p = getpwuid(uid);
-    if(p) {
+    if (p) {
         /* Let's be careful about this, shall we? */
         size_t len = strlen(p->pw_dir);
         if (p->pw_dir[len-1] == '/') {
@@ -306,7 +329,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 {
@@ -320,6 +343,18 @@ uid_homedir(uid_t uid)
         return 0;
     }
 }
+
+char *
+user_homedir(char *name)
+{
+    return passwd_homedir(getpwnam(name));
+}
+
+char *
+uid_homedir(uid_t uid)
+{
+    return passwd_homedir(getpwuid(uid));
+}
 #endif /* !LISP_FEATURE_WIN32 */
 \f
 /*
@@ -343,7 +378,7 @@ wrapped_environ()
  * faked-up implementation of select(). Right now just enough to get through
  * second genesis.
  */
-int select(int top_fd, DWORD *read_set, DWORD *write_set, DWORD *except_set, time_t *timeout)
+int sb_select(int top_fd, DWORD *read_set, DWORD *write_set, DWORD *except_set, time_t *timeout)
 {
     /*
      * FIXME: Going forward, we may want to use MsgWaitForMultipleObjects
@@ -390,10 +425,18 @@ int select(int top_fd, DWORD *read_set, DWORD *write_set, DWORD *except_set, tim
  * yet, however, and the closest we can easily get to a timeval is the
  * seconds part. So that's what we do.
  */
-int gettimeofday(long *timeval, long *timezone)
+#define UNIX_EPOCH_FILETIME 116444736000000000ULL
+
+int sb_gettimeofday(long *timeval, long *timezone)
 {
-    timeval[0] = time(NULL);
-    timeval[1] = 0;
+    FILETIME ft;
+    ULARGE_INTEGER uft;
+    GetSystemTimeAsFileTime(&ft);
+    uft.LowPart = ft.dwLowDateTime;
+    uft.HighPart = ft.dwHighDateTime;
+    uft.QuadPart -= UNIX_EPOCH_FILETIME;
+    timeval[0] = uft.QuadPart / 10000000;
+    timeval[1] = (uft.QuadPart % 10000000)/10;
 
     return 0;
 }
@@ -471,3 +514,37 @@ int s_issock(mode_t mode)
 #endif
 }
 #endif /* !LISP_FEATURE_WIN32 */
+
+#ifndef LISP_FEATURE_WIN32
+int sb_getrusage(int who, struct rusage *rusage)
+{
+        return getrusage(who, rusage);
+}
+
+int sb_gettimeofday(struct timeval *tp, void *tzp)
+{
+        return gettimeofday(tp, tzp);
+}
+
+int sb_nanosleep(struct timespec *rqtp, struct timespec *rmtp)
+{
+        return nanosleep(rqtp, rmtp);
+}
+
+int sb_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
+    struct timeval *timeout)
+{
+        return select(nfds, readfds, writefds, exceptfds, timeout);
+}
+
+int sb_getitimer(int which, struct itimerval *value)
+{
+        return getitimer(which, value);
+}
+
+int sb_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
+{
+        return setitimer(which, value, ovalue);
+}
+#endif /* !LISP_FEATURE_WIN32 */
+