Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / port / strlcpy.c
blob410da1f88b0054f978a272694c98598b32f0fb69
1 /*-------------------------------------------------------------------------
3 * strlcpy.c
4 * strncpy done right
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 * This file was taken from OpenBSD and is used on platforms that don't
13 * provide strlcpy(). The OpenBSD copyright terms follow.
14 *-------------------------------------------------------------------------
17 /* $OpenBSD$ */
20 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
26 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
27 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
29 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
32 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #include "c.h"
39 * Copy src to string dst of size siz. At most siz-1 characters
40 * will be copied. Always NUL terminates (unless siz == 0).
41 * Returns strlen(src); if retval >= siz, truncation occurred.
42 * Function creation history: http://www.gratisoft.us/todd/papers/strlcpy.html
44 size_t
45 strlcpy(char *dst, const char *src, size_t siz)
47 char *d = dst;
48 const char *s = src;
49 size_t n = siz;
51 /* Copy as many bytes as will fit */
52 if (n != 0)
54 while (--n != 0)
56 if ((*d++ = *s++) == '\0')
57 break;
61 /* Not enough room in dst, add NUL and traverse rest of src */
62 if (n == 0)
64 if (siz != 0)
65 *d = '\0'; /* NUL-terminate dst */
66 while (*s++)
70 return (s - src - 1); /* count does not include NUL */