X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsame.c;h=aa1a2318fc44250c879c38a0759e53d9c6709730;hb=fdae5d9f72a0596fc7b5933509a9cd03cbebf6eb;hp=d41cccc46138da457f92b690d127f882791ebf0d;hpb=437df36c45f7643f0b40d2dc5100b2490613702d;p=gnulib.git diff --git a/lib/same.c b/lib/same.c index d41cccc46..aa1a2318f 100644 --- a/lib/same.c +++ b/lib/same.c @@ -21,13 +21,12 @@ # include #endif +#include #include #ifdef HAVE_UNISTD_H # include #endif -#if HAVE_STDLIB_H -# include -#endif +#include #include #include #include @@ -36,10 +35,11 @@ extern int errno; #endif -#if HAVE_STRING_H -# include -#else -# include +#include + +#include +#ifndef _POSIX_NAME_MAX +# define _POSIX_NAME_MAX 14 #endif #include "same.h" @@ -47,12 +47,7 @@ extern int errno; #include "error.h" #include "xalloc.h" -#ifndef HAVE_DECL_FREE -"this configure-time declaration test was not run" -#endif -#if !HAVE_DECL_FREE -void free (); -#endif +#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define SAME_INODE(Stat_buf_1, Stat_buf_2) \ ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \ @@ -69,9 +64,24 @@ same_name (const char *source, const char *dest) char const *dest_basename = base_name (dest); size_t source_baselen = base_len (source_basename); size_t dest_baselen = base_len (dest_basename); + bool identical_basenames = + (source_baselen == dest_baselen + && memcmp (source_basename, dest_basename, dest_baselen) == 0); + bool compare_dirs = identical_basenames; + bool same = false; + +#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX + /* This implementation silently truncates pathname components. If + the base names might be truncated, check whether the truncated + base names are the same, while checking the directories. */ + size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX; + size_t min_baselen = MIN (source_baselen, dest_baselen); + if (slen_max <= min_baselen + && memcmp (source_basename, dest_basename, slen_max) == 0) + compare_dirs = true; +#endif - if (source_baselen == dest_baselen - && memcmp (source_basename, dest_basename, dest_baselen) == 0) + if (compare_dirs) { struct stat source_dir_stats; struct stat dest_dir_stats; @@ -93,12 +103,30 @@ same_name (const char *source, const char *dest) error (1, errno, "%s", dest_dirname); } + same = SAME_INODE (source_dir_stats, dest_dir_stats); + +#if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX + if (same && ! identical_basenames) + { + long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX)); + if (name_max < 0) + { + if (errno) + { + /* Shouldn't happen. */ + error (1, errno, "%s", dest_dirname); + } + same = false; + } + else + same = (name_max <= min_baselen + && memcmp (source_basename, dest_basename, name_max) == 0); + } +#endif + free (source_dirname); free (dest_dirname); - - if (SAME_INODE (source_dir_stats, dest_dir_stats)) - return 1; } - return 0; + return same; }