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