root/libltdl/lt__strl.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lt_strlcat
  2. lt_strlcpy

   1 /* lt__strl.c -- size-bounded string copying and concatenation
   2 
   3    Copyright (C) 2004 Free Software Foundation, Inc.
   4    Written by Bob Friesenhahn, 2004
   5 
   6    NOTE: The canonical source of this file is maintained with the
   7    GNU Libtool package.  Report bugs to bug-libtool@gnu.org.
   8 
   9 GNU Libltdl is free software; you can redistribute it and/or
  10 modify it under the terms of the GNU Lesser General Public
  11 License as published by the Free Software Foundation; either
  12 version 2 of the License, or (at your option) any later version.
  13 
  14 As a special exception to the GNU Lesser General Public License,
  15 if you distribute this file as part of a program or library that
  16 is built using GNU Libtool, you may include this file under the
  17 same distribution terms that you use for the rest of that program.
  18 
  19 GNU Libltdl is distributed in the hope that it will be useful,
  20 but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 GNU Lesser General Public License for more details.
  23 
  24 You should have received a copy of the GNU Lesser General Public
  25 License along with GNU Libltdl; see the file COPYING.LIB.  If not, a
  26 copy can be downloaded from  http://www.gnu.org/licenses/lgpl.html,
  27 or obtained by writing to the Free Software Foundation, Inc.,
  28 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  29 */
  30 
  31 #include <assert.h>
  32 #include <string.h>
  33 
  34 #include "lt__strl.h"
  35 
  36 /*
  37  lt_strlcat appends the NULL-terminated string src to the end of dst.
  38  It will append at most dstsize - strlen(dst) - 1 bytes,
  39  NULL-terminating the result. The total length of the string which
  40  would have been created given sufficient buffer size (may be longer
  41  than dstsize) is returned.  This function substitutes for strlcat()
  42  which is available under NetBSD, FreeBSD and Solaris 9.
  43 
  44  Buffer overflow can be checked as follows:
  45 
  46    if (lt_strlcat(dst, src, dstsize) >= dstsize)
  47      return -1;
  48 */
  49 #if !defined(HAVE_STRLCAT)
  50 size_t
  51 lt_strlcat(char *dst, const char *src, const size_t dstsize)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53   size_t length;
  54   char *p;
  55   const char *q;
  56 
  57   assert(dst != NULL);
  58   assert(src != (const char *) NULL);
  59   assert(dstsize >= 1);
  60 
  61   length=strlen(dst);
  62 
  63   /*
  64     Copy remaining characters from src while constraining length to
  65     size - 1.
  66   */
  67   for ( p = dst + length, q = src;
  68         (*q != 0) && (length < dstsize - 1) ;
  69         length++, p++, q++ )
  70     *p = *q;
  71 
  72   dst[length]='\0';
  73 
  74   /*
  75     Add remaining length of src to length.
  76   */
  77   while (*q++)
  78     length++;
  79 
  80   return length;
  81 }
  82 #endif /* !defined(HAVE_STRLCAT) */
  83 
  84 /*
  85   lt_strlcpy copies up to dstsize - 1 characters from the NULL-terminated
  86   string src to dst, NULL-terminating the result. The total length of
  87   the string which would have been created given sufficient buffer
  88   size (may be longer than dstsize) is returned. This function
  89   substitutes for strlcpy() which is available under OpenBSD, FreeBSD
  90   and Solaris 9.
  91 
  92   Buffer overflow can be checked as  follows:
  93 
  94     if (lt_strlcpy(dst, src, dstsize) >= dstsize)
  95       return -1;
  96 */
  97 #if !defined(HAVE_STRLCPY)
  98 size_t
  99 lt_strlcpy(char *dst, const char *src, const size_t dstsize)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101   size_t length=0;
 102   char *p;
 103   const char *q;
 104 
 105   assert(dst != NULL);
 106   assert(src != (const char *) NULL);
 107   assert(dstsize >= 1);
 108 
 109   /*
 110     Copy src to dst within bounds of size-1.
 111   */
 112   for ( p=dst, q=src, length=0 ;
 113         (*q != 0) && (length < dstsize-1) ;
 114         length++, p++, q++ )
 115     *p = *q;
 116 
 117   dst[length]='\0';
 118 
 119   /*
 120     Add remaining length of src to length.
 121   */
 122   while (*q++)
 123     length++;
 124 
 125   return length;
 126 }
 127 #endif /* !defined(HAVE_STRLCPY) */

/* [previous][next][first][last][top][bottom][index][help] */