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