Re: dynamicly build a list of strings?

From: Jonathan Burd (jburd_at_nospam.com)
Date: 02/03/05


Date: Thu, 03 Feb 2005 14:44:41 +0530

Neil Kurzman wrote:
>

<snip>

>
> Not list[i][strlen[line])=line; use strncpy()
>

<snip>

I would suggest using strlcpy instead. The latest source code is
available from the OpenBSD ftp. strlcpy guarantees null termination
of the string and is more efficient since it does not pad
the string buffer with redundant NULs. strncpy can be a problem if
used improperly (you have to remember to pass the correct
count of characters and then manually terminate the string).
Also, strlcpy is more secure and more intuitive to use.
You simply pass the size of the buffer in bytes as the last
parameter.

/* xstrlcpy.c */
#include <stddef.h>

/*
   @synopsis
     #include <xstring.h>
     size_t xstrlcpy (char *dst, const char *src, size_t siz);
   @description
     Copies not more than siz-1 characters into the array
     pointed to by dst from the string pointed to by src
     (characters that follow a null character are not copied).
     If copying takes place between two objects that overlap,
     the behavior is undefined.

     This function does not add redundant NUL characters
     to fill in the size requirements. The array pointed
     to by dst is guaranteed to be terminated by a NUL
     character (unless siz is zero).
   @param dst pointer to the destination buffer array
   @param src pointer to the source string
   @param siz the size of the destination buffer array
                object including the space for a terminating
                NUL character.
   @return
     The length of the string pointed to by dst after
     copying (the number of characters before the terminating
     null character).

     If the returned value is greater than or equal to
     siz, truncation has occurred.
   @remarks
     This function should be used in place of the functions
     xstrcpy and xstrncpy whenever possible due to the following
     reasons:
     * reduces the risk of a buffer overflow
     * guarantees NUL-termination of the destination buffer
     * is more logical since you only have to remember to
       pass the size of the entire destination array object
     * efficiency is much better than xstrncpy while negligibly
       lesser than xstrcpy.

     The xstrncpy function is useful when you do not
     need to terminate the destination array with a NUL-character
     and want to copy exactly the number of characters specified.
   @references
     * ISO/IEC 9899:1999 (C Standard) -- 7.21.2.4 -- The strncpy function
     * "strlcpy and strlcat -- consistent, safe, string copy
       and concatenation" - Todd C. Miller and Theo de Raadt
   @source
     The source code for this function was obtained from the
     OpenBSD libc string routines library. It is available under a
     BSD-style license.
   @see
     xstrncpy
     xstrcpy
*/
/*
  * Copy src to string dst of size siz. At most siz-1 characters
  * will be copied. Always NUL terminates (unless siz == 0).
  * Returns strlen(src); if retval >= siz, truncation occurred.
  */
size_t
xstrlcpy(char *dst, const char *src, size_t siz)
{
   char *d = dst;
   const char *s = src;
   register size_t n = siz;

   /* Copy as many bytes as will fit */
   if (n != 0 && --n != 0) {
     do {
       if ((*d++ = *s++) == 0)
         break;
     } while (--n != 0);
   }

   /* Not enough room in dst, add NUL and traverse rest of src */
   if (n == 0) {
     if (siz != 0)
       *d = '\0'; /* NUL-terminate dst */
     while (*s++)
       ;
   }

   return(s - src - 1); /* count does not include NUL */
}

Regards,
Jonathan.

-- 
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP
"We must do something.  This is something.  Therefore, we must do this."
  - Keith Thompson


Relevant Pages

  • Re: Delimit char for value parm of <input> tag
    ... >embedded " as termination of the value string. ... might contain any characters, including " and '. ... way to delimit embedded chars, or a way to use something other than " ...
    (comp.lang.php)
  • Re: Null terminated strings: bad or good?
    ... Another abstraction is probably appropriate (a simple buffer ... of characters without null termination?) ... start of the string to the terminating nul. ...
    (comp.lang.c)
  • Re: Null terminated strings: bad or good?
    ... Another abstraction is probably appropriate (a simple buffer ... of characters without null termination?) ... You may not like the idea of running strlenevery time you want to know the length of a null-terminated string but, as it was already said, that isn't something you are forced to do, let alone need. ...
    (comp.lang.c)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)

Loading