Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/memcoll.c @ 6912:314715e0260d
Merge from coreutils.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 03 Jul 2006 08:32:46 +0000 |
parents | 96c32553b4c6 |
children | 1388e7363d86 |
rev | line source |
---|---|
1869 | 1 /* Locale-specific memory comparison. |
5159 | 2 Copyright (C) 1999, 2002, 2003, 2004 Free Software Foundation, Inc. |
1869 | 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, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
1869 | 17 |
18 /* Contributed by Paul Eggert <eggert@twinsun.com>. */ | |
19 | |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
20 #ifdef HAVE_CONFIG_H |
1869 | 21 # include <config.h> |
22 #endif | |
23 | |
4347
df44e79ce676
.h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents:
4120
diff
changeset
|
24 #include "memcoll.h" |
df44e79ce676
.h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents:
4120
diff
changeset
|
25 |
3708
bd719c800d4b
Include errno.h, and declare errno if not defined.
Jim Meyering <jim@meyering.net>
parents:
2997
diff
changeset
|
26 #include <errno.h> |
4664 | 27 #include <string.h> |
1869 | 28 |
29 /* Compare S1 (with length S1LEN) and S2 (with length S2LEN) according | |
1992
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
30 to the LC_COLLATE locale. S1 and S2 do not overlap, and are not |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
31 adjacent. Perhaps temporarily modify the bytes after S1 and S2, |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
32 but restore their original contents before returning. Set errno to an |
3708
bd719c800d4b
Include errno.h, and declare errno if not defined.
Jim Meyering <jim@meyering.net>
parents:
2997
diff
changeset
|
33 error number if there is an error, and to zero otherwise. */ |
1869 | 34 int |
35 memcoll (char *s1, size_t s1len, char *s2, size_t s2len) | |
36 { | |
37 int diff; | |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
38 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
39 #if HAVE_STRCOLL |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
40 |
1992
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
41 char n1 = s1[s1len]; |
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
42 char n2 = s2[s2len]; |
1869 | 43 |
1992
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
44 s1[s1len++] = '\0'; |
da00e3cba254
(memcoll): The two arguments cannot be
Jim Meyering <jim@meyering.net>
parents:
1869
diff
changeset
|
45 s2[s2len++] = '\0'; |
1869 | 46 |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
47 while (! (errno = 0, (diff = strcoll (s1, s2)) || errno)) |
1869 | 48 { |
49 /* strcoll found no difference, but perhaps it was fooled by NUL | |
50 characters in the data. Work around this problem by advancing | |
51 past the NUL chars. */ | |
52 size_t size1 = strlen (s1) + 1; | |
53 size_t size2 = strlen (s2) + 1; | |
54 s1 += size1; | |
55 s2 += size2; | |
56 s1len -= size1; | |
57 s2len -= size2; | |
58 | |
59 if (s1len == 0) | |
60 { | |
61 if (s2len != 0) | |
62 diff = -1; | |
63 break; | |
64 } | |
65 else if (s2len == 0) | |
66 { | |
67 diff = 1; | |
68 break; | |
69 } | |
70 } | |
71 | |
72 s1[s1len - 1] = n1; | |
73 s2[s2len - 1] = n2; | |
74 | |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
75 #else |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
76 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
77 diff = memcmp (s1, s2, s1len < s2len ? s1len : s2len); |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
78 if (! diff) |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
79 diff = s1len < s2len ? -1 : s1len != s2len; |
4464
580114b1a865
(memcoll) [!HAVE_STRCOLL]: Clear errno.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4347
diff
changeset
|
80 errno = 0; |
4120
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
81 |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
82 #endif |
77d7a6d46166
(memcoll): Fall back on a simple algorithm using
Paul Eggert <eggert@cs.ucla.edu>
parents:
4104
diff
changeset
|
83 |
1869 | 84 return diff; |
85 } |