source: branches/backfire/toolchain/uClibc/patches-0.9.30.2/903-backport-strverscmp_versionsort.patch @ 26888

Last change on this file since 26888 was 26888, checked in by jow, 6 years ago

[backfire] uclibc: backport strverscmp(), versionsort() and versionsort64() from uClibc git, required by at least alsa-lib

File size: 7.3 KB
  • include/dirent.h

    diff -urN a/include/dirent.h b/include/dirent.h
    a b  
    286286     __THROW __attribute_pure__ __nonnull ((1, 2)); 
    287287# endif 
    288288 
     289/* Function to compare two `struct dirent's alphabetically.  */ 
     290# ifndef __USE_FILE_OFFSET64 
     291extern int versionsort (__const void *__e1, __const void *__e2) 
     292     __THROW __attribute_pure__ __nonnull ((1, 2)); 
     293# else 
     294#  ifdef __REDIRECT 
     295extern int __REDIRECT (versionsort, 
     296                           (__const void *__e1, __const void *__e2), 
     297                           versionsort64) __attribute_pure__ __nonnull ((1, 2)); 
     298#  else 
     299#   define versionsort versionsort64 
     300#  endif 
     301# endif 
     302 
     303# if defined __USE_GNU && defined __USE_LARGEFILE64 
     304extern int versionsort64 (__const void *__e1, __const void *__e2) 
     305     __THROW __attribute_pure__ __nonnull ((1, 2)); 
     306# endif 
     307 
    289308#endif /* Use BSD or misc.  */ 
    290309 
    291310__END_DECLS 
  • include/string.h

    diff -urN a/include/string.h b/include/string.h
    a b  
    442442 
    443443#ifdef  __USE_GNU 
    444444/* Compare S1 and S2 as strings holding name & indices/version numbers.  */ 
    445 #if 0 
    446445extern int strverscmp (__const char *__s1, __const char *__s2) 
    447446     __THROW __attribute_pure__ __nonnull ((1, 2)); 
    448447libc_hidden_proto(strverscmp) 
    449 #endif 
    450448 
    451449/* Return a string describing the meaning of the signal number in SIG.  */ 
    452450extern char *strsignal (int __sig) __THROW; 
  • libc/misc/dirent/Makefile.in

    diff -urN a/libc/misc/dirent/Makefile.in b/libc/misc/dirent/Makefile.in
    a b  
    66# 
    77 
    88CSRC := alphasort.c closedir.c dirfd.c opendir.c readdir.c rewinddir.c \ 
    9         scandir.c seekdir.c telldir.c readdir_r.c 
     9        scandir.c seekdir.c telldir.c readdir_r.c versionsort.c 
    1010 
    1111ifeq ($(UCLIBC_HAS_LFS),y) 
    12 CSRC += readdir64.c alphasort64.c scandir64.c readdir64_r.c 
     12CSRC += readdir64.c alphasort64.c scandir64.c readdir64_r.c versionsort64.c 
    1313endif 
    1414 
    1515MISC_DIRENT_DIR := $(top_srcdir)libc/misc/dirent 
  • libc/misc/dirent/versionsort64.c

    diff -urN a/libc/misc/dirent/versionsort64.c b/libc/misc/dirent/versionsort64.c
    a b  
     1/* 
     2 * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com> 
     3 * 
     4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. 
     5 */ 
     6 
     7#include <_lfs_64.h> 
     8 
     9#include <dirent.h> 
     10#include <string.h> 
     11#include "dirstream.h" 
     12 
     13int versionsort64(const void *a, const void *b) 
     14{ 
     15        return strverscmp((*(const struct dirent64 **) a)->d_name, 
     16                        (*(const struct dirent64 **) b)->d_name); 
     17} 
  • libc/misc/dirent/versionsort.c

    diff -urN a/libc/misc/dirent/versionsort.c b/libc/misc/dirent/versionsort.c
    a b  
     1/* 
     2 * Copyright (C) 2008-2009 Hai Zaar, Codefidence Ltd <haizaar@codefidence.com> 
     3 * 
     4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. 
     5 */ 
     6 
     7#include <dirent.h> 
     8#include <string.h> 
     9#include "dirstream.h" 
     10 
     11int versionsort(const void *a, const void *b) 
     12{ 
     13        return strverscmp((*(const struct dirent **) a)->d_name, 
     14                        (*(const struct dirent **) b)->d_name); 
     15} 
  • libc/string/strverscmp.c

    diff -urN a/libc/string/strverscmp.c b/libc/string/strverscmp.c
    a b  
     1/* GNU's strverscmp() function, taken from glibc 2.3.2 sources 
     2 */ 
     3 
     4/* Compare strings while treating digits characters numerically. 
     5   Copyright (C) 1997, 2002 Free Software Foundation, Inc. 
     6   This file is part of the GNU C Library. 
     7   Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997. 
     8 
     9   Derived work for uClibc by Hai Zaar, Codefidence Ltd <haizaar@codefidence.com> 
     10 
     11   The GNU C Library is free software; you can redistribute it and/or 
     12   modify it under the terms of the GNU Lesser General Public 
     13   License as published by the Free Software Foundation; either 
     14   version 2.1 of the License, or (at your option) any later version. 
     15 
     16   The GNU C Library is distributed in the hope that it will be useful, 
     17   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
     19   Lesser General Public License for more details. 
     20 
     21   You should have received a copy of the GNU Lesser General Public 
     22   License along with the GNU C Library; if not, write to the Free 
     23   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
     24   02111-1307 USA.  */ 
     25 
     26#include <string.h> 
     27#include <ctype.h> 
     28#include <stdint.h> 
     29 
     30 
     31/* states: S_N: normal, S_I: comparing integral part, S_F: comparing 
     32   fractional parts, S_Z: idem but with leading Zeroes only */ 
     33#define  S_N    0x0 
     34#define  S_I    0x4 
     35#define  S_F    0x8 
     36#define  S_Z    0xC 
     37 
     38/* result_type: CMP: return diff; LEN: compare using len_diff/diff */ 
     39#define  CMP    2 
     40#define  LEN    3 
     41 
     42/* using more efficient isdigit() */ 
     43#undef isdigit 
     44#define isdigit(a) ((unsigned)((a) - '0') <= 9) 
     45 
     46/* Compare S1 and S2 as strings holding indices/version numbers, 
     47   returning less than, equal to or greater than zero if S1 is less than, 
     48   equal to or greater than S2 (for more info, see the texinfo doc). 
     49*/ 
     50int strverscmp (const char *s1, const char *s2) 
     51{ 
     52  const unsigned char *p1 = (const unsigned char *) s1; 
     53  const unsigned char *p2 = (const unsigned char *) s2; 
     54  unsigned char c1, c2; 
     55  int state; 
     56  int diff; 
     57 
     58  /* Symbol(s)    0       [1-9]   others  (padding) 
     59     Transition   (10) 0  (01) d  (00) x  (11) -   */ 
     60  static const uint8_t next_state[] = 
     61  { 
     62      /* state    x    d    0    - */ 
     63      /* S_N */  S_N, S_I, S_Z, S_N, 
     64      /* S_I */  S_N, S_I, S_I, S_I, 
     65      /* S_F */  S_N, S_F, S_F, S_F, 
     66      /* S_Z */  S_N, S_F, S_Z, S_Z 
     67  }; 
     68 
     69  static const int8_t result_type[] = 
     70  { 
     71      /* state   x/x  x/d  x/0  x/-  d/x  d/d  d/0  d/- 
     72                 0/x  0/d  0/0  0/-  -/x  -/d  -/0  -/- */ 
     73 
     74      /* S_N */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, 
     75                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, 
     76      /* S_I */  CMP, -1,  -1,  CMP, +1,  LEN, LEN, CMP, 
     77                 +1,  LEN, LEN, CMP, CMP, CMP, CMP, CMP, 
     78      /* S_F */  CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP, 
     79                 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, 
     80      /* S_Z */  CMP, +1,  +1,  CMP, -1,  CMP, CMP, CMP, 
     81                 -1,  CMP, CMP, CMP 
     82  }; 
     83 
     84  if (p1 == p2) 
     85    return 0; 
     86 
     87  c1 = *p1++; 
     88  c2 = *p2++; 
     89  /* Hint: '0' is a digit too.  */ 
     90  state = S_N | ((c1 == '0') + (isdigit (c1) != 0)); 
     91 
     92  while ((diff = c1 - c2) == 0 && c1 != '\0') 
     93    { 
     94      state = next_state[state]; 
     95      c1 = *p1++; 
     96      c2 = *p2++; 
     97      state |= (c1 == '0') + (isdigit (c1) != 0); 
     98    } 
     99 
     100  state = result_type[state << 2 | (((c2 == '0') + (isdigit (c2) != 0)))]; 
     101 
     102  switch (state) 
     103  { 
     104    case CMP: 
     105      return diff; 
     106 
     107    case LEN: 
     108      while (isdigit (*p1++)) 
     109        if (!isdigit (*p2++)) 
     110          return 1; 
     111 
     112      return isdigit (*p2) ? -1 : diff; 
     113 
     114    default: 
     115      return state; 
     116  } 
     117} 
     118libc_hidden_def(strverscmp) 
Note: See TracBrowser for help on using the repository browser.