daily update
[deliverable/binutils-gdb.git] / libiberty / filename_cmp.c
CommitLineData
9c577e89
DD
1/* File name comparison routine.
2
3 Copyright (C) 2007 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
18
2657faa6
AS
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
9c577e89
DD
23#ifdef HAVE_STRING_H
24#include <string.h>
25#endif
26
9c577e89 27#include "filenames.h"
73bdefcf 28#include "safe-ctype.h"
9c577e89
DD
29
30/*
31
32@deftypefn Extension int filename_cmp (const char *@var{s1}, const char *@var{s2})
33
34d8f01b
JB
34Return zero if the two file names @var{s1} and @var{s2} are equivalent.
35If not equivalent, the returned value is similar to what @code{strcmp}
36would return. In other words, it returns a negative value if @var{s1}
37is less than @var{s2}, or a positive value if @var{s2} is greater than
38@var{s2}.
9c577e89 39
34d8f01b 40This function does not normalize file names. As a result, this function
9c577e89
DD
41will treat filenames that are spelled differently as different even in
42the case when the two filenames point to the same underlying file.
43However, it does handle the fact that on DOS-like file systems, forward
44and backward slashes are equal.
45
46@end deftypefn
47
48*/
49
50int
51filename_cmp (const char *s1, const char *s2)
52{
53#ifndef HAVE_DOS_BASED_FILE_SYSTEM
54 return strcmp(s1, s2);
55#else
56 for (;;)
57 {
73bdefcf
JB
58 int c1 = TOLOWER (*s1);
59 int c2 = TOLOWER (*s2);
9c577e89
DD
60
61 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
62 if (c1 == '/')
63 c1 = '\\';
64 if (c2 == '/')
65 c2 = '\\';
66
67 if (c1 != c2)
68 return (c1 - c2);
69
70 if (c1 == '\0')
71 return 0;
72
73 s1++;
74 s2++;
75 }
76#endif
77}
78
007d6189
KT
79/*
80
81@deftypefn Extension int filename_ncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})
82
83Return zero if the two file names @var{s1} and @var{s2} are equivalent
84in range @var{n}.
85If not equivalent, the returned value is similar to what @code{strncmp}
86would return. In other words, it returns a negative value if @var{s1}
87is less than @var{s2}, or a positive value if @var{s2} is greater than
88@var{s2}.
89
90This function does not normalize file names. As a result, this function
91will treat filenames that are spelled differently as different even in
92the case when the two filenames point to the same underlying file.
93However, it does handle the fact that on DOS-like file systems, forward
94and backward slashes are equal.
95
96@end deftypefn
97
98*/
99
100int
101filename_ncmp (const char *s1, const char *s2, size_t n)
102{
103#ifndef HAVE_DOS_BASED_FILE_SYSTEM
104 return strncmp(s1, s2, n);
105#else
106 if (!n)
107 return 0;
108 for (; n > 0; --n)
109 {
110 int c1 = TOLOWER (*s1);
111 int c2 = TOLOWER (*s2);
112
113 /* On DOS-based file systems, the '/' and the '\' are equivalent. */
114 if (c1 == '/')
115 c1 = '\\';
116 if (c2 == '/')
117 c2 = '\\';
118
119 if (c1 == '\0' || c1 != c2)
120 return (c1 - c2);
121
122 s1++;
123 s2++;
124 }
125 return 0;
126#endif
127}
This page took 0.170285 seconds and 4 git commands to generate.