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