Header And Logo

PostgreSQL
| The world's most advanced open source database.

strlcat.c

Go to the documentation of this file.
00001 /*
00002  * src/port/strlcat.c
00003  *
00004  *  $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $  */
00005 
00006 /*
00007  * Copyright (c) 1998 Todd C. Miller <[email protected]>
00008  *
00009  * Permission to use, copy, modify, and distribute this software for any
00010  * purpose with or without fee is hereby granted, provided that the above
00011  * copyright notice and this permission notice appear in all copies.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00014  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00015  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00016  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00018  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00019  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  */
00021 
00022 #include "c.h"
00023 
00024 
00025 /*
00026  * Appends src to string dst of size siz (unlike strncat, siz is the
00027  * full size of dst, not space left).  At most siz-1 characters
00028  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
00029  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
00030  * If retval >= siz, truncation occurred.
00031  */
00032 size_t
00033 strlcat(char *dst, const char *src, size_t siz)
00034 {
00035     char       *d = dst;
00036     const char *s = src;
00037     size_t      n = siz;
00038     size_t      dlen;
00039 
00040     /* Find the end of dst and adjust bytes left but don't go past end */
00041     while (n-- != 0 && *d != '\0')
00042         d++;
00043     dlen = d - dst;
00044     n = siz - dlen;
00045 
00046     if (n == 0)
00047         return (dlen + strlen(s));
00048     while (*s != '\0')
00049     {
00050         if (n != 1)
00051         {
00052             *d++ = *s;
00053             n--;
00054         }
00055         s++;
00056     }
00057     *d = '\0';
00058 
00059     return (dlen + (s - src));  /* count does not include NUL */
00060 }