[backfire] uclibc: backport strverscmp(), versionsort() and versionsort64() from...
[openwrt-10.03/.git] / toolchain / uClibc / patches-0.9.30.2 / 903-backport-strverscmp_versionsort.patch
1 diff -urN a/include/dirent.h b/include/dirent.h
2 --- a/include/dirent.h  2011-03-14 09:54:12.980183443 +0300
3 +++ b/include/dirent.h  2011-03-14 10:07:34.290183418 +0300
4 @@ -286,6 +286,25 @@
5       __THROW __attribute_pure__ __nonnull ((1, 2));
6  # endif
7  
8 +/* Function to compare two `struct dirent's alphabetically.  */
9 +# ifndef __USE_FILE_OFFSET64
10 +extern int versionsort (__const void *__e1, __const void *__e2)
11 +     __THROW __attribute_pure__ __nonnull ((1, 2));
12 +# else
13 +#  ifdef __REDIRECT
14 +extern int __REDIRECT (versionsort,
15 +                          (__const void *__e1, __const void *__e2),
16 +                          versionsort64) __attribute_pure__ __nonnull ((1, 2));
17 +#  else
18 +#   define versionsort versionsort64
19 +#  endif
20 +# endif
21 +
22 +# if defined __USE_GNU && defined __USE_LARGEFILE64
23 +extern int versionsort64 (__const void *__e1, __const void *__e2)
24 +     __THROW __attribute_pure__ __nonnull ((1, 2));
25 +# endif
26 +
27  #endif /* Use BSD or misc.  */
28  
29  __END_DECLS
30 diff -urN a/include/string.h b/include/string.h
31 --- a/include/string.h  2011-03-14 09:54:12.982185223 +0300
32 +++ b/include/string.h  2011-03-14 10:08:45.551183543 +0300
33 @@ -442,11 +442,9 @@
34  
35  #ifdef __USE_GNU
36  /* Compare S1 and S2 as strings holding name & indices/version numbers.  */
37 -#if 0
38  extern int strverscmp (__const char *__s1, __const char *__s2)
39       __THROW __attribute_pure__ __nonnull ((1, 2));
40  libc_hidden_proto(strverscmp)
41 -#endif
42  
43  /* Return a string describing the meaning of the signal number in SIG.  */
44  extern char *strsignal (int __sig) __THROW;
45 diff -urN a/libc/misc/dirent/Makefile.in b/libc/misc/dirent/Makefile.in
46 --- a/libc/misc/dirent/Makefile.in      2011-03-14 09:54:13.071183374 +0300
47 +++ b/libc/misc/dirent/Makefile.in      2011-03-14 10:09:44.544183543 +0300
48 @@ -6,10 +6,10 @@
49  #
50  
51  CSRC :=        alphasort.c closedir.c dirfd.c opendir.c readdir.c rewinddir.c \
52 -       scandir.c seekdir.c telldir.c readdir_r.c
53 +       scandir.c seekdir.c telldir.c readdir_r.c versionsort.c
54  
55  ifeq ($(UCLIBC_HAS_LFS),y)
56 -CSRC +=        readdir64.c alphasort64.c scandir64.c readdir64_r.c
57 +CSRC +=        readdir64.c alphasort64.c scandir64.c readdir64_r.c versionsort64.c
58  endif
59  
60  MISC_DIRENT_DIR := $(top_srcdir)libc/misc/dirent
61 diff -urN a/libc/misc/dirent/versionsort64.c b/libc/misc/dirent/versionsort64.c
62 --- a/libc/misc/dirent/versionsort64.c  1970-01-01 03:00:00.000000000 +0300
63 +++ b/libc/misc/dirent/versionsort64.c  2011-03-14 10:11:05.459183378 +0300
64 @@ -0,0 +1,17 @@
65 +/*
66 + * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
67 + *
68 + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
69 + */
70 +
71 +#include <_lfs_64.h>
72 +
73 +#include <dirent.h>
74 +#include <string.h>
75 +#include "dirstream.h"
76 +
77 +int versionsort64(const void *a, const void *b)
78 +{
79 +       return strverscmp((*(const struct dirent64 **) a)->d_name,
80 +                       (*(const struct dirent64 **) b)->d_name);
81 +}
82 diff -urN a/libc/misc/dirent/versionsort.c b/libc/misc/dirent/versionsort.c
83 --- a/libc/misc/dirent/versionsort.c    1970-01-01 03:00:00.000000000 +0300
84 +++ b/libc/misc/dirent/versionsort.c    2011-03-14 10:10:33.191183386 +0300
85 @@ -0,0 +1,15 @@
86 +/*
87 + * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
88 + *
89 + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
90 + */
91 +
92 +#include <dirent.h>
93 +#include <string.h>
94 +#include "dirstream.h"
95 +
96 +int versionsort(const void *a, const void *b)
97 +{
98 +       return strverscmp((*(const struct dirent **) a)->d_name,
99 +                       (*(const struct dirent **) b)->d_name);
100 +}
101 diff -urN a/libc/string/strverscmp.c b/libc/string/strverscmp.c
102 --- a/libc/string/strverscmp.c  1970-01-01 03:00:00.000000000 +0300
103 +++ b/libc/string/strverscmp.c  2011-03-14 10:03:35.000000000 +0300
104 @@ -0,0 +1,118 @@
105 +/* GNU's strverscmp() function, taken from glibc 2.3.2 sources
106 + */
107 +
108 +/* Compare strings while treating digits characters numerically.
109 +   Copyright (C) 1997, 2002 Free Software Foundation, Inc.
110 +   This file is part of the GNU C Library.
111 +   Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
112 +
113 +   Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
114 +
115 +   The GNU C Library is free software; you can redistribute it and/or
116 +   modify it under the terms of the GNU Lesser General Public
117 +   License as published by the Free Software Foundation; either
118 +   version 2.1 of the License, or (at your option) any later version.
119 +
120 +   The GNU C Library is distributed in the hope that it will be useful,
121 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
122 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
123 +   Lesser General Public License for more details.
124 +
125 +   You should have received a copy of the GNU Lesser General Public
126 +   License along with the GNU C Library; if not, write to the Free
127 +   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
128 +   02111-1307 USA.  */
129 +
130 +#include <string.h>
131 +#include <ctype.h>
132 +#include <stdint.h>
133 +
134 +
135 +/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
136 +   fractional parts, S_Z: idem but with leading Zeroes only */
137 +#define  S_N    0x0
138 +#define  S_I    0x4
139 +#define  S_F    0x8
140 +#define  S_Z    0xC
141 +
142 +/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
143 +#define  CMP    2
144 +#define  LEN    3
145 +
146 +/* using more efficient isdigit() */
147 +#undef isdigit
148 +#define isdigit(a) ((unsigned)((a) - '0') <= 9)
149 +
150 +/* Compare S1 and S2 as strings holding indices/version numbers,
151 +   returning less than, equal to or greater than zero if S1 is less than,
152 +   equal to or greater than S2 (for more info, see the texinfo doc).
153 +*/
154 +int strverscmp (const char *s1, const char *s2)
155 +{
156 +  const unsigned char *p1 = (const unsigned char *) s1;
157 +  const unsigned char *p2 = (const unsigned char *) s2;
158 +  unsigned char c1, c2;
159 +  int state;
160 +  int diff;
161 +
162 +  /* Symbol(s)    0       [1-9]   others  (padding)
163 +     Transition   (10) 0  (01) d  (00) x  (11) -   */
164 +  static const uint8_t next_state[] =
165 +  {
166 +      /* state    x    d    0    - */
167 +      /* S_N */  S_N, S_I, S_Z, S_N,
168 +      /* S_I */  S_N, S_I, S_I, S_I,
169 +      /* S_F */  S_N, S_F, S_F, S_F,
170 +      /* S_Z */  S_N, S_F, S_Z, S_Z
171 +  };
172 +
173 +  static const int8_t result_type[] =
174 +  {
175 +      /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/-
176 +                 0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */
177 +
178 +      /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
179 +                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
180 +      /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP,
181 +                 +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP,
182 +      /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
183 +                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
184 +      /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP,
185 +                 -1,  CMP, CMP, CMP
186 +  };
187 +
188 +  if (p1 == p2)
189 +    return 0;
190 +
191 +  c1 = *p1++;
192 +  c2 = *p2++;
193 +  /* Hint: '0' is a digit too.  */
194 +  state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
195 +
196 +  while ((diff = c1 - c2) == 0 && c1 != '\0')
197 +    {
198 +      state = next_state[state];
199 +      c1 = *p1++;
200 +      c2 = *p2++;
201 +      state |= (c1 == '0') + (isdigit (c1) != 0);
202 +    }
203 +
204 +  state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
205 +
206 +  switch (state)
207 +  {
208 +    case CMP:
209 +      return diff;
210 +
211 +    case LEN:
212 +      while (isdigit (*p1++))
213 +       if (!isdigit (*p2++))
214 +         return 1;
215 +
216 +      return isdigit (*p2) ? -1 : diff;
217 +
218 +    default:
219 +      return state;
220 +  }
221 +}
222 +libc_hidden_def(strverscmp)