1 /* Determine whether two file names refer to the same file.
2 Copyright (C) 1997-2000, 2002-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* written by Jim Meyering */
30 #include <sys/types.h>
41 #ifndef _POSIX_NAME_MAX
42 # define _POSIX_NAME_MAX 14
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
52 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
53 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
54 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
56 /* Return nonzero if SOURCE and DEST point to the same name in the same
60 same_name (const char *source
, const char *dest
)
62 /* Compare the basenames. */
63 char const *source_basename
= base_name (source
);
64 char const *dest_basename
= base_name (dest
);
65 size_t source_baselen
= base_len (source_basename
);
66 size_t dest_baselen
= base_len (dest_basename
);
67 bool identical_basenames
=
68 (source_baselen
== dest_baselen
69 && memcmp (source_basename
, dest_basename
, dest_baselen
) == 0);
70 bool compare_dirs
= identical_basenames
;
73 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
74 /* This implementation silently truncates pathname components. If
75 the base names might be truncated, check whether the truncated
76 base names are the same, while checking the directories. */
77 size_t slen_max
= HAVE_LONG_FILE_NAMES
? 255 : _POSIX_NAME_MAX
;
78 size_t min_baselen
= MIN (source_baselen
, dest_baselen
);
79 if (slen_max
<= min_baselen
80 && memcmp (source_basename
, dest_basename
, slen_max
) == 0)
86 struct stat source_dir_stats
;
87 struct stat dest_dir_stats
;
88 char *source_dirname
, *dest_dirname
;
90 /* Compare the parent directories (via the device and inode numbers). */
91 source_dirname
= dir_name (source
);
92 dest_dirname
= dir_name (dest
);
94 if (stat (source_dirname
, &source_dir_stats
))
96 /* Shouldn't happen. */
97 error (1, errno
, "%s", source_dirname
);
100 if (stat (dest_dirname
, &dest_dir_stats
))
102 /* Shouldn't happen. */
103 error (1, errno
, "%s", dest_dirname
);
106 same
= SAME_INODE (source_dir_stats
, dest_dir_stats
);
108 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
109 if (same
&& ! identical_basenames
)
111 long name_max
= (errno
= 0, pathconf (dest_dirname
, _PC_NAME_MAX
));
116 /* Shouldn't happen. */
117 error (1, errno
, "%s", dest_dirname
);
122 same
= (name_max
<= min_baselen
123 && memcmp (source_basename
, dest_basename
, name_max
) == 0);
127 free (source_dirname
);