xref: /titanic_41/usr/src/lib/libast/common/string/strnvcmp.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
134f9b3eeSRoland Mainz /***********************************************************************
234f9b3eeSRoland Mainz *                                                                      *
334f9b3eeSRoland Mainz *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
534f9b3eeSRoland Mainz *                      and is licensed under the                       *
634f9b3eeSRoland Mainz *                  Common Public License, Version 1.0                  *
734f9b3eeSRoland Mainz *                    by AT&T Intellectual Property                     *
834f9b3eeSRoland Mainz *                                                                      *
934f9b3eeSRoland Mainz *                A copy of the License is available at                 *
1034f9b3eeSRoland Mainz *            http://www.opensource.org/licenses/cpl1.0.txt             *
1134f9b3eeSRoland Mainz *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
1234f9b3eeSRoland Mainz *                                                                      *
1334f9b3eeSRoland Mainz *              Information and Software Systems Research               *
1434f9b3eeSRoland Mainz *                            AT&T Research                             *
1534f9b3eeSRoland Mainz *                           Florham Park NJ                            *
1634f9b3eeSRoland Mainz *                                                                      *
1734f9b3eeSRoland Mainz *                 Glenn Fowler <gsf@research.att.com>                  *
1834f9b3eeSRoland Mainz *                  David Korn <dgk@research.att.com>                   *
1934f9b3eeSRoland Mainz *                   Phong Vo <kpv@research.att.com>                    *
2034f9b3eeSRoland Mainz *                                                                      *
2134f9b3eeSRoland Mainz ***********************************************************************/
2234f9b3eeSRoland Mainz #pragma prototyped
2334f9b3eeSRoland Mainz 
2434f9b3eeSRoland Mainz #include <ast.h>
2534f9b3eeSRoland Mainz #include <ctype.h>
2634f9b3eeSRoland Mainz 
2734f9b3eeSRoland Mainz /*
2834f9b3eeSRoland Mainz  * version strncmp(3)
2934f9b3eeSRoland Mainz  */
3034f9b3eeSRoland Mainz 
3134f9b3eeSRoland Mainz int
strnvcmp(register const char * a,register const char * b,size_t n)3234f9b3eeSRoland Mainz strnvcmp(register const char* a, register const char* b, size_t n)
3334f9b3eeSRoland Mainz {
3434f9b3eeSRoland Mainz 	register const char*	ae;
3534f9b3eeSRoland Mainz 	register const char*	be;
3634f9b3eeSRoland Mainz 	register unsigned long	na;
3734f9b3eeSRoland Mainz 	register unsigned long	nb;
3834f9b3eeSRoland Mainz 
3934f9b3eeSRoland Mainz 	ae = a + n;
4034f9b3eeSRoland Mainz 	be = b + n;
4134f9b3eeSRoland Mainz 	for (;;)
4234f9b3eeSRoland Mainz 	{
4334f9b3eeSRoland Mainz 		if (a >= ae)
4434f9b3eeSRoland Mainz 		{
4534f9b3eeSRoland Mainz 			if (b >= be)
4634f9b3eeSRoland Mainz 				return 0;
4734f9b3eeSRoland Mainz 			return 1;
4834f9b3eeSRoland Mainz 		}
4934f9b3eeSRoland Mainz 		else if (b >= be)
5034f9b3eeSRoland Mainz 			return -1;
5134f9b3eeSRoland Mainz 		if (isdigit(*a) && isdigit(*b))
5234f9b3eeSRoland Mainz 		{
5334f9b3eeSRoland Mainz 			na = nb = 0;
5434f9b3eeSRoland Mainz 			while (a < ae && isdigit(*a))
5534f9b3eeSRoland Mainz 				na = na * 10 + *a++ - '0';
5634f9b3eeSRoland Mainz 			while (b < be && isdigit(*b))
5734f9b3eeSRoland Mainz 				nb = nb * 10 + *b++ - '0';
5834f9b3eeSRoland Mainz 			if (na < nb)
5934f9b3eeSRoland Mainz 				return -1;
6034f9b3eeSRoland Mainz 			if (na > nb)
6134f9b3eeSRoland Mainz 				return 1;
6234f9b3eeSRoland Mainz 		}
6334f9b3eeSRoland Mainz 		else if (*a != *b)
6434f9b3eeSRoland Mainz 			break;
6534f9b3eeSRoland Mainz 		else if (!*a)
6634f9b3eeSRoland Mainz 			return 0;
6734f9b3eeSRoland Mainz 		else
6834f9b3eeSRoland Mainz 		{
6934f9b3eeSRoland Mainz 			a++;
7034f9b3eeSRoland Mainz 			b++;
7134f9b3eeSRoland Mainz 		}
7234f9b3eeSRoland Mainz 	}
7334f9b3eeSRoland Mainz 	if (*a == 0)
7434f9b3eeSRoland Mainz 		return -1;
7534f9b3eeSRoland Mainz 	if (*b == 0)
7634f9b3eeSRoland Mainz 		return 1;
7734f9b3eeSRoland Mainz 	if (*a == '.')
7834f9b3eeSRoland Mainz 		return -1;
7934f9b3eeSRoland Mainz 	if (*b == '.')
8034f9b3eeSRoland Mainz 		return 1;
8134f9b3eeSRoland Mainz 	if (*a == '-')
8234f9b3eeSRoland Mainz 		return -1;
8334f9b3eeSRoland Mainz 	if (*b == '-')
8434f9b3eeSRoland Mainz 		return 1;
8534f9b3eeSRoland Mainz 	return *a < *b ? -1 : 1;
8634f9b3eeSRoland Mainz }
87