8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / strdup.c
blob13425ddc70eebcc7ff1d0e1b45ab313ab06706bb
1 /*
2 * Copyright (c) 2000-2001, 2003 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
9 */
11 #pragma ident "%Z%%M% %I% %E% SMI"
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: strdup.c,v 1.15 2003/10/10 17:56:57 ca Exp $")
16 #include <sm/heap.h>
17 #include <sm/string.h>
20 ** SM_STRNDUP_X -- Duplicate a string of a given length
22 ** Allocates memory and copies source string (of given length) into it.
24 ** Parameters:
25 ** s -- string to copy.
26 ** n -- length to copy.
28 ** Returns:
29 ** copy of string, raises exception if out of memory.
31 ** Side Effects:
32 ** allocate memory for new string.
35 char *
36 sm_strndup_x(s, n)
37 const char *s;
38 size_t n;
40 char *d = sm_malloc_x(n + 1);
42 (void) memcpy(d, s, n);
43 d[n] = '\0';
44 return d;
48 ** SM_STRDUP -- Duplicate a string
50 ** Allocates memory and copies source string into it.
52 ** Parameters:
53 ** s -- string to copy.
55 ** Returns:
56 ** copy of string, NULL if out of memory.
58 ** Side Effects:
59 ** allocate memory for new string.
62 char *
63 sm_strdup(s)
64 char *s;
66 size_t l;
67 char *d;
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;
76 #if DO_NOT_USE_STRCPY
79 ** SM_STRDUP_X -- Duplicate a string
81 ** Allocates memory and copies source string into it.
83 ** Parameters:
84 ** s -- string to copy.
86 ** Returns:
87 ** copy of string, exception if out of memory.
89 ** Side Effects:
90 ** allocate memory for new string.
93 char *
94 sm_strdup_x(s)
95 const char *s;
97 size_t l;
98 char *d;
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;
107 ** SM_PSTRDUP_X -- Duplicate a string (using "permanent" memory)
109 ** Allocates memory and copies source string into it.
111 ** Parameters:
112 ** s -- string to copy.
114 ** Returns:
115 ** copy of string, exception if out of memory.
117 ** Side Effects:
118 ** allocate memory for new string.
121 char *
122 sm_pstrdup_x(s)
123 const char *s;
125 size_t l;
126 char *d;
128 l = strlen(s) + 1;
129 d = sm_pmalloc_x(l);
130 (void) sm_strlcpy(d, s, l);
131 return d;
135 ** SM_STRDUP_X -- Duplicate a string
137 ** Allocates memory and copies source string into it.
139 ** Parameters:
140 ** s -- string to copy.
141 ** file -- name of source file
142 ** line -- line in source file
143 ** group -- heap group
145 ** Returns:
146 ** copy of string, exception if out of memory.
148 ** Side Effects:
149 ** allocate memory for new string.
152 char *
153 sm_strdup_tagged_x(s, file, line, group)
154 const char *s;
155 char *file;
156 int line, group;
158 size_t l;
159 char *d;
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;
167 #endif /* DO_NOT_USE_STRCPY */