File: misc.c

package info (click to toggle)
imapfilter 1%3A1.2.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 300 kB
  • ctags: 315
  • sloc: ansic: 3,392; sh: 182; makefile: 103
file content (74 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (10)
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "imapfilter.h"


/*
 * An implementation of strstr() with case-insensitivity.
 */
const char *
xstrcasestr(const char *haystack, const char *needle)
{
	const char *h, *n, *c;
	size_t hl, nl;

	c = haystack;
	n = needle;
	hl = strlen(haystack);
	nl = strlen(needle);

	while (hl >= nl) {
		while (tolower((int)(*c)) != tolower((int)(*needle))) {
			c++;
			hl--;
			if (hl < nl)
				return NULL;
		}

		h = c;
		n = needle;

		while (tolower((int)(*h)) == tolower((int)(*n))) {
			h++;
			n++;

			if (*n == '\0')
				return c;
		}
		c++;
		hl--;
	}

	return NULL;
}


/*
 * Copies at most size characters from the string pointed by src to the array
 * pointed by dest, always NULL terminating (unless size == 0).  Returns
 * pointer to dest.
 */
char *
xstrncpy(char *dst, const char *src, size_t len)
{
	char *d;
	const char *s;
	size_t n;

	d = dst;
	s = src;
	n = len;

	while (n != 0) {
		if ((*d++ = *s++) == '\0')
			break;
		n--;
	}

	if (n == 0 && len != 0)
		*d = '\0';

	return dst;
}