xref: /freebsd/contrib/sendmail/libsm/strdup.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
1 /*
2  * Copyright (c) 2000-2001 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.13 2001/09/11 04:04:49 gshapiro 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 *
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