source: squid-ssl/trunk/fuentes/libltdl/lt__strl.c @ 5495

Last change on this file since 5495 was 5495, checked in by Juanma, 2 years ago

Initial release

File size: 3.5 KB
Line 
1/* lt__strl.c -- size-bounded string copying and concatenation
2
3   Copyright (C) 2004, 2011-2015 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
9GNU Libltdl is free software; you can redistribute it and/or
10modify it under the terms of the GNU Lesser General Public
11License as published by the Free Software Foundation; either
12version 2 of the License, or (at your option) any later version.
13
14As a special exception to the GNU Lesser General Public License,
15if you distribute this file as part of a program or library that
16is built using GNU Libtool, you may include this file under the
17same distribution terms that you use for the rest of that program.
18
19GNU Libltdl is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22GNU Lesser General Public License for more details.
23
24You should have received a copy of the GNU Lesser General Public
25License along with GNU Libltdl; see the file COPYING.LIB.  If not, a
26copy can be downloaded from  http://www.gnu.org/licenses/lgpl.html,
27or obtained by writing to the Free Software Foundation, Inc.,
2851 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 that
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
50size_t
51lt_strlcat(char *dst, const char *src, const size_t dstsize)
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 that 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
98size_t
99lt_strlcpy(char *dst, const char *src, const size_t dstsize)
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 */
Note: See TracBrowser for help on using the repository browser.