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