File nstrcmp.c of Package net-tools

/* 
 * 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 <string.h>
#include "util.h"


/* like strcmp(), but knows about numbers */
/* basically splits the string up in non-numerical and numerical
 * parts, compares the ascii values with their character values
 * and the numbers by their numerical value.
 */
int nstrcmp(const char *a, const char *b)
{
    int nra, nrb;
    const char *na, *nb;

    /* skip equal chars */
    while (*a == *b && !isdigit(*a)) {
	if (*a++ == 0)
	    return 0;
	b++;
    }

    /* compare numbers */
    if (isdigit(*a) && isdigit(*b)) {
	nra = strtoul(a,&na,10);
	nrb = strtoul(b,&nb,10);

	/* different interfaces (eth1 vs eth2) */
	if (nra != nrb)
	    return nra - nrb;

	/* no sub interface ( eth0 / eth0 ) */
	if ((*na == 0) && (*nb == 0))
	    return 0;
	a = na;
	b = nb;
    }
    /* While there might be more numbers to come, the kernel
     * now takes them as strings.
     * eth1:blubber and eth1:0 and eth1:00 are all different things.
     */
    return strcmp(a,b);
}
openSUSE Build Service is sponsored by