/[svn]/ircd-hybrid/trunk/src/irc_string.c
ViewVC logotype

Diff of /ircd-hybrid/trunk/src/irc_string.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8160 by michael, Sat Dec 31 13:57:08 2016 UTC revision 8161 by michael, Sun Apr 9 11:27:54 2017 UTC
# Line 161  libio_basename(const char *path) Line 161  libio_basename(const char *path)
161    return s;    return s;
162  }  }
163    
164    /*    $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $    */
165    /*    $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $    */
166  /*  /*
167   * strlcat and strlcpy were ripped from openssh 2.5.1p2   * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
  * They had the following Copyright info:  
168   *   *
169     * Permission to use, copy, modify, and distribute this software for any
170     * purpose with or without fee is hereby granted, provided that the above
171     * copyright notice and this permission notice appear in all copies.
172   *   *
173   * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
174   * All rights reserved.   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
175   *   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
176   * Redistribution and use in source and binary forms, with or without   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
177   * modification, are permitted provided that the following conditions   * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
178   * are met:   * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
179   * 1. Redistributions of source code must retain the above copyright   * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *    notice, this list of conditions and the following disclaimer.  
  * 2. Redistributions in binary form must reproduce the above copyright  
  *    notice, this list of conditions and the following disclaimer in the  
  *    documentation and/or other materials provided with the distribution.  
  * 3. The name of the author may not be used to endorse or promote products  
  *    derived from this software without specific prior written permission.  
  *  
  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,  
  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY  
  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL  
  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
180   */   */
181    
182  #ifndef HAVE_STRLCAT  #ifndef HAVE_STRLCAT
183    /*
184     * Appends src to string dst of size dsize (unlike strncat, dsize is the
185     * full size of dst, not space left).  At most dsize-1 characters
186     * will be copied.  Always NUL terminates (unless dsize <= strlen(dst)).
187     * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
188     * If retval >= dsize, truncation occurred.
189     */
190  size_t  size_t
191  strlcat(char *dst, const char *src, size_t siz)  strlcat(char *dst, const char *src, size_t dsize)
192  {  {
193    char *d = dst;    const char *odst = dst;
194    const char *s = src;    const char *osrc = src;
195    size_t n = siz, dlen;    size_t n = dsize;
196      size_t dlen;
197    while (n-- != 0 && *d != '\0')  
198      d++;    /* Find the end of dst and adjust bytes left but don't go past end. */
199      while (n-- != 0 && *dst != '\0')
200        dst++;
201    
202    dlen = d - dst;    dlen = dst - odst;
203    n    = siz - dlen;    n = dsize - dlen;
204    
205    if (n == 0)    if (n-- == 0)
206      return dlen + strlen(s);      return dlen + strlen(src);
207    
208    while (*s != '\0')    while (*src != '\0')
209    {    {
210      if (n != 1)      if (n != 0)
211      {      {
212        *d++ = *s;        *dst++ = *src;
213        n--;        n--;
214      }      }
215    
216      s++;      src++;
217    }    }
218    
219    *d = '\0';    *dst = '\0';
220    return dlen + (s - src); /* count does not include NUL */  
221      return dlen + (src - osrc);  /* count does not include NUL */
222  }  }
223  #endif  #endif
224    
225  #ifndef HAVE_STRLCPY  #ifndef HAVE_STRLCPY
226    /*
227     * Copy string src to buffer dst of size dsize.  At most dsize-1
228     * chars will be copied.  Always NUL terminates (unless dsize == 0).
229     * Returns strlen(src); if retval >= dsize, truncation occurred.
230     */
231  size_t  size_t
232  strlcpy(char *dst, const char *src, size_t siz)  strlcpy(char *dst, const char *src, size_t dsize)
233  {  {
234    char *d = dst;    const char *osrc = src;
235    const char *s = src;    size_t nleft = dsize;
   size_t n = siz;  
236    
237    /* Copy as many bytes as will fit */    /* Copy as many bytes as will fit. */
238    if (n != 0 && --n != 0)    if (nleft != 0)
239    {    {
240      do      while (--nleft != 0)
241      {      {
242        if ((*d++ = *s++) == 0)        if ((*dst++ = *src++) == '\0')
243          break;          break;
244      } while (--n != 0);      }
245    }    }
246    
247    /* Not enough room in dst, add NUL and traverse rest of src */    /* Not enough room in dst, add NUL and traverse rest of src. */
248    if (n == 0)    if (nleft == 0)
249    {    {
250      if (siz != 0)      if (dsize != 0)
251        *d = '\0'; /* NUL-terminate dst */        *dst = '\0';  /* NUL-terminate dst */
252      while (*s++)  
253        while (*src++)
254        ;        ;
255    }    }
256    
257    return s - src - 1; /* count does not include NUL */    return src - osrc - 1;  /* count does not include NUL */
258  }  }
259  #endif  #endif

Legend:
Removed from v.8160  
changed lines
  Added in v.8161

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28