summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/strdup.c
blob: 3ddf73b4629e10c4009aa2fc2288fa0467f73cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2000-2001, 2003 Proofpoint, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: strdup.c,v 1.16 2013-11-22 20:51:43 ca Exp $")

#include <sm/heap.h>
#include <sm/string.h>

/*
**  SM_STRNDUP_X -- Duplicate a string of a given length
**
**	Allocates memory and copies source string (of given length) into it.
**
**	Parameters:
**		s -- string to copy.
**		n -- length to copy.
**
**	Returns:
**		copy of string, raises exception if out of memory.
**
**	Side Effects:
**		allocate memory for new string.
*/

char *
sm_strndup_x(s, n)
	const char *s;
	size_t n;
{
	char *d = sm_malloc_x(n + 1);

	(void) memcpy(d, s, n);
	d[n] = '\0';
	return d;
}

/*
**  SM_STRDUP -- Duplicate a string
**
**	Allocates memory and copies source string into it.
**
**	Parameters:
**		s -- string to copy.
**
**	Returns:
**		copy of string, NULL if out of memory.
**
**	Side Effects:
**		allocate memory for new string.
*/

char *
sm_strdup(s)
	char *s;
{
	size_t l;
	char *d;

	l = strlen(s) + 1;
	d = sm_malloc_tagged(l, "sm_strdup", 0, sm_heap_group());
	if (d != NULL)
		(void) sm_strlcpy(d, s, l);
	return d;
}

#if DO_NOT_USE_STRCPY

/*
**  SM_STRDUP_X -- Duplicate a string
**
**	Allocates memory and copies source string into it.
**
**	Parameters:
**		s -- string to copy.
**
**	Returns:
**		copy of string, exception if out of memory.
**
**	Side Effects:
**		allocate memory for new string.
*/

char *
sm_strdup_x(s)
	const char *s;
{
	size_t l;
	char *d;

	l = strlen(s) + 1;
	d = sm_malloc_tagged_x(l, "sm_strdup_x", 0, sm_heap_group());
	(void) sm_strlcpy(d, s, l);
	return d;
}

/*
**  SM_PSTRDUP_X -- Duplicate a string (using "permanent" memory)
**
**	Allocates memory and copies source string into it.
**
**	Parameters:
**		s -- string to copy.
**
**	Returns:
**		copy of string, exception if out of memory.
**
**	Side Effects:
**		allocate memory for new string.
*/

char *
sm_pstrdup_x(s)
	const char *s;
{
	size_t l;
	char *d;

	l = strlen(s) + 1;
	d = sm_pmalloc_x(l);
	(void) sm_strlcpy(d, s, l);
	return d;
}

/*
**  SM_STRDUP_X -- Duplicate a string
**
**	Allocates memory and copies source string into it.
**
**	Parameters:
**		s -- string to copy.
**		file -- name of source file
**		line -- line in source file
**		group -- heap group
**
**	Returns:
**		copy of string, exception if out of memory.
**
**	Side Effects:
**		allocate memory for new string.
*/

char *
sm_strdup_tagged_x(s, file, line, group)
	const char *s;
	char *file;
	int line, group;
{
	size_t l;
	char *d;

	l = strlen(s) + 1;
	d = sm_malloc_tagged_x(l, file, line, group);
	(void) sm_strlcpy(d, s, l);
	return d;
}

#endif /* DO_NOT_USE_STRCPY */

OpenPOWER on IntegriCloud