File: strutil.c

package info (click to toggle)
nws 2.11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,700 kB
  • ctags: 2,820
  • sloc: ansic: 28,849; sh: 3,289; java: 1,205; makefile: 697; perl: 12
file content (150 lines) | stat: -rw-r--r-- 3,147 bytes parent folder | download
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
/* $Id: strutil.c,v 1.26 2004/11/04 21:25:17 graziano Exp $ */

#include <ctype.h>
#include <string.h>
#include <stdarg.h>

#include "strutil.h"


#define EOS '\0'


void
strcase(char *string,
        CaseTypes whatCase) {
	char *c;
	int raise;

	raise = (whatCase == ALL_UPPER) || (whatCase == INITIAL_UPPER);

	/* to* are undefined if *c isn't a character, so we check */
	for(c = string; *c != EOS && isalpha((int) *c); c++) {
		*c = raise ? toupper((int)*c) : tolower((int)*c);
		raise = (whatCase == ALL_UPPER) || (whatCase == INITIAL_LOWER);
	}
}


int
strnmatch(	const char *string,
		const char *pattern,
		size_t len) {
	const char *lastChance;
	const char *nextWild;
	int tameLen;

	if(*pattern == '*') {
		do pattern++; while (*pattern == '*');
		if(*pattern == '\0') {
			return 1; /* Trailing star matches everything. */
		}
		nextWild = strchr(pattern, '*');
		tameLen = (nextWild == NULL) ? strlen(pattern) : nextWild - pattern;
		/* The wildcard we're processing can match any string of
		 * characters up to an occurrence of the following
		 * non-wild subpattern.  For each subpattern occurrence,
		 * see if the remaining string matches the remaining
		 * pattern. */
		lastChance = string + len - tameLen;
		for(; string <= lastChance; string++) {
			if((strncmp(string, pattern, tameLen) == 0) && strnmatch(string + tameLen, pattern + tameLen, lastChance - string)) {
				return 1;
			}
		}
		return 0;
	} else {
		nextWild = strchr(pattern, '*');
		if(nextWild == NULL) {
			/* No wildcards in pattern; check for exact match. */
			return (strlen(pattern) == len) && (strncmp(string, pattern, len) == 0);
 		} else {
			/* Check for leading exact match followed by a
			 * wildcard match. */
			tameLen = nextWild - pattern;
			return (tameLen <= len) && (strncmp(string, pattern, tameLen) == 0) && strnmatch(string + tameLen, nextWild, len - tameLen);
		}
	}
}


#ifndef HAVE_STRNLEN
size_t
strnlen(	const char *s,
		size_t maxlen) {
	size_t i;

	if (maxlen <= 0 || s == NULL) {
		return 0;
	}

	for (i=0; i<maxlen; i++) {
		if (s[i] == '\0') {
			/* done */
			break;
		}
	}
	return i;
}
#endif

int
strntok(char *dest,
        const char *source,
        int len,
        const char *delim,
        const char **end) {

	char *last = dest + len - 1;

	/* sanity check */
	if (dest == NULL || source == NULL) {
		return 0;
	}

	/* Skip leading delimiters. */
	while( (*source != EOS) && (strchr(delim, *source) != NULL) ) {
		source++;
	}

	if(*source == EOS) {
		return 0;
	}

	while( (dest < last) && (*source != EOS) &&
			(strchr(delim, *source) == NULL) ) {
		*dest++ = *source++;
	}

	if(end != NULL) {
		*end = (const char *)source;
	}
	*dest = EOS;

	return 1;
}


int
vstrncpy(char *dest,
         size_t len,
         int count,
         ...) {

  va_list paramList;
  int i;
  const char *source;
  char *end = dest + len - 1;
  char *start = dest;

  va_start(paramList, count);
  for (i = 0; i < count; i++) {
    for (source = va_arg(paramList, const char*);
         (dest < end) && (*source != EOS); dest++, source++)
      *dest = *source;
  }
  *dest = EOS;
  va_end(paramList);
  return dest - start;

}