File: nstrcmp.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (156 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Copyright 1998 by Andi Kleen. Subject to the GPL. */
/* rewritten by bernd eckenfels because of complicated alias semantic */
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"


/* return numerical :999 suffix or null. sideeffect: replace ':' with \0 */
char* cutalias(char* name)
{
	int digit = 0;
	int pos;

	for(pos=strlen(name); pos>0; pos--)
	{
		if (name[pos-1]==':' && digit)
		{
			name[pos-1]='\0';
			return name+pos;
		}
		if (!isdigit(name[pos-1]))
			break;
		digit = 1;
	}
	return NULL;
}


/* return index of last non digit or -1 if it does not end with digits */
int rindex_nondigit(char *name)
{
	int pos = strlen(name);

	for(pos=strlen(name); pos>0; pos--)
	{
		if (!isdigit(name[pos-1]))
			return pos;
	}
	return 0;
}


/* like strcmp(), but knows about numbers and ':' alias suffix */
int nstrcmp(const char *ap, const char *bp)
{
	char *a = (char*)strdup(ap);
	char *b = (char*)strdup(bp);
	char *an, *bn;
	int av = 0, bv = 0;
	char *aalias=cutalias(a);
	char *balias=cutalias(b);
	int aindex=rindex_nondigit(a);
	int bindex=rindex_nondigit(b);
	int complen=(aindex<bindex)?aindex:bindex;
	int res = strncmp(a, b, complen);

	if (res != 0)
		{ free(a); free(b); return res; }

	if (aindex > bindex)
		{ free(a); free(b); return 1; }

	if (aindex < bindex)
		{ free(a); free(b); return -1; }

	an = a+aindex;
	bn = b+bindex;

	av = atoi(an);
	bv = atoi(bn);

	if (av < bv)
		{ free(a); free(b); return -1; }

	if (av > bv)
		{ free(a); free(b); return 1; }

	av = -1;
	if (aalias != NULL)
		av = atoi(aalias);

	bv = -1;
	if (balias != NULL)
		bv = atoi(balias);

	free(a); free(b);

	if (av < bv)
		return -1;

	if (av > bv)
		return 1;

	return 0;
}


#ifdef NSTRCMP_TEST

int cs(int s)
{
	if (s < 0) return -1;
	if (s > 0) return 1;
	return 0;
}


int dotest(char* a, char* b, int exp)
{
	int res = nstrcmp(a, b);
	int err = (cs(res) != cs(exp));
	printf("nstrcmp(\"%s\", \"%s\")=%d %d %s\n", a, b, res, exp, err?"WRONG":"OK");
	return err;
}

int main()
{
	int err = 0;

	err |= dotest("eth1", "eth1", 0);
	err |= dotest("eth0:1", "eth0:1", 0);
	err |= dotest("lan", "lan", 0);
	err |= dotest("100", "100", 0);
	err |= dotest("", "", 0);
	err |= dotest(":", ":", 0);
	err |= dotest("a:b:c", "a:b:c", 0);
	err |= dotest("a:", "a:", 0);
	err |= dotest(":a", ":a", 0);

	err |= dotest("a", "aa", -1);
	err |= dotest("eth0", "eth1", -1);
	err |= dotest("eth1", "eth20", -1);
	err |= dotest("eth20", "eth100", -1);
	err |= dotest("eth1", "eth13", -1);
	err |= dotest("eth", "eth2", -1);
	err |= dotest("eth0:1", "eth0:2", -1);
	err |= dotest("eth1:10", "eth13:10", -1);
	err |= dotest("eth1:1", "eth1:13", -1);
	err |= dotest("a", "a:", -1);

	err |= dotest("aa", "a", 1);
	err |= dotest("eth2", "eth1", 1);
	err |= dotest("eth13", "eth1", 1);
	err |= dotest("eth2", "eth", 1);
	err |= dotest("eth2:10", "eth2:1", 1);
	err |= dotest("eth2:5", "eth2:4", 1);
	err |= dotest("eth3:2", "eth2:3", 1);
	err |= dotest("eth13:1", "eth1:0", 1);
	err |= dotest("a:", "a", 1);
	err |= dotest("a1b12", "a1b2", 1);

	return err;
}

#endif