-/* Copyright 1998 by Andi Kleen. Subject to the GPL. */
-/* $Id: nstrcmp.c,v 1.2 1998/11/15 20:11:38 freitag Exp $ */
+/*
+ * Copyright 1998 by Andi Kleen. Subject to the GPL.
+ * Copyright 2002 by Bruno Hall who contributed a shorter rewrite
+ * which actually works
+ *
+ * $Id: nstrcmp.c,v 1.3 2002/12/10 00:37:33 ecki Exp $
+ */
#include <ctype.h>
#include <stdlib.h>
#include "util.h"
+
/* like strcmp(), but knows about numbers */
-int nstrcmp(const char *astr, const char *b)
+int nstrcmp(const char *a, const char *b)
{
- const char *a = astr;
-
- while (*a == *b) {
- if (*a == '\0')
- return 0;
- a++;
- b++;
- }
- if (isdigit(*a)) {
- if (!isdigit(*b))
- return -1;
- while (a > astr) {
- a--;
- if (!isdigit(*a)) {
- a++;
- break;
- }
- if (!isdigit(*b))
- return -1;
- b--;
- }
- return atoi(a) > atoi(b) ? 1 : -1;
+ while (*a == *b && !isdigit(*a)) {
+ if (*a++ == 0)
+ return 0;
+ b++;
}
- return *a - *b;
+ if (isdigit(*a) && isdigit(*b))
+ return atoi(a) - atoi(b);
+ return *(const unsigned char *)a - *(const unsigned char *)b;
}