Merge from coreutils.
[gnulib.git] / lib / same.c
1 /* Determine whether two file names refer to the same file.
2
3    Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006 Free
4    Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* written by Jim Meyering */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <string.h>
36
37 #include <limits.h>
38 #ifndef _POSIX_NAME_MAX
39 # define _POSIX_NAME_MAX 14
40 #endif
41
42 #include "same.h"
43 #include "dirname.h"
44 #include "error.h"
45 #include "same-inode.h"
46 #include "xalloc.h"
47
48 #ifndef MIN
49 # define MIN(a, b) ((a) < (b) ? (a) : (b))
50 #endif
51
52 /* Return nonzero if SOURCE and DEST point to the same name in the same
53    directory.  */
54
55 bool
56 same_name (const char *source, const char *dest)
57 {
58   /* Compare the basenames.  */
59   char const *source_basename = last_component (source);
60   char const *dest_basename = last_component (dest);
61   size_t source_baselen = base_len (source_basename);
62   size_t dest_baselen = base_len (dest_basename);
63   bool identical_basenames =
64     (source_baselen == dest_baselen
65      && memcmp (source_basename, dest_basename, dest_baselen) == 0);
66   bool compare_dirs = identical_basenames;
67   bool same = false;
68
69 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
70   /* This implementation silently truncates components of file names.  If
71      the base names might be truncated, check whether the truncated
72      base names are the same, while checking the directories.  */
73   size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
74   size_t min_baselen = MIN (source_baselen, dest_baselen);
75   if (slen_max <= min_baselen
76       && memcmp (source_basename, dest_basename, slen_max) == 0)
77     compare_dirs = true;
78 #endif
79
80   if (compare_dirs)
81     {
82       struct stat source_dir_stats;
83       struct stat dest_dir_stats;
84       char *source_dirname, *dest_dirname;
85
86       /* Compare the parent directories (via the device and inode numbers).  */
87       source_dirname = dir_name (source);
88       dest_dirname = dir_name (dest);
89
90       if (stat (source_dirname, &source_dir_stats))
91         {
92           /* Shouldn't happen.  */
93           error (1, errno, "%s", source_dirname);
94         }
95
96       if (stat (dest_dirname, &dest_dir_stats))
97         {
98           /* Shouldn't happen.  */
99           error (1, errno, "%s", dest_dirname);
100         }
101
102       same = SAME_INODE (source_dir_stats, dest_dir_stats);
103
104 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
105       if (same && ! identical_basenames)
106         {
107           long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX));
108           if (name_max < 0)
109             {
110               if (errno)
111                 {
112                   /* Shouldn't happen.  */
113                   error (1, errno, "%s", dest_dirname);
114                 }
115               same = false;
116             }
117           else
118             same = (name_max <= min_baselen
119                     && memcmp (source_basename, dest_basename, name_max) == 0);
120         }
121 #endif
122
123       free (source_dirname);
124       free (dest_dirname);
125     }
126
127   return same;
128 }