xref: /titanic_41/usr/src/cmd/sendmail/libsm/strdup.c (revision da14cebe459d3275048785f25bd869cb09b5307f)
1 /*
2  * Copyright (c) 2000-2001, 2003 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
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.
8  *
9  */
10 
11 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: strdup.c,v 1.15 2003/10/10 17:56:57 ca Exp $")
15 
16 #include <sm/heap.h>
17 #include <sm/string.h>
18 
19 /*
20 **  SM_STRNDUP_X -- Duplicate a string of a given length
21 **
22 **	Allocates memory and copies source string (of given length) into it.
23 **
24 **	Parameters:
25 **		s -- string to copy.
26 **		n -- length to copy.
27 **
28 **	Returns:
29 **		copy of string, raises exception if out of memory.
30 **
31 **	Side Effects:
32 **		allocate memory for new string.
33 */
34 
35 char *
36 sm_strndup_x(s, n)
37 	const char *s;
38 	size_t n;
39 {
40 	char *d = sm_malloc_x(n + 1);
41 
42 	(void) memcpy(d, s, n);
43 	d[n] = '\0';
44 	return d;
45 }
46 
47 /*
48 **  SM_STRDUP -- Duplicate a string
49 **
50 **	Allocates memory and copies source string into it.
51 **
52 **	Parameters:
53 **		s -- string to copy.
54 **
55 **	Returns:
56 **		copy of string, NULL if out of memory.
57 **
58 **	Side Effects:
59 **		allocate memory for new string.
60 */
61 
62 char *
63 sm_strdup(s)
64 	char *s;
65 {
66 	size_t l;
67 	char *d;
68 
69 	l = strlen(s) + 1;
70 	d = sm_malloc_tagged(l, "sm_strdup", 0, sm_heap_group());
71 	if (d != NULL)
72 		(void) sm_strlcpy(d, s, l);
73 	return d;
74 }
75 
76 #if DO_NOT_USE_STRCPY
77 
78 /*
79 **  SM_STRDUP_X -- Duplicate a string
80 **
81 **	Allocates memory and copies source string into it.
82 **
83 **	Parameters:
84 **		s -- string to copy.
85 **
86 **	Returns:
87 **		copy of string, exception if out of memory.
88 **
89 **	Side Effects:
90 **		allocate memory for new string.
91 */
92 
93 char *
94 sm_strdup_x(s)
95 	const char *s;
96 {
97 	size_t l;
98 	char *d;
99 
100 	l = strlen(s) + 1;
101 	d = sm_malloc_tagged_x(l, "sm_strdup_x", 0, sm_heap_group());
102 	(void) sm_strlcpy(d, s, l);
103 	return d;
104 }
105 
106 /*
107 **  SM_PSTRDUP_X -- Duplicate a string (using "permanent" memory)
108 **
109 **	Allocates memory and copies source string into it.
110 **
111 **	Parameters:
112 **		s -- string to copy.
113 **
114 **	Returns:
115 **		copy of string, exception if out of memory.
116 **
117 **	Side Effects:
118 **		allocate memory for new string.
119 */
120 
121 char *
122 sm_pstrdup_x(s)
123 	const char *s;
124 {
125 	size_t l;
126 	char *d;
127 
128 	l = strlen(s) + 1;
129 	d = sm_pmalloc_x(l);
130 	(void) sm_strlcpy(d, s, l);
131 	return d;
132 }
133 
134 /*
135 **  SM_STRDUP_X -- Duplicate a string
136 **
137 **	Allocates memory and copies source string into it.
138 **
139 **	Parameters:
140 **		s -- string to copy.
141 **		file -- name of source file
142 **		line -- line in source file
143 **		group -- heap group
144 **
145 **	Returns:
146 **		copy of string, exception if out of memory.
147 **
148 **	Side Effects:
149 **		allocate memory for new string.
150 */
151 
152 char *
153 sm_strdup_tagged_x(s, file, line, group)
154 	const char *s;
155 	char *file;
156 	int line, group;
157 {
158 	size_t l;
159 	char *d;
160 
161 	l = strlen(s) + 1;
162 	d = sm_malloc_tagged_x(l, file, line, group);
163 	(void) sm_strlcpy(d, s, l);
164 	return d;
165 }
166 
167 #endif /* DO_NOT_USE_STRCPY */
168 
169