]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - strlcpy.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / strlcpy.c
1 /* -*-pgsql-c-*- */
2 /*
3  *
4  * $Header: /cvsroot/pgpool/pgpool-II/strlcpy.c,v 1.2.2.1 2009/08/22 04:19:49 t-ishii Exp $
5  *
6  * This file was imported from PostgreSQL source code.
7  * See below for the copyright and description.
8  *
9  * pgpool: a language independent connection pool server for PostgreSQL
10  * written by Tatsuo Ishii
11  *
12  * Portions Copyright (c) 2003-2008     PgPool Global Development Group
13  *
14  */
15 /*-------------------------------------------------------------------------
16  *
17  * strlcpy.c
18  *        strncpy done right
19  *
20  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
21  *
22  *
23  * IDENTIFICATION
24  *        $PostgreSQL: pgsql/src/port/strlcpy.c,v 1.3 2006/10/04 00:30:14 momjian Exp $
25  *
26  * This file was taken from OpenBSD and is used on platforms that don't
27  * provide strlcpy().  The OpenBSD copyright terms follow.
28  *-------------------------------------------------------------------------
29  */
30
31 /*      $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $    */
32
33 /*
34  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
35  *
36  * Permission to use, copy, modify, and distribute this software for any
37  * purpose with or without fee is hereby granted, provided that the above
38  * copyright notice and this permission notice appear in all copies.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47  */
48
49 #include <unistd.h>
50 #include "pool.h"
51
52 /*
53  * Copy src to string dst of size siz.  At most siz-1 characters
54  * will be copied.      Always NUL terminates (unless siz == 0).
55  * Returns strlen(src); if retval >= siz, truncation occurred.
56  * Function creation history:  http://www.gratisoft.us/todd/papers/strlcpy.html
57  */
58 size_t
59 strlcpy(char *dst, const char *src, size_t siz)
60 {
61         char       *d = dst;
62         const char *s = src;
63         size_t          n = siz;
64
65         /* Copy as many bytes as will fit */
66         if (n != 0)
67         {
68                 while (--n != 0)
69                 {
70                         if ((*d++ = *s++) == '\0')
71                                 break;
72                 }
73         }
74
75         /* Not enough room in dst, add NUL and traverse rest of src */
76         if (n == 0)
77         {
78                 if (siz != 0)
79                         *d = '\0';                      /* NUL-terminate dst */
80                 while (*s++)
81                         ;
82         }
83
84         return (s - src - 1);           /* count does not include NUL */
85 }