From 0b8448af68b2720d08640604355ef7d961a5acd6 Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Tue, 24 Mar 2020 15:24:02 +0000 Subject: [PATCH] Add code to the BFD library to handle opening files with pathnames longer than MAX_PATH on Win32 systems. PR 25713 * bfdio.c (_bfd_real_fopen): Add code to handle long filenames on Win32 systems. --- bfd/ChangeLog | 6 ++++++ bfd/bfdio.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index d9e1139eb1a..93c4bf950e6 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2020-03-24 Nick Clifton + + PR 25713 + * bfdio.c (_bfd_real_fopen): Add code to handle long filenames on + Win32 systems. + 2020-03-24 Nick Clifton PR 25681 diff --git a/bfd/bfdio.c b/bfd/bfdio.c index 71ac17ec515..9e88f5be915 100644 --- a/bfd/bfdio.c +++ b/bfd/bfdio.c @@ -26,6 +26,9 @@ #include "bfd.h" #include "libbfd.h" #include "aout/ar.h" +#if defined (_WIN32) +#include +#endif #ifndef S_IXUSR #define S_IXUSR 0100 /* Execute by owner. */ @@ -93,12 +96,7 @@ _bfd_real_fopen (const char *filename, const char *modes) In fopen-vms.h, they are separated from the mode with a comma. Split here. */ vms_attr = strchr (modes, ','); - if (vms_attr == NULL) - { - /* No attributes. */ - return close_on_exec (fopen (filename, modes)); - } - else + if (vms_attr != NULL) { /* Attributes found. Split. */ size_t modes_len = strlen (modes) + 1; @@ -116,13 +114,29 @@ _bfd_real_fopen (const char *filename, const char *modes) } return close_on_exec (fopen (filename, at[0], at[1], at[2])); } -#else /* !VMS */ -#if defined (HAVE_FOPEN64) + +#elif defined (_WIN32) + size_t filelen = strlen (filename) + 1; + + if (filelen > MAX_PATH - 1) + { + FILE *file; + char* fullpath = (char *) malloc (filelen + 8); + + /* Add a Microsoft recommended prefix that + will allow the extra-long path to work. */ + strcpy (fullpath, "\\\\?\\"); + strcat (fullpath, filename); + file = close_on_exec (fopen (fullpath, modes)); + free (fullpath); + return file; + } + +#elif defined (HAVE_FOPEN64) return close_on_exec (fopen64 (filename, modes)); -#else - return close_on_exec (fopen (filename, modes)); #endif -#endif /* !VMS */ + + return close_on_exec (fopen (filename, modes)); } /* -- 2.30.2