Include <string.h> or <strings.h>, as appropriate, for declaration of strcmp.
[gnulib.git] / lib / same.c
1 /* Determine whether two file names refer to the same file.
2    Copyright (C) 1997-2000 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 <stdio.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #if HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #ifndef errno
36 extern int errno;
37 #endif
38
39 #if HAVE_STRING_H
40 # include <string.h>
41 #else
42 # include <strings.h>
43 #endif
44
45 #include "same.h"
46 #include "dirname.h"
47 #include "error.h"
48
49 #if ENABLE_NLS
50 # include <libintl.h>
51 # define _(Text) gettext (Text)
52 #else
53 # define _(Text) Text
54 #endif
55
56 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
57
58 #ifndef HAVE_DECL_FREE
59 "this configure-time declaration test was not run"
60 #endif
61 #if !HAVE_DECL_FREE
62 void free ();
63 #endif
64
65 char *base_name PARAMS ((char const *));
66
67 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
68   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
69    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
70
71 /* Return nonzero if SOURCE and DEST point to the same name in the same
72    directory.  */
73
74 int
75 same_name (const char *source, const char *dest)
76 {
77   struct stat source_dir_stats;
78   struct stat dest_dir_stats;
79   char *source_dirname, *dest_dirname;
80
81   source_dirname = dir_name (source);
82   dest_dirname = dir_name (dest);
83   if (source_dirname == NULL || dest_dirname == NULL)
84     error (1, 0, _("virtual memory exhausted"));
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   free (source_dirname);
99   free (dest_dirname);
100
101   return (SAME_INODE (source_dir_stats, dest_dir_stats)
102           && STREQ (base_name (source), base_name (dest)));
103 }