xref: /titanic_51/usr/src/lib/libast/common/string/strnvcmp.c (revision dbed73cbda2229fd1aa6dc5743993cae7f0a7ee9)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 #include <ast.h>
25 #include <ctype.h>
26 
27 /*
28  * version strncmp(3)
29  */
30 
31 int
32 strnvcmp(register const char* a, register const char* b, size_t n)
33 {
34 	register const char*	ae;
35 	register const char*	be;
36 	register unsigned long	na;
37 	register unsigned long	nb;
38 
39 	ae = a + n;
40 	be = b + n;
41 	for (;;)
42 	{
43 		if (a >= ae)
44 		{
45 			if (b >= be)
46 				return 0;
47 			return 1;
48 		}
49 		else if (b >= be)
50 			return -1;
51 		if (isdigit(*a) && isdigit(*b))
52 		{
53 			na = nb = 0;
54 			while (a < ae && isdigit(*a))
55 				na = na * 10 + *a++ - '0';
56 			while (b < be && isdigit(*b))
57 				nb = nb * 10 + *b++ - '0';
58 			if (na < nb)
59 				return -1;
60 			if (na > nb)
61 				return 1;
62 		}
63 		else if (*a != *b)
64 			break;
65 		else if (!*a)
66 			return 0;
67 		else
68 		{
69 			a++;
70 			b++;
71 		}
72 	}
73 	if (*a == 0)
74 		return -1;
75 	if (*b == 0)
76 		return 1;
77 	if (*a == '.')
78 		return -1;
79 	if (*b == '.')
80 		return 1;
81 	if (*a == '-')
82 		return -1;
83 	if (*b == '-')
84 		return 1;
85 	return *a < *b ? -1 : 1;
86 }
87