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