Use HAVE_STRUCT_STAT_ST_BLOCKS, not deprecated HAVE_ST_BLOCKS.
[gnulib.git] / lib / same.c
1 #if HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdio.h>
6 #ifdef HAVE_UNISTD_H
7 # include <unistd.h>
8 #endif
9 #if HAVE_STDLIB_H
10 # include <stdlib.h>
11 #endif
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #ifndef errno
17 extern int errno;
18 #endif
19
20 #include "same.h"
21 #include "dirname.h"
22 #include "error.h"
23
24 #if ENABLE_NLS
25 # include <libintl.h>
26 # define _(Text) gettext (Text)
27 #else
28 # define _(Text) Text
29 #endif
30
31 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
32
33 #ifndef HAVE_DECL_FREE
34 void free ();
35 #endif
36
37 char *base_name PARAMS ((char const *));
38
39 #define SAME_INODE(Stat_buf_1, Stat_buf_2) \
40   ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
41    && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
42
43 /* Return nonzero if SOURCE and DEST point to the same name in the same
44    directory.  */
45
46 int
47 same_name (const char *source, const char *dest)
48 {
49   struct stat source_dir_stats;
50   struct stat dest_dir_stats;
51   char *source_dirname, *dest_dirname;
52
53   source_dirname = dir_name (source);
54   dest_dirname = dir_name (dest);
55   if (source_dirname == NULL || dest_dirname == NULL)
56     error (1, 0, _("virtual memory exhausted"));
57
58   if (stat (source_dirname, &source_dir_stats))
59     {
60       /* Shouldn't happen.  */
61       error (1, errno, "%s", source_dirname);
62     }
63
64   if (stat (dest_dirname, &dest_dir_stats))
65     {
66       /* Shouldn't happen.  */
67       error (1, errno, "%s", dest_dirname);
68     }
69
70   free (source_dirname);
71   free (dest_dirname);
72
73   return (SAME_INODE (source_dir_stats, dest_dir_stats)
74           && STREQ (base_name (source), base_name (dest)));
75 }