Thread: is_absolute_path cleanup

is_absolute_path cleanup

From
Bruce Momjian
Date:
I have applied the attached patch to cleanup coding of the
is_absolute_path macro, and added documentation of how 'E:abc' is
handled on Win32.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +
diff --git a/src/include/port.h b/src/include/port.h
index 4f0c0c1..2020a26 100644
*** a/src/include/port.h
--- b/src/include/port.h
*************** extern void pgfnames_cleanup(char **file
*** 68,84 ****
   *    By making this a macro we avoid needing to include path.c in libpq.
   */
  #ifndef WIN32
  #define is_absolute_path(filename) \
  ( \
!     ((filename)[0] == '/') \
  )
  #else
  #define is_absolute_path(filename) \
  ( \
!     ((filename)[0] == '/') || \
!     (filename)[0] == '\\' || \
      (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
!     ((filename)[2] == '\\' || (filename)[2] == '/')) \
  )
  #endif

--- 68,94 ----
   *    By making this a macro we avoid needing to include path.c in libpq.
   */
  #ifndef WIN32
+ #define IS_DIR_SEP(ch)    ((ch) == '/')
+
  #define is_absolute_path(filename) \
  ( \
!     IS_DIR_SEP((filename)[0]) \
  )
  #else
+ #define IS_DIR_SEP(ch)    ((ch) == '/' || (ch) == '\\')
+
+ /*
+  * On Win32, a drive letter _not_ followed by a slash, e.g. 'E:abc', is
+  * relative to the cwd on that drive, or the drive's root directory
+  * if that drive has no cwd.  Because the path itself cannot tell us
+  * which is the case, we have to assume the worst, i.e. that it is not
+  * absolute;  this check is done by IS_DIR_SEP(filename[2]).
+  */
  #define is_absolute_path(filename) \
  ( \
!     IS_DIR_SEP((filename)[0]) || \
      (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
!      IS_DIR_SEP((filename)[2])) \
  )
  #endif

diff --git a/src/port/path.c b/src/port/path.c
index ccf801e..5b0056d 100644
*** a/src/port/path.c
--- b/src/port/path.c
***************
*** 35,46 ****


  #ifndef WIN32
- #define IS_DIR_SEP(ch)    ((ch) == '/')
- #else
- #define IS_DIR_SEP(ch)    ((ch) == '/' || (ch) == '\\')
- #endif
-
- #ifndef WIN32
  #define IS_PATH_VAR_SEP(ch) ((ch) == ':')
  #else
  #define IS_PATH_VAR_SEP(ch) ((ch) == ';')
--- 35,40 ----