Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/same.c @ 16846:a80d21de5373
system-quote, execute, spawn-pipe: Escape '?' on Windows.
* lib/system-quote.c (SHELL_SPECIAL_CHARS, CMD_SPECIAL_CHARS): Add the
'?' character.
* lib/w32spawn.h (SHELL_SPECIAL_CHARS): Likewise.
* tests/test-system-quote-main.c (check_all): Check also strings like
"??????????".
Reported by Eli Zaretskii <eliz@gnu.org>.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 11 May 2012 01:39:04 +0200 |
parents | 8250f2777afc |
children | e542fd46ad6f |
rev | line source |
---|---|
2092
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
1 /* Determine whether two file names refer to the same file. |
5135
46d5c1c09035
(same_name): Return bool, not int.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4910
diff
changeset
|
2 |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
3 Copyright (C) 1997-2000, 2002-2006, 2009-2012 Free Software Foundation, Inc. |
2092
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
2092
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 (at your option) any later version. |
2092
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
9 |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
13 GNU General Public License for more details. |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
14 |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
2092
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
17 |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
18 /* written by Jim Meyering */ |
0cf0dc1ce656
add copyright/no-warranty comment
Jim Meyering <jim@meyering.net>
parents:
1857
diff
changeset
|
19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6912
diff
changeset
|
20 #include <config.h> |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
21 |
4910 | 22 #include <stdbool.h> |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
23 #include <stdio.h> |
6275 | 24 #include <unistd.h> |
4674 | 25 #include <stdlib.h> |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
26 #include <sys/types.h> |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
27 #include <sys/stat.h> |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
28 #include <ctype.h> |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
29 #include <errno.h> |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
30 |
4674 | 31 #include <string.h> |
2671
db2d95fa362f
Include <string.h> or <strings.h>, as appropriate, for declaration of strcmp.
Jim Meyering <jim@meyering.net>
parents:
2293
diff
changeset
|
32 |
4910 | 33 #include <limits.h> |
34 #ifndef _POSIX_NAME_MAX | |
35 # define _POSIX_NAME_MAX 14 | |
36 #endif | |
37 | |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
38 #include "same.h" |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
39 #include "dirname.h" |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
40 #include "error.h" |
6912 | 41 #include "same-inode.h" |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
42 |
5491
b3d5c90efc81
Merge from coreutils for getcwd and HP-UX 11.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5135
diff
changeset
|
43 #ifndef MIN |
b3d5c90efc81
Merge from coreutils for getcwd and HP-UX 11.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5135
diff
changeset
|
44 # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
b3d5c90efc81
Merge from coreutils for getcwd and HP-UX 11.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5135
diff
changeset
|
45 #endif |
4910 | 46 |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
47 /* Return nonzero if SOURCE and DEST point to the same name in the same |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
48 directory. */ |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
49 |
5135
46d5c1c09035
(same_name): Return bool, not int.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4910
diff
changeset
|
50 bool |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
51 same_name (const char *source, const char *dest) |
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
52 { |
4367 | 53 /* Compare the basenames. */ |
6912 | 54 char const *source_basename = last_component (source); |
55 char const *dest_basename = last_component (dest); | |
4367 | 56 size_t source_baselen = base_len (source_basename); |
57 size_t dest_baselen = base_len (dest_basename); | |
4910 | 58 bool identical_basenames = |
59 (source_baselen == dest_baselen | |
60 && memcmp (source_basename, dest_basename, dest_baselen) == 0); | |
61 bool compare_dirs = identical_basenames; | |
62 bool same = false; | |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
63 |
4910 | 64 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX |
5907 | 65 /* This implementation silently truncates components of file names. If |
4910 | 66 the base names might be truncated, check whether the truncated |
67 base names are the same, while checking the directories. */ | |
68 size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX; | |
69 size_t min_baselen = MIN (source_baselen, dest_baselen); | |
70 if (slen_max <= min_baselen | |
71 && memcmp (source_basename, dest_basename, slen_max) == 0) | |
72 compare_dirs = true; | |
73 #endif | |
74 | |
75 if (compare_dirs) | |
4179
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
76 { |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
77 struct stat source_dir_stats; |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
78 struct stat dest_dir_stats; |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
79 char *source_dirname, *dest_dirname; |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
80 |
4181 | 81 /* Compare the parent directories (via the device and inode numbers). */ |
4179
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
82 source_dirname = dir_name (source); |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
83 dest_dirname = dir_name (dest); |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
84 |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
85 if (stat (source_dirname, &source_dir_stats)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
86 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
87 /* Shouldn't happen. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
88 error (1, errno, "%s", source_dirname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
89 } |
4179
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
90 |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
91 if (stat (dest_dirname, &dest_dir_stats)) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
92 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
93 /* Shouldn't happen. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
94 error (1, errno, "%s", dest_dirname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
95 } |
4179
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
96 |
4910 | 97 same = SAME_INODE (source_dir_stats, dest_dir_stats); |
98 | |
99 #if ! _POSIX_NO_TRUNC && HAVE_PATHCONF && defined _PC_NAME_MAX | |
100 if (same && ! identical_basenames) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
101 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
102 long name_max = (errno = 0, pathconf (dest_dirname, _PC_NAME_MAX)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
103 if (name_max < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
104 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
105 if (errno) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
106 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
107 /* Shouldn't happen. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
108 error (1, errno, "%s", dest_dirname); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
109 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
110 same = false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
111 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
112 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
113 same = (name_max <= min_baselen |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
114 && memcmp (source_basename, dest_basename, name_max) == 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12069
diff
changeset
|
115 } |
4910 | 116 #endif |
117 | |
4179
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
118 free (source_dirname); |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
119 free (dest_dirname); |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
120 } |
7dd6887fa0db
Optimization: Avoid unnecessary stat() calls.
Bruno Haible <bruno@clisp.org>
parents:
3966
diff
changeset
|
121 |
4910 | 122 return same; |
1857
f47e2d0bff02
New file (function extracted from ln.c).
Jim Meyering <jim@meyering.net>
parents:
diff
changeset
|
123 } |