summaryrefslogtreecommitdiff
path: root/misc/strdup.c
blob: 9236fe7db80d1ae95c6bef064bd981e228ad1a38 (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
/*
 * Author: Tim Mooney <mooney@plains.nodak.edu>
 * Copyright: This file is in the public domain.
 *
 * a replacement for strdup() for those platforms that don't have it,
 * like Ultrix.
 *
 * Requires: malloc(), strlen(), strcpy().
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#if defined(HAVE_STDLIB_H) || defined(STDC_HEADERS)
# include <stdlib.h>
#else
extern void *malloc(size_t);
#endif

#if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
# include <string.h>
#else
extern size_t strlen(const char *);
extern char *strcpy(char *, const char *);
#endif

char * strdup(const char *s) {
	void *p = NULL;

	p = malloc(strlen(s)+1);
	if (!p) {
		return NULL;
	}

	strcpy( (char *) p, s);
	return (char *) p;
}