1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Determine whether two file names refer to the same file.
5 Copyright (C) 1997-2000, 2002-2006, 2009-2010 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* written by Jim Meyering */
28 #include <sys/types.h>
36 #ifndef _POSIX_NAME_MAX
37 # define _POSIX_NAME_MAX 14
43 #include "same-inode.h"
46 # define MIN(a, b) ((a) < (b) ? (a) : (b))
49 /* Return nonzero if SOURCE and DEST point to the same name in the same
53 same_name (const char *source, const char *dest)
55 /* Compare the basenames. */
56 char const *source_basename = last_component (source);
57 char const *dest_basename = last_component (dest);
58 size_t source_baselen = base_len (source_basename);
59 size_t dest_baselen = base_len (dest_basename);
60 bool identical_basenames =
61 (source_baselen == dest_baselen
62 && memcmp (source_basename, dest_basename, dest_baselen) == 0);
63 bool compare_dirs = identical_basenames;
66 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
67 /* This implementation silently truncates components of file names. If
68 the base names might be truncated, check whether the truncated
69 base names are the same, while checking the directories. */
70 size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
71 size_t min_baselen = MIN (source_baselen, dest_baselen);
72 if (slen_max <= min_baselen
73 && memcmp (source_basename, dest_basename, slen_max) == 0)
79 struct stat source_dir_stats;
80 struct stat dest_dir_stats;
81 char *source_dirname, *dest_dirname;
83 /* Compare the parent directories (via the device and inode numbers). */
84 source_dirname = dir_name (source);
85 dest_dirname = dir_name (dest);
87 if (stat (source_dirname, &source_dir_stats))
89 /* Shouldn't happen. */
90 error (1, errno, "%s", source_dirname);
93 if (stat (dest_dirname, &dest_dir_stats))
95 /* Shouldn't happen. */
96 error (1, errno, "%s", dest_dirname);
99 same = SAME_INODE (source_dir_stats, dest_dir_stats);
101 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
102 if (same && ! identical_basenames)
104 long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX));
109 /* Shouldn't happen. */
110 error (1, errno, "%s", dest_dirname);
115 same = (name_max <= min_baselen
116 && memcmp (source_basename, dest_basename, name_max) == 0);
120 free (source_dirname);