Merge from coreutils.
[gnulib.git] / lib / same.c
1 /* Determine whether two file names refer to the same file.
2    Copyright (C) 1997-2000, 2002-2003 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /* written by Jim Meyering */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
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
37
38 #include <string.h>
39
40 #include <limits.h>
41 #ifndef _POSIX_NAME_MAX
42 # define _POSIX_NAME_MAX 14
43 #endif
44
45 #include "same.h"
46 #include "dirname.h"
47 #include "error.h"
48 #include "xalloc.h"
49
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51
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)
55
56 /* Return nonzero if SOURCE and DEST point to the same name in the same
57    directory.  */
58
59 int
60 same_name (const char *source, const char *dest)
61 {
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;
72
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
83
84   if (compare_dirs)
85     {
86       struct stat source_dir_stats;
87       struct stat dest_dir_stats;
88       char *source_dirname, *dest_dirname;
89
90       /* Compare the parent directories (via the device and inode numbers).  */
91       source_dirname = dir_name (source);
92       dest_dirname = dir_name (dest);
93
94       if (stat (source_dirname, &source_dir_stats))
95         {
96           /* Shouldn't happen.  */
97           error (1, errno, "%s", source_dirname);
98         }
99
100       if (stat (dest_dirname, &dest_dir_stats))
101         {
102           /* Shouldn't happen.  */
103           error (1, errno, "%s", dest_dirname);
104         }
105
106       same = SAME_INODE (source_dir_stats, dest_dir_stats);
107
108 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
109       if (same && ! identical_basenames)
110         {
111           long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX));
112           if (name_max < 0)
113             {
114               if (errno)
115                 {
116                   /* Shouldn't happen.  */
117                   error (1, errno, "%s", dest_dirname);
118                 }
119               same = false;
120             }
121           else
122             same = (name_max <= min_baselen
123                     && memcmp (source_basename, dest_basename, name_max) == 0);
124         }
125 #endif
126
127       free (source_dirname);
128       free (dest_dirname);
129     }
130
131   return same;
132 }