summaryrefslogtreecommitdiff
path: root/lib/mlibc/options/posix/generic/posix_string.cpp
blob: d0bc7b5eb25aa0aac91021319d62975d95ca9f89 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <bits/ensure.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>

#include <mlibc/debug.hpp>

char *strdup(const char *string) {
	auto num_bytes = strlen(string);

	char *new_string = (char *)malloc(num_bytes + 1);
	if(!new_string) // TODO: set errno
		return nullptr;

	memcpy(new_string, string, num_bytes);
	new_string[num_bytes] = 0;
	return new_string;
}

char *strndup(const char *string, size_t max_size) {
	auto num_bytes = strnlen(string, max_size);
	char *new_string = (char *)malloc(num_bytes + 1);
	if(!new_string) // TODO: set errno
		return nullptr;

	memcpy(new_string, string, num_bytes);
	new_string[num_bytes] = 0;
	return new_string;
}

char *stpcpy(char *__restrict dest, const char *__restrict src) {
	auto n = strlen(src);
	memcpy(dest, src, n + 1);
	return dest + n;
}

char *stpncpy(char *__restrict dest, const char *__restrict src, size_t n) {
	size_t nulls, copied, srcLen = strlen(src);
	if (n >= srcLen) {
		nulls = n - srcLen;
		copied = srcLen;
	} else {
		nulls = 0;
		copied = n;
	}

	memcpy(dest, src, copied);
	memset(dest + srcLen, 0, nulls);
	return dest + n - nulls;
}

size_t strnlen(const char *s, size_t n) {
	size_t len = 0;
	while(len < n && s[len])
		++len;
	return len;
}

char *strsep(char **m, const char *del) {
	__ensure(m);

	auto tok = *m;
	if(!tok)
		return nullptr;

	// Replace the following delimiter by a null-terminator.
	// After this loop: *p is null iff we reached the end of the string.
	auto p = tok;
	while(*p && !strchr(del, *p))
		p++;

	if(*p) {
		*p = 0;
		*m = p + 1;
	}else{
		*m = nullptr;
	}
	return tok;
}

char *strsignal(int sig) {
	#define CASE_FOR(sigconst) case sigconst: s = #sigconst; break;
	const char *s;
	switch(sig) {
	CASE_FOR(SIGABRT)
	CASE_FOR(SIGFPE)
	CASE_FOR(SIGILL)
	CASE_FOR(SIGINT)
	CASE_FOR(SIGSEGV)
	CASE_FOR(SIGTERM)
	CASE_FOR(SIGPROF)
	CASE_FOR(SIGIO)
	CASE_FOR(SIGPWR)
	CASE_FOR(SIGALRM)
	CASE_FOR(SIGBUS)
	CASE_FOR(SIGCHLD)
	CASE_FOR(SIGCONT)
	CASE_FOR(SIGHUP)
	CASE_FOR(SIGKILL)
	CASE_FOR(SIGPIPE)
	CASE_FOR(SIGQUIT)
	CASE_FOR(SIGSTOP)
	CASE_FOR(SIGTSTP)
	CASE_FOR(SIGTTIN)
	CASE_FOR(SIGTTOU)
	CASE_FOR(SIGUSR1)
	CASE_FOR(SIGUSR2)
	CASE_FOR(SIGSYS)
	CASE_FOR(SIGTRAP)
	CASE_FOR(SIGURG)
	CASE_FOR(SIGVTALRM)
	CASE_FOR(SIGXCPU)
	CASE_FOR(SIGXFSZ)
	CASE_FOR(SIGWINCH)
	default:
		mlibc::infoLogger() << "mlibc: Unknown signal number " << sig << frg::endlog;
		s = "Unknown signal number";
	}
	return const_cast<char *>(s);
}

char *strcasestr(const char *s, const char *pattern) {
	size_t plen = strlen(pattern);
	const char *p = s;
	while(*p) {
		// Need strncasecmp() to avoid checking past the end of a successful match.
		if(!strncasecmp(p, pattern, plen))
			return const_cast<char *>(p);
		++p;
	}
	return nullptr;
}

void *memccpy(void *__restrict, const void *__restrict, int, size_t) {
	__ensure(!"Not implemented");
	__builtin_unreachable();
}

// This implementation was taken from musl
void *memrchr(const void *m, int c, size_t n) {
	const unsigned char *s = (const unsigned char *)m;
	c = (unsigned char)c;
	while(n--) {
		if(s[n] == c)
			return (void *)(s + n);
	}
	return 0;
}

// BSD extensions.
// Taken from musl
size_t strlcpy(char *d, const char *s, size_t n) {
	char *d0 = d;

	if(!n--)
		goto finish;
	for(; n && (*d=*s); n--, s++, d++);
	*d = 0;
finish:
	return d-d0 + strlen(s);
}

size_t strlcat(char *d, const char *s, size_t n) {
	size_t l = strnlen(d, n);
	if(l == n) {
		return l + strlen(s);
	}
	return l + strlcpy(d + l, s, n - l);
}