summaryrefslogtreecommitdiff
path: root/src/libc/string/string.c
blob: e93b0a6526451390bb17eefb46391973c69eeb26 (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
/** String functions that don't fit into any of the other .c files */

#include <bits/panic.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

char *strncat(char *restrict dst, const char *restrict src, size_t n) {
	(void)dst; (void)src; (void)n;
	__libc_panic("unimplemented");
}

char *stpncpy(char *restrict dst, const char *restrict src, size_t n) {
	(void)dst; (void)src; (void)n;
	__libc_panic("unimplemented");
}

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

char *strsignal(int sig) {
	static char buf[32];
	snprintf(buf, sizeof(buf), "signal %d", sig);
	return buf;
}

/* strings.h */
int strcasecmp(const char *s1, const char *s2) {
	return strncasecmp(s1, s2, ~0);
}

int strncasecmp(const char *s1, const char *s2, size_t n) {
	for (size_t i = 0; i < n; i++) {
		char c1 = tolower(s1[i]), c2 = tolower(s2[i]);
		if (c1 == '\0' || c1 != c2) {
			if (c1 < c2)      return -1;
			else if (c1 > c2) return 1;
			else              return 0;
		}
	}
	return 0;
}