1N/A/*
1N/A * Copyright (c) 2000-2001, 2003 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: strdup.c,v 1.15 2003/10/10 17:56:57 ca Exp $")
1N/A
1N/A#include <sm/heap.h>
1N/A#include <sm/string.h>
1N/A
1N/A/*
1N/A** SM_STRNDUP_X -- Duplicate a string of a given length
1N/A**
1N/A** Allocates memory and copies source string (of given length) into it.
1N/A**
1N/A** Parameters:
1N/A** s -- string to copy.
1N/A** n -- length to copy.
1N/A**
1N/A** Returns:
1N/A** copy of string, raises exception if out of memory.
1N/A**
1N/A** Side Effects:
1N/A** allocate memory for new string.
1N/A*/
1N/A
1N/Achar *
1N/Asm_strndup_x(s, n)
1N/A const char *s;
1N/A size_t n;
1N/A{
1N/A char *d = sm_malloc_x(n + 1);
1N/A
1N/A (void) memcpy(d, s, n);
1N/A d[n] = '\0';
1N/A return d;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRDUP -- Duplicate a string
1N/A**
1N/A** Allocates memory and copies source string into it.
1N/A**
1N/A** Parameters:
1N/A** s -- string to copy.
1N/A**
1N/A** Returns:
1N/A** copy of string, NULL if out of memory.
1N/A**
1N/A** Side Effects:
1N/A** allocate memory for new string.
1N/A*/
1N/A
1N/Achar *
1N/Asm_strdup(s)
1N/A char *s;
1N/A{
1N/A size_t l;
1N/A char *d;
1N/A
1N/A l = strlen(s) + 1;
1N/A d = sm_malloc_tagged(l, "sm_strdup", 0, sm_heap_group());
1N/A if (d != NULL)
1N/A (void) sm_strlcpy(d, s, l);
1N/A return d;
1N/A}
1N/A
1N/A#if DO_NOT_USE_STRCPY
1N/A
1N/A/*
1N/A** SM_STRDUP_X -- Duplicate a string
1N/A**
1N/A** Allocates memory and copies source string into it.
1N/A**
1N/A** Parameters:
1N/A** s -- string to copy.
1N/A**
1N/A** Returns:
1N/A** copy of string, exception if out of memory.
1N/A**
1N/A** Side Effects:
1N/A** allocate memory for new string.
1N/A*/
1N/A
1N/Achar *
1N/Asm_strdup_x(s)
1N/A const char *s;
1N/A{
1N/A size_t l;
1N/A char *d;
1N/A
1N/A l = strlen(s) + 1;
1N/A d = sm_malloc_tagged_x(l, "sm_strdup_x", 0, sm_heap_group());
1N/A (void) sm_strlcpy(d, s, l);
1N/A return d;
1N/A}
1N/A
1N/A/*
1N/A** SM_PSTRDUP_X -- Duplicate a string (using "permanent" memory)
1N/A**
1N/A** Allocates memory and copies source string into it.
1N/A**
1N/A** Parameters:
1N/A** s -- string to copy.
1N/A**
1N/A** Returns:
1N/A** copy of string, exception if out of memory.
1N/A**
1N/A** Side Effects:
1N/A** allocate memory for new string.
1N/A*/
1N/A
1N/Achar *
1N/Asm_pstrdup_x(s)
1N/A const char *s;
1N/A{
1N/A size_t l;
1N/A char *d;
1N/A
1N/A l = strlen(s) + 1;
1N/A d = sm_pmalloc_x(l);
1N/A (void) sm_strlcpy(d, s, l);
1N/A return d;
1N/A}
1N/A
1N/A/*
1N/A** SM_STRDUP_X -- Duplicate a string
1N/A**
1N/A** Allocates memory and copies source string into it.
1N/A**
1N/A** Parameters:
1N/A** s -- string to copy.
1N/A** file -- name of source file
1N/A** line -- line in source file
1N/A** group -- heap group
1N/A**
1N/A** Returns:
1N/A** copy of string, exception if out of memory.
1N/A**
1N/A** Side Effects:
1N/A** allocate memory for new string.
1N/A*/
1N/A
1N/Achar *
1N/Asm_strdup_tagged_x(s, file, line, group)
1N/A const char *s;
1N/A char *file;
1N/A int line, group;
1N/A{
1N/A size_t l;
1N/A char *d;
1N/A
1N/A l = strlen(s) + 1;
1N/A d = sm_malloc_tagged_x(l, file, line, group);
1N/A (void) sm_strlcpy(d, s, l);
1N/A return d;
1N/A}
1N/A
1N/A#endif /* DO_NOT_USE_STRCPY */
1N/A