Mercurial > code
annotate port/strdup.c @ 291:f4fc723429fe
TreeNode: improve performances by taking the value to the stack instead of a second unique_ptr
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 13 Nov 2014 13:21:53 +0100 |
parents | 2d6c466e56dc |
children |
rev | line source |
---|---|
121
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
122 | 2 * strdup.c -- duplicate a string |
121
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2011, 2012, David Demelier <markand@malikania.fr> |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #include <stdio.h> |
122 | 20 #include <stdlib.h> |
21 #include <string.h> | |
121
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 |
122 | 23 char * |
24 strdup(const char *src) | |
25 { | |
26 char *dst; | |
27 size_t len; | |
121
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
28 |
122 | 29 len = strlen(src); |
30 | |
31 if ((dst = calloc(len + 1, sizeof (char))) == NULL) | |
32 return NULL; | |
33 | |
34 return strcpy(dst, src); | |
121
0635f98a2227
Forgot to add port .h files
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
35 } |