about summary refs log tree commit diff
path: root/src/string
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-02-26 01:36:47 -0500
committerRich Felker <dalias@aerifal.cx>2013-02-26 01:36:47 -0500
commit4853c1f7f7b5023aa6a409abc1e759f5f92c9c4e (patch)
treea54c11d7f2e156e3716fb9c8bf19bbee55641d25 /src/string
parentd1eae83a593309842550988e9c56492fac9cdece (diff)
downloadmusl-4853c1f7f7b5023aa6a409abc1e759f5f92c9c4e.tar.gz
musl-4853c1f7f7b5023aa6a409abc1e759f5f92c9c4e.tar.xz
musl-4853c1f7f7b5023aa6a409abc1e759f5f92c9c4e.zip
implement non-stub strverscmp
patch by Isaac Dunham.
Diffstat (limited to 'src/string')
-rw-r--r--src/string/strverscmp.c37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/string/strverscmp.c b/src/string/strverscmp.c
index 70549678..33a42eed 100644
--- a/src/string/strverscmp.c
+++ b/src/string/strverscmp.c
@@ -1,7 +1,40 @@
+#define _GNU_SOURCE
+#include <ctype.h>
 #include <string.h>
+#include <sys/types.h>
 
 int strverscmp(const char *l, const char *r)
 {
-	/* FIXME */
-	return strcmp(l, r);
+	int haszero=1;
+	while (*l==*r) {
+		if (!*l) return 0;
+
+		if (*l=='0') {
+			if (haszero==1) {
+				haszero=0;
+			}
+		} else if (isdigit(*l)) {
+			if (haszero==1) {
+				haszero=2;
+			}
+		} else {
+			haszero=1;
+		}
+		l++; r++;
+	}
+	if (haszero==1 && (*l=='0' || *r=='0')) {
+		haszero=0;
+	}
+	if ((isdigit(*l) && isdigit(*r) ) && haszero) {
+		size_t lenl=0, lenr=0;
+		while (isdigit(l[lenl]) ) lenl++;
+		while (isdigit(r[lenr]) ) lenr++;
+		if (lenl==lenr) {
+			return (*l -  *r);
+		} else {
+			return (lenl - lenr);
+		}
+	} else {
+		return (*l -  *r);
+	}
 }