source: lib/strndup.c @ ebf8071

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since ebf8071 was ebf8071, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Updated licensing and documentation for another set of source files
  • Property mode set to 100644
File size: 1.7 KB
Line 
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/* Some systems don't include strndup as part of their standard C library, so
41 * we need to provide our own version.
42 *
43 * Full credit to Matthew Luckie, who wrote this particular version and allowed
44 * us to borrow it.
45 */
46
47char *strndup(const char *s, size_t size)
48{
49  char   *str;
50  size_t  len;
51
52  if(size == 0 || s == NULL)
53    {
54      errno = EINVAL;
55      return NULL;
56    }
57
58  if(size > (len = strlen(s)))
59    {
60      size = len+1;
61    }
62
63  if((str = malloc(size)) == NULL)
64    {
65      errno = ENOMEM;
66      return NULL;
67    }
68
69  memcpy(str, s, size);
70  str[size-1] = '\0';
71
72  return str;
73}
74
75#endif
Note: See TracBrowser for help on using the repository browser.