1 /* Determine whether two file names refer to the same file.
3 Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* written by Jim Meyering */
30 #include <sys/types.h>
38 #ifndef _POSIX_NAME_MAX
39 # define _POSIX_NAME_MAX 14
48 # define MIN(a, b) ((a) < (b) ? (a) : (b))
51 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
52 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
53 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
55 /* Return nonzero if SOURCE and DEST point to the same name in the same
59 same_name (const char *source, const char *dest)
61 /* Compare the basenames. */
62 char const *source_basename = base_name (source);
63 char const *dest_basename = base_name (dest);
64 size_t source_baselen = base_len (source_basename);
65 size_t dest_baselen = base_len (dest_basename);
66 bool identical_basenames =
67 (source_baselen == dest_baselen
68 && memcmp (source_basename, dest_basename, dest_baselen) == 0);
69 bool compare_dirs = identical_basenames;
72 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
73 /* This implementation silently truncates components of file names. If
74 the base names might be truncated, check whether the truncated
75 base names are the same, while checking the directories. */
76 size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
77 size_t min_baselen = MIN (source_baselen, dest_baselen);
78 if (slen_max <= min_baselen
79 && memcmp (source_basename, dest_basename, slen_max) == 0)
85 struct stat source_dir_stats;
86 struct stat dest_dir_stats;
87 char *source_dirname, *dest_dirname;
89 /* Compare the parent directories (via the device and inode numbers). */
90 source_dirname = dir_name (source);
91 dest_dirname = dir_name (dest);
93 if (stat (source_dirname, &source_dir_stats))
95 /* Shouldn't happen. */
96 error (1, errno, "%s", source_dirname);
99 if (stat (dest_dirname, &dest_dir_stats))
101 /* Shouldn't happen. */
102 error (1, errno, "%s", dest_dirname);
105 same = SAME_INODE (source_dir_stats, dest_dir_stats);
107 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
108 if (same && ! identical_basenames)
110 long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX));
115 /* Shouldn't happen. */
116 error (1, errno, "%s", dest_dirname);
121 same = (name_max <= min_baselen
122 && memcmp (source_basename, dest_basename, name_max) == 0);
126 free (source_dirname);