import -ko -b 1.1.3 XEmacs XEmacs-21_2 r21-2-35
[chise/xemacs-chise.git.1] / src / realpath.c
index f81bab6..be14a42 100644 (file)
@@ -28,35 +28,40 @@ Boston, MA 02111-1307, USA.  */
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef _POSIX_VERSION
-#include <limits.h>                    /* for PATH_MAX */
-#else
-#include <sys/param.h>                 /* for MAXPATHLEN */
+
+#if defined (HAVE_SYS_PARAM_H)
+#include <sys/param.h>
 #endif
 
-#ifdef WINDOWSNT
+#ifdef WIN32_NATIVE
 #include <direct.h>
 #endif
 
 #include <sys/stat.h>                  /* for S_IFLNK */
 
-#ifndef PATH_MAX
-#ifdef _POSIX_VERSION
-#define PATH_MAX _POSIX_PATH_MAX
-#else
-#ifdef MAXPATHLEN
-#define PATH_MAX MAXPATHLEN
-#else
-#define PATH_MAX 1024
-#endif
+#if !defined (HAVE_GETCWD) && defined (HAVE_GETWD)
+#undef getcwd
+#define getcwd(buffer, len) getwd (buffer)
 #endif
+
+#ifndef PATH_MAX
+# if defined (_POSIX_PATH_MAX)
+#  define PATH_MAX _POSIX_PATH_MAX
+# elif defined (MAXPATHLEN)
+#  define PATH_MAX MAXPATHLEN
+# else
+#  define PATH_MAX 1024
+# endif
 #endif
 
 #define MAX_READLINKS 32
 
+char * xrealpath (const char *path, char resolved_path []);
 char *
 xrealpath (const char *path, char resolved_path [])
 {
@@ -70,10 +75,10 @@ xrealpath (const char *path, char resolved_path [])
 #endif
 
   /* Make a copy of the source path since we may need to modify it. */
-  strcpy(copy_path, path);
+  strcpy (copy_path, path);
   path = copy_path;
   max_path = copy_path + PATH_MAX - 2;
-#ifdef WINDOWSNT
+#ifdef WIN32_NATIVE
   /*
   ** In NT we have two different cases:  (1) the path name begins
   ** with a drive letter, e.g., "C:"; and (2) the path name begins
@@ -99,7 +104,7 @@ xrealpath (const char *path, char resolved_path [])
   */
   else if (*path == '/')
     {
-      getcwd(new_path, PATH_MAX - 1);
+      getcwd (new_path, PATH_MAX - 1);
       new_path += 3;
       path++;
     }
@@ -109,21 +114,17 @@ xrealpath (const char *path, char resolved_path [])
   */
   else
     {
-      getcwd(new_path, PATH_MAX - 1);
+      getcwd (new_path, PATH_MAX - 1);
       new_path += strlen(new_path);
       if (new_path[-1] != '/')
        *new_path++ = '/';
     }
 
 #else
-  /* If it's a relative pathname use getwd for starters. */
+  /* If it's a relative pathname use getcwd for starters. */
   if (*path != '/')
     {
-#ifdef HAVE_GETCWD
-      getcwd(new_path, PATH_MAX - 1);
-#else
-      getwd(new_path);
-#endif
+      getcwd (new_path, PATH_MAX - 1);
       new_path += strlen(new_path);
       if (new_path[-1] != '/')
        *new_path++ = '/';
@@ -153,21 +154,20 @@ xrealpath (const char *path, char resolved_path [])
              continue;
            }
 
-         if (path[1] == '.')
+         /* Handle ".." */
+         if (path[1] == '.' &&
+             (path[2] == '\0' || path[2] == '/'))
            {
-             if (path[2] == '\0' || path[2] == '/')
-               {
-                 path += 2;
-
-                 /* Ignore ".." at root. */
-                 if (new_path == resolved_path + 1)
-                   continue;
-
-                 /* Handle ".." by backing up. */
-                 while ((--new_path)[-1] != '/')
-                   ;
-                 continue;
-               }
+             path += 2;
+
+             /* Ignore ".." at root. */
+             if (new_path == resolved_path + 1)
+               continue;
+
+             /* Handle ".." by backing up. */
+             while ((--new_path)[-1] != '/')
+               ;
+             continue;
            }
        }
 
@@ -185,7 +185,7 @@ xrealpath (const char *path, char resolved_path [])
 #ifdef S_IFLNK
       /* See if latest pathname component is a symlink. */
       *new_path = '\0';
-      n = readlink(resolved_path, link_path, PATH_MAX - 1);
+      n = readlink (resolved_path, link_path, PATH_MAX - 1);
 
       if (n < 0)
        {