*** empty log message ***
[coreutils.git] / lib / same.c
blob8dda9fd514d052bd58a8053dc24b68ec643cce2e
1 #if HAVE_CONFIG_H
2 # include <config.h>
3 #endif
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
20 #include "same.h"
21 #include "dirname.h"
22 #include "error.h"
24 #if ENABLE_NLS
25 # include <libintl.h>
26 # define _(Text) gettext (Text)
27 #else
28 # define _(Text) Text
29 #endif
31 #define STREQ(a, b) (strcmp ((a), (b)) == 0)
33 #ifndef HAVE_DECL_FREE
34 void free ();
35 #endif
37 char *base_name PARAMS ((char const *));
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)
43 /* Return nonzero if SOURCE and DEST point to the same name in the same
44 directory. */
46 int
47 same_name (const char *source, const char *dest)
49 struct stat source_dir_stats;
50 struct stat dest_dir_stats;
51 char *source_dirname, *dest_dirname;
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"));
58 if (stat (source_dirname, &source_dir_stats))
60 /* Shouldn't happen. */
61 error (1, errno, "%s", source_dirname);
64 if (stat (dest_dirname, &dest_dir_stats))
66 /* Shouldn't happen. */
67 error (1, errno, "%s", dest_dirname);
70 free (source_dirname);
71 free (dest_dirname);
73 return (SAME_INODE (source_dir_stats, dest_dir_stats)
74 && STREQ (base_name (source), base_name (dest)));