summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/libpkgver/colcomp.c
blob: 4b151e3faf62ba775fd3e4b4fcc9a67f3c4d49e8 (plain)
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
/* COLLATE COMPARE, COMPARES DIGITS NUMERICALLY AND OTHERS IN ASCII */

/*
 *   Copyright 2001, 2015, Harlan Stenn.  Used by NTP with permission.
 *
 *   Author: Harlan Stenn <harlan@pfcs.com>
 *
 *   Copying and distribution of this file, with or without modification,
 *   are permitted in any medium without royalty provided the copyright
 *   notice and this notice are preserved. This file is offered as-is,
 *   without any warranty.
 */

/*
 * Expected collate order for numeric "pieces" is:
 * 0 - 9	followed by
 * 00 - 99	followed by
 * 000 - 999	followed by
 * ...
 */

#include <ctype.h>

/*
 * Older versions of isdigit() require the argument be isascii()
 */

#if 0
# define MyIsDigit(x)	\
      (isascii ((unsigned char) (x)) && isdigit ((unsigned char) (x)))
#else
# define MyIsDigit(x)	isdigit ((unsigned char) (x))
#endif


int 
colcomp (s1, s2)
     register char *s1;
     register char *s2;
{
  int hilo = 0;			/* comparison value */

  while (*s1 && *s2)
    {
      if  (  MyIsDigit(*s1)
          && MyIsDigit(*s2))
	{
	  hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
	  ++s1;
	  ++s2;
	  while (MyIsDigit(*s1)
	     &&  MyIsDigit(*s2))
	    {
	      if (!hilo)
		hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
	      ++s1;
	      ++s2;
	    }
	  if (MyIsDigit(*s1))
	    hilo = 1;		/* s2 is first */
	  if (MyIsDigit(*s2))
	    hilo = -1;		/* s1 is first */
	  if (hilo)
	    break;
	  continue;
	}
      if (MyIsDigit(*s1))
	{
	  hilo = -1;		/* s1 must come first */
	  break;
	}
      if (MyIsDigit(*s2))
	{
	  hilo = 1;		/* s2 must come first */
	  break;
	}
      hilo = (*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0;
      if (hilo)
	break;
      ++s1;
      ++s2;
    }
  if (*s1 && *s2)
    return (hilo);
  if (hilo)
    return (hilo);
  return ((*s1 < *s2) ? -1 : (*s1 > *s2) ? 1 : 0);
}

#ifdef TEST

#include <stdlib.h>

static int  qcmp(   const void      *fi1,
                    const void      *fi2)
{
    return colcomp(*(char**)fi1, *(char**)fi2);
}

int main( int argc, char *argv[], char *environ[]) {
  void *base;
  size_t nmemb = 0;
  size_t size = sizeof(char *);
  char *ca[] = {
    "999", "0", "10", "1", "01", "100", "010", "99", "00", "001", "099", "9"
  };
  char **cp;
  int i;

  if (argc > 1) {
    /* Sort use-provided list */
  } else {
    base = (void *) ca;
    nmemb = sizeof ca / size;
  }
  printf("argc is <%d>, nmemb = <%d>\n", argc, nmemb);

  printf("Before:\n");
  cp = (char **)base;
  for (i = 0; i < nmemb; ++i) {
    printf("%s\n", *cp++);
  }

  qsort((void *)base, nmemb, size, qcmp);

  printf("After:\n");
  cp = (char **)base;
  for (i = 0; i < nmemb; ++i) {
    printf("%s\n", *cp++);
  }

  exit(0);
}

#endif
OpenPOWER on IntegriCloud