summaryrefslogtreecommitdiff
path: root/lib/same.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2003-08-18 07:47:40 +0000
committerJim Meyering <jim@meyering.net>2003-08-18 07:47:40 +0000
commit0fc380c89d65822d8f72f4946269e77e92ece67b (patch)
tree62edd106e7c585c23428038ff9d0f41bbca1cff5 /lib/same.c
parentd957e165d437c0914feb2dd52de0ad59bd40aaa3 (diff)
downloadcoreutils-0fc380c89d65822d8f72f4946269e77e92ece67b.tar.xz
Include <stdbool.h>, <limits.h>.
(_POSIX_NAME_MAX): Define if not defined. (MIN): New macro. (same_name): If file names are silently truncated, report that the file names are the same if they are the same after the silent truncation.
Diffstat (limited to 'lib/same.c')
-rw-r--r--lib/same.c53
1 files changed, 47 insertions, 6 deletions
diff --git a/lib/same.c b/lib/same.c
index d41cccc46..c0b92447e 100644
--- a/lib/same.c
+++ b/lib/same.c
@@ -21,6 +21,7 @@
# include <config.h>
#endif
+#include <stdbool.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
@@ -42,6 +43,11 @@ extern int errno;
# include <strings.h>
#endif
+#include <limits.h>
+#ifndef _POSIX_NAME_MAX
+# define _POSIX_NAME_MAX 14
+#endif
+
#include "same.h"
#include "dirname.h"
#include "error.h"
@@ -54,6 +60,8 @@ extern int errno;
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 \
&& (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
@@ -69,9 +77,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 +116,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 (source_dirname, _PC_NAME_MAX));
+ if (name_max < 0)
+ {
+ if (errno)
+ {
+ /* Shouldn't happen. */
+ error (1, errno, "%s", source_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;
}