Rev | Line | |
---|
[6393] | 1 | /* strndup.c |
---|
| 2 | * |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | /* Written by Niels Möller <nisse@lysator.liu.se> |
---|
| 6 | * |
---|
| 7 | * This file is hereby placed in the public domain. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <string.h> |
---|
| 12 | |
---|
| 13 | char * |
---|
| 14 | strndup (const char *s, size_t size) |
---|
| 15 | { |
---|
| 16 | char *r; |
---|
| 17 | char *end = memchr(s, 0, size); |
---|
| 18 | |
---|
| 19 | if (end) |
---|
| 20 | /* Length + 1 */ |
---|
| 21 | size = end - s + 1; |
---|
| 22 | |
---|
| 23 | r = malloc(size); |
---|
| 24 | |
---|
| 25 | if (size) |
---|
| 26 | { |
---|
| 27 | memcpy(r, s, size-1); |
---|
| 28 | r[size-1] = '\0'; |
---|
| 29 | } |
---|
| 30 | return r; |
---|
| 31 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.