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
5 __THROW __attribute_pure__ __nonnull ((1, 2));
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));
14 +extern int __REDIRECT (versionsort,
15 + (__const void *__e1, __const void *__e2),
16 + versionsort64) __attribute_pure__ __nonnull ((1, 2));
18 +# define versionsort versionsort64
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));
27 #endif /* Use BSD or misc. */
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
36 /* Compare S1 and S2 as strings holding name & indices/version numbers. */
38 extern int strverscmp (__const char *__s1, __const char *__s2)
39 __THROW __attribute_pure__ __nonnull ((1, 2));
40 libc_hidden_proto(strverscmp)
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
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
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
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
66 + * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
68 + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
75 +#include "dirstream.h"
77 +int versionsort64(const void *a, const void *b)
79 + return strverscmp((*(const struct dirent64 **) a)->d_name,
80 + (*(const struct dirent64 **) b)->d_name);
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
87 + * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
89 + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
94 +#include "dirstream.h"
96 +int versionsort(const void *a, const void *b)
98 + return strverscmp((*(const struct dirent **) a)->d_name,
99 + (*(const struct dirent **) b)->d_name);
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
105 +/* GNU's strverscmp() function, taken from glibc 2.3.2 sources
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.
113 + Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com>
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.
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.
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
135 +/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
136 + fractional parts, S_Z: idem but with leading Zeroes only */
142 +/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
146 +/* using more efficient isdigit() */
148 +#define isdigit(a) ((unsigned)((a) - '0') <= 9)
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).
154 +int strverscmp (const char *s1, const char *s2)
156 + const unsigned char *p1 = (const unsigned char *) s1;
157 + const unsigned char *p2 = (const unsigned char *) s2;
158 + unsigned char c1, c2;
162 + /* Symbol(s) 0 [1-9] others (padding)
163 + Transition (10) 0 (01) d (00) x (11) - */
164 + static const uint8_t next_state[] =
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
173 + static const int8_t result_type[] =
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 -/- */
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,
193 + /* Hint: '0' is a digit too. */
194 + state = S_N | ((c1 == '0') + (isdigit (c1) != 0));
196 + while ((diff = c1 - c2) == 0 && c1 != '\0')
198 + state = next_state[state];
201 + state |= (c1 == '0') + (isdigit (c1) != 0);
204 + state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))];
212 + while (isdigit (*p1++))
213 + if (!isdigit (*p2++))
216 + return isdigit (*p2) ? -1 : diff;
222 +libc_hidden_def(strverscmp)