Use unique_xmalloc_ptr in fixup_go_packaging
[deliverable/binutils-gdb.git] / libiberty / lrealpath.c
index b001b38ef66325a61f93d8d8a81c64b6cd78eced..ac914a7a4f403b09c90416c8e1fa3c6b52c1b48a 100644 (file)
@@ -1,7 +1,7 @@
 /* Libiberty realpath.  Like realpath, but more consistent behavior.
    Based on gdb_realpath from GDB.
 
-   Copyright 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003-2019 Free Software Foundation, Inc.
 
    This file is part of the libiberty library.
 
@@ -17,8 +17,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
 
 /*
 
@@ -49,6 +49,9 @@ components will be simplified.  The returned value will be allocated using
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
 
 /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
 #if defined(HAVE_CANONICALIZE_FILE_NAME) \
@@ -64,11 +67,16 @@ extern char *canonicalize_file_name (const char *);
 #   define REALPATH_LIMIT MAXPATHLEN
 #  endif
 # endif
+#else
+  /* cygwin has realpath, so it won't get here.  */ 
+# if defined (_WIN32)
+#  define WIN32_LEAN_AND_MEAN
+#  include <windows.h> /* for GetFullPathName */
+# endif
 #endif
 
 char *
-lrealpath (filename)
-     const char *filename;
+lrealpath (const char *filename)
 {
   /* Method 1: The system has a compile time upper bound on a filename
      path.  Use that and realpath() to canonicalize the name.  This is
@@ -112,7 +120,7 @@ lrealpath (filename)
       {
        /* PATH_MAX is bounded.  */
        char *buf, *rp, *ret;
-       buf = malloc (path_max);
+       buf = (char *) malloc (path_max);
        if (buf == NULL)
          return NULL;
        rp = realpath (filename, buf);
@@ -123,6 +131,43 @@ lrealpath (filename)
   }
 #endif
 
+  /* The MS Windows method.  If we don't have realpath, we assume we
+     don't have symlinks and just canonicalize to a Windows absolute
+     path.  GetFullPath converts ../ and ./ in relative paths to
+     absolute paths, filling in current drive if one is not given
+     or using the current directory of a specified drive (eg, "E:foo").
+     It also converts all forward slashes to back slashes.  */
+#if defined (_WIN32)
+  {
+    char buf[MAX_PATH];
+    char* basename;
+    DWORD len = GetFullPathName (filename, MAX_PATH, buf, &basename);
+    if (len == 0 || len > MAX_PATH - 1)
+      return strdup (filename);
+    else
+      {
+       /* The file system is case-preserving but case-insensitive,
+          Canonicalize to lowercase, using the codepage associated
+          with the process locale.  */
+        CharLowerBuff (buf, len);
+        return strdup (buf);
+      }
+  }
+#endif
+
   /* This system is a lost cause, just duplicate the filename.  */
   return strdup (filename);
 }
+
+/* Return true when FD file descriptor exists.  */
+
+int
+is_valid_fd (int fd)
+{
+#if defined(_WIN32)
+  HANDLE h = (HANDLE) _get_osfhandle (fd);
+  return h != (HANDLE) -1;
+#else
+  return fcntl (fd, F_GETFD) >= 0;
+#endif
+}
This page took 0.025522 seconds and 4 git commands to generate.