.
[coreutils.git] / lib / same.c
blobaa1a2318fc44250c879c38a0759e53d9c6709730
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)
7 any later version.
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 */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdbool.h>
25 #include <stdio.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #ifndef errno
35 extern int errno;
36 #endif
38 #include <string.h>
40 #include <limits.h>
41 #ifndef _POSIX_NAME_MAX
42 # define _POSIX_NAME_MAX 14
43 #endif
45 #include "same.h"
46 #include "dirname.h"
47 #include "error.h"
48 #include "xalloc.h"
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
57 directory. */
59 int
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;
71 bool same = false;
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)
81 compare_dirs = true;
82 #endif
84 if (compare_dirs)
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));
112 if (name_max < 0)
114 if (errno)
116 /* Shouldn't happen. */
117 error (1, errno, "%s", dest_dirname);
119 same = false;
121 else
122 same = (name_max <= min_baselen
123 && memcmp (source_basename, dest_basename, name_max) == 0);
125 #endif
127 free (source_dirname);
128 free (dest_dirname);
131 return same;