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