summaryrefslogtreecommitdiff
path: root/compat.c
blob: 1530bd515ee4885b34206d153de9376319dc4d0e (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>

int
xstrcasecmp(const char *_l, const char *_r) {
	const unsigned char *l = (void *)_l, *r = (void *)_r;
	for(; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++);
	return tolower(*l) - tolower(*r);
}

int
xstrncasecmp(const char *_l, const char *_r, size_t n) {
	const unsigned char *l=(void *)_l, *r=(void *)_r;
	if(!n--)
		return 0;
	for(; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--);
	return tolower(*l) - tolower(*r);
}

void *
xstrdup(const char *s) {
	size_t len = strlen(s) + 1;
	void *p = malloc(len);
	if(p)
		memcpy(p, s, len);
	return p;
}

int
xmkdir(const char *path, mode_t mode) {
/* TODO: fix for mingw */
#if MINGW
	return mkdir(path);
#else
	return mkdir(path, mode);
#endif
}