/*
* Copyright (c) 2002, 2003, 2005, 2006 Sendmail, 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/generic.h"
SM_RCSID("@(#)$Id: cstrscpyn0.c,v 1.8 2006/10/05 04:27:37 ca Exp $")
#include "sm/assert.h"
#include "sm/magic.h"
#include "sm/error.h"
#include "sm/memops.h"
#include "sm/cstr.h"
#include "sm/limits.h"
/*
** SM_CSTR_SCPYN0 -- Create a cstring and copy n+1 bytes from src into it
** This is used to create a '\0' terminated string without counting
** the trailing '\0'.
**
** Parameters:
** src -- Byte array to copy from
** n -- Number of bytes - 1 to copy from src
**
** Returns:
** New cstr object
** NULL on error
*/
sm_cstr_P
sm_cstr_scpyn0(const uchar *src, uint n)
{
sm_cstr_P cstr;
#if MTA_USE_PTHREADS
int r;
#endif
SM_REQUIRE(src != NULL);
if (n >= CSTR_MAX_SIZE)
return NULL;
cstr = sm_malloc(sizeof(*cstr));
if (cstr == NULL)
return NULL;
cstr->sm_cstr_base = sm_malloc(n + 1);
if (cstr->sm_cstr_base == NULL)
{
sm_free_size(cstr, sizeof(*cstr));
return NULL;
}
#if MTA_USE_PTHREADS
r = pthread_mutex_init(&cstr->sm_cstr_mutex, SM_PTHREAD_MUTEXATTR);
if (r != 0)
{
sm_free_size(cstr->sm_cstr_base, n + 1);
sm_free_size(cstr, sizeof(*cstr));
return NULL;
}
#endif /* MTA_USE_PTHREADS */
cstr->sm_cstr_len = n;
cstr->sm_cstr_refcnt = 1;
sm_memcpy(cstr->sm_cstr_base, src, n + 1);
cstr->sm_magic = SM_CSTR_MAGIC;
return cstr;
}
syntax highlighted by Code2HTML, v. 0.9.1