update nearly all FSF copyright year lists to include 2010
[gnulib.git] / lib / same.c
1 /* Determine whether two file names refer to the same file.
2
3    Copyright (C) 1997-2000, 2002-2006, 2009-2010 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* written by Jim Meyering */
19
20 #include <config.h>
21
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
29 #include <errno.h>
30
31 #include <string.h>
32
33 #include <limits.h>
34 #ifndef _POSIX_NAME_MAX
35 # define _POSIX_NAME_MAX 14
36 #endif
37
38 #include "same.h"
39 #include "dirname.h"
40 #include "error.h"
41 #include "same-inode.h"
42 #include "xalloc.h"
43
44 #ifndef MIN
45 # define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #endif
47
48 /* Return nonzero if SOURCE and DEST point to the same name in the same
49    directory.  */
50
51 bool
52 same_name (const char *source, const char *dest)
53 {
54   /* Compare the basenames.  */
55   char const *source_basename = last_component (source);
56   char const *dest_basename = last_component (dest);
57   size_t source_baselen = base_len (source_basename);
58   size_t dest_baselen = base_len (dest_basename);
59   bool identical_basenames =
60     (source_baselen == dest_baselen
61      && memcmp (source_basename, dest_basename, dest_baselen) == 0);
62   bool compare_dirs = identical_basenames;
63   bool same = false;
64
65 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
66   /* This implementation silently truncates components of file names.  If
67      the base names might be truncated, check whether the truncated
68      base names are the same, while checking the directories.  */
69   size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
70   size_t min_baselen = MIN (source_baselen, dest_baselen);
71   if (slen_max <= min_baselen
72       && memcmp (source_basename, dest_basename, slen_max) == 0)
73     compare_dirs = true;
74 #endif
75
76   if (compare_dirs)
77     {
78       struct stat source_dir_stats;
79       struct stat dest_dir_stats;
80       char *source_dirname, *dest_dirname;
81
82       /* Compare the parent directories (via the device and inode numbers).  */
83       source_dirname = dir_name (source);
84       dest_dirname = dir_name (dest);
85
86       if (stat (source_dirname, &source_dir_stats))
87         {
88           /* Shouldn't happen.  */
89           error (1, errno, "%s", source_dirname);
90         }
91
92       if (stat (dest_dirname, &dest_dir_stats))
93         {
94           /* Shouldn't happen.  */
95           error (1, errno, "%s", dest_dirname);
96         }
97
98       same = SAME_INODE (source_dir_stats, dest_dir_stats);
99
100 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX
101       if (same && ! identical_basenames)
102         {
103           long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX));
104           if (name_max < 0)
105             {
106               if (errno)
107                 {
108                   /* Shouldn't happen.  */
109                   error (1, errno, "%s", dest_dirname);
110                 }
111               same = false;
112             }
113           else
114             same = (name_max <= min_baselen
115                     && memcmp (source_basename, dest_basename, name_max) == 0);
116         }
117 #endif
118
119       free (source_dirname);
120       free (dest_dirname);
121     }
122
123   return same;
124 }