forked from seehuhn/moon-buggy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxstrdup.c
43 lines (34 loc) · 743 Bytes
/
xstrdup.c
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
/* xstrdup.c - fail save strdup
*
* Copyright 1998 Jochen Voss. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "moon-buggy.h"
char *
xstrdup (const char *str)
/* Duplicate STR as `strdup' does, but never return NULL. */
{
char *tmp = xmalloc (strlen(str) + 1);
strcpy (tmp, str);
return tmp;
}
size_t
xstrnlen (const char *str, size_t size)
{
size_t n = 0;
while (n<size && str[n]) ++n;
return n;
}
char *
xstrndup (const char *str, size_t size)
/* Like `strndup' from glibc, but never returns NULL. */
{
size_t n = xstrnlen (str, size);
char *tmp = xmalloc (n + 1);
if (tmp == NULL) fatal ("Memory exhausted");
strncpy (tmp, str, n);
tmp[n] = '\0';
return tmp;
}