1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Matthew Luckie |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * This code has been developed by the University of Waikato WAND |
---|
12 | * research group. For further information please see http://www.wand.net.nz/ |
---|
13 | * |
---|
14 | * libtrace is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License as published by |
---|
16 | * the Free Software Foundation; either version 2 of the License, or |
---|
17 | * (at your option) any later version. |
---|
18 | * |
---|
19 | * libtrace is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | * GNU General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU General Public License |
---|
25 | * along with libtrace; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | * |
---|
28 | * $Id$ |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | #include "config.h" |
---|
33 | |
---|
34 | #ifndef HAVE_STRNDUP |
---|
35 | |
---|
36 | #include <stdlib.h> |
---|
37 | #include <errno.h> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | #include <libtrace_int.h> |
---|
41 | |
---|
42 | /* Some systems don't include strndup as part of their standard C library, so |
---|
43 | * we need to provide our own version. |
---|
44 | * |
---|
45 | * Full credit to Matthew Luckie, who wrote this particular version and allowed |
---|
46 | * us to borrow it. |
---|
47 | */ |
---|
48 | |
---|
49 | char *strndup(const char *s, size_t size) |
---|
50 | { |
---|
51 | char *str; |
---|
52 | size_t len; |
---|
53 | |
---|
54 | if(size == 0 || s == NULL) |
---|
55 | { |
---|
56 | errno = EINVAL; |
---|
57 | return NULL; |
---|
58 | } |
---|
59 | |
---|
60 | if(size > (len = strlen(s))) |
---|
61 | { |
---|
62 | size = len+1; |
---|
63 | } |
---|
64 | |
---|
65 | if((str = malloc(size)) == NULL) |
---|
66 | { |
---|
67 | errno = ENOMEM; |
---|
68 | return NULL; |
---|
69 | } |
---|
70 | |
---|
71 | memcpy(str, s, size); |
---|
72 | str[size-1] = '\0'; |
---|
73 | |
---|
74 | return str; |
---|
75 | } |
---|
76 | |
---|
77 | #endif |
---|