include cleanups: remove unused headers and add feature test macros
[musl] / src / string / strverscmp.c
1 #define _GNU_SOURCE
2 #include <ctype.h>
3 #include <string.h>
4
5 int strverscmp(const char *l, const char *r)
6 {
7         int haszero=1;
8         while (*l==*r) {
9                 if (!*l) return 0;
10
11                 if (*l=='0') {
12                         if (haszero==1) {
13                                 haszero=0;
14                         }
15                 } else if (isdigit(*l)) {
16                         if (haszero==1) {
17                                 haszero=2;
18                         }
19                 } else {
20                         haszero=1;
21                 }
22                 l++; r++;
23         }
24         if (haszero==1 && (*l=='0' || *r=='0')) {
25                 haszero=0;
26         }
27         if ((isdigit(*l) && isdigit(*r) ) && haszero) {
28                 size_t lenl=0, lenr=0;
29                 while (isdigit(l[lenl]) ) lenl++;
30                 while (isdigit(r[lenr]) ) lenr++;
31                 if (lenl==lenr) {
32                         return (*l -  *r);
33                 } else if (lenl>lenr) {
34                         return 1;
35                 } else {
36                         return -1;
37                 }
38         } else {
39                 return (*l -  *r);
40         }
41 }