BRL-CAD
strdup.c
Go to the documentation of this file.
1 /* S T R D U P . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libsysv/strdup.c
21  *
22  * Duplicate a string.
23  *
24  * Author -
25  * Michael John Muuss
26  *
27  */
28 
29 #include "common.h"
30 
31 /* quell warnings */
32 #if defined(HAVE_WUNUSED_CONST_VARIABLE)
33 # if HAVE_GCC_DIAG_PRAGMAS
34 # pragma GCC diagnostic push /* begin ignoring warnings */
35 # pragma GCC diagnostic ignored "-Wunused-const-variable"
36 # elif HAVE_CLANG_DIAG_PRAGMAS
37 # pragma clang diagnostic push /* begin ignoring warnings */
38 # pragma clang diagnostic ignored "-Wunused-const-variable"
39 # endif
40 #endif
41 
42 /* quell empty-compilation unit warnings */
43 static const int unused = 0;
44 
45 #if defined(HAVE_WUNUSED_CONST_VARIABLE)
46 # if HAVE_GCC_DIAG_PRAGMAS
47 # pragma GCC diagnostic pop /* end ignoring warnings */
48 # elif HAVE_CLANG_DIAG_PRAGMAS
49 # pragma clang diagnostic pop /* end ignoring warnings */
50 # endif
51 #endif
52 
53 #ifndef HAVE_STRDUP
54 #include "sysv.h"
55 
56 /* for malloc */
57 #include <stdlib.h>
58 #include <string.h>
59 
60 
61 /*
62  * Given a string, allocate enough memory to hold it using malloc(),
63  * duplicate the strings, returns a pointer to the new string.
64  */
65 char *
66 strdup(register const char *cp)
67 {
68  register char *base;
69  register int len;
70 
71  len = strlen( cp )+2;
72  if ( (base = (char *)malloc( len )) == (char *)0 )
73  return (char *)0;
74 
75  memcpy(base, cp, len);
76  return base;
77 }
78 
79 #endif
80 
81 /*
82  * Local Variables:
83  * mode: C
84  * tab-width: 8
85  * indent-tabs-mode: t
86  * c-file-style: "stroustrup"
87  * End:
88  * ex: shiftwidth=4 tabstop=8
89  */
Header file for the BRL-CAD common definitions.
char * strdup(register const char *cp)
Definition: strdup.c:66