Import sendmail 8.13.4 into a new contrib directory as the first step
[dragonfly.git] / contrib / sendmail-8.13.4 / libsm / strdup.c
blob7094275e046cf83c49c9a629ece97e93afa54b45
1 /*
2 * Copyright (c) 2000-2001, 2003 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
9 */
11 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: strdup.c,v 1.15 2003/10/10 17:56:57 ca Exp $")
14 #include <sm/heap.h>
15 #include <sm/string.h>
18 ** SM_STRNDUP_X -- Duplicate a string of a given length
20 ** Allocates memory and copies source string (of given length) into it.
22 ** Parameters:
23 ** s -- string to copy.
24 ** n -- length to copy.
26 ** Returns:
27 ** copy of string, raises exception if out of memory.
29 ** Side Effects:
30 ** allocate memory for new string.
33 char *
34 sm_strndup_x(s, n)
35 const char *s;
36 size_t n;
38 char *d = sm_malloc_x(n + 1);
40 (void) memcpy(d, s, n);
41 d[n] = '\0';
42 return d;
46 ** SM_STRDUP -- Duplicate a string
48 ** Allocates memory and copies source string into it.
50 ** Parameters:
51 ** s -- string to copy.
53 ** Returns:
54 ** copy of string, NULL if out of memory.
56 ** Side Effects:
57 ** allocate memory for new string.
60 char *
61 sm_strdup(s)
62 char *s;
64 size_t l;
65 char *d;
67 l = strlen(s) + 1;
68 d = sm_malloc_tagged(l, "sm_strdup", 0, sm_heap_group());
69 if (d != NULL)
70 (void) sm_strlcpy(d, s, l);
71 return d;
74 #if DO_NOT_USE_STRCPY
77 ** SM_STRDUP_X -- Duplicate a string
79 ** Allocates memory and copies source string into it.
81 ** Parameters:
82 ** s -- string to copy.
84 ** Returns:
85 ** copy of string, exception if out of memory.
87 ** Side Effects:
88 ** allocate memory for new string.
91 char *
92 sm_strdup_x(s)
93 const char *s;
95 size_t l;
96 char *d;
98 l = strlen(s) + 1;
99 d = sm_malloc_tagged_x(l, "sm_strdup_x", 0, sm_heap_group());
100 (void) sm_strlcpy(d, s, l);
101 return d;
105 ** SM_PSTRDUP_X -- Duplicate a string (using "permanent" memory)
107 ** Allocates memory and copies source string into it.
109 ** Parameters:
110 ** s -- string to copy.
112 ** Returns:
113 ** copy of string, exception if out of memory.
115 ** Side Effects:
116 ** allocate memory for new string.
119 char *
120 sm_pstrdup_x(s)
121 const char *s;
123 size_t l;
124 char *d;
126 l = strlen(s) + 1;
127 d = sm_pmalloc_x(l);
128 (void) sm_strlcpy(d, s, l);
129 return d;
133 ** SM_STRDUP_X -- Duplicate a string
135 ** Allocates memory and copies source string into it.
137 ** Parameters:
138 ** s -- string to copy.
139 ** file -- name of source file
140 ** line -- line in source file
141 ** group -- heap group
143 ** Returns:
144 ** copy of string, exception if out of memory.
146 ** Side Effects:
147 ** allocate memory for new string.
150 char *
151 sm_strdup_tagged_x(s, file, line, group)
152 const char *s;
153 char *file;
154 int line, group;
156 size_t l;
157 char *d;
159 l = strlen(s) + 1;
160 d = sm_malloc_tagged_x(l, file, line, group);
161 (void) sm_strlcpy(d, s, l);
162 return d;
165 #endif /* DO_NOT_USE_STRCPY */