Skip to content

Commit 996b203

Browse files
committed
Add strlcpy() to the set of functions supported by src/port/ when not
available directly on the platform. Per discussion, this function is sufficiently widely recognized to be treated as standard.
1 parent 1d45168 commit 996b203

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14505,7 +14505,8 @@ fi
1450514505

1450614506

1450714507

14508-
for ac_func in crypt fseeko getopt getrusage inet_aton random rint srandom strdup strerror strtol strtoul unsetenv
14508+
14509+
for ac_func in crypt fseeko getopt getrusage inet_aton random rint srandom strdup strerror strlcpy strtol strtoul unsetenv
1450914510
do
1451014511
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
1451114512
echo "$as_me:$LINENO: checking for $ac_func" >&5

configure.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $PostgreSQL: pgsql/configure.in,v 1.475 2006/09/23 01:33:32 pgsql Exp $
2+
dnl $PostgreSQL: pgsql/configure.in,v 1.476 2006/09/27 16:29:45 tgl Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -958,7 +958,7 @@ else
958958
AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
959959
fi
960960

961-
AC_REPLACE_FUNCS([crypt fseeko getopt getrusage inet_aton random rint srandom strdup strerror strtol strtoul unsetenv])
961+
AC_REPLACE_FUNCS([crypt fseeko getopt getrusage inet_aton random rint srandom strdup strerror strlcpy strtol strtoul unsetenv])
962962

963963
# System's version of getaddrinfo(), if any, may be used only if we found
964964
# a definition for struct addrinfo; see notes in src/include/getaddrinfo.h.

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@
375375
/* Define to 1 if you have the <string.h> header file. */
376376
#undef HAVE_STRING_H
377377

378+
/* Define to 1 if you have the `strlcpy' function. */
379+
#undef HAVE_STRLCPY
380+
378381
/* Define to 1 if you have the `strtol' function. */
379382
#undef HAVE_STRTOL
380383

src/include/port.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.99 2006/09/22 21:39:58 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.100 2006/09/27 16:29:46 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -329,7 +329,11 @@ extern int inet_aton(const char *cp, struct in_addr * addr);
329329
#endif
330330

331331
#ifndef HAVE_STRDUP
332-
extern char *strdup(char const *);
332+
extern char *strdup(const char *str);
333+
#endif
334+
335+
#ifndef HAVE_STRLCPY
336+
extern size_t strlcpy(char *dst, const char *src, size_t siz);
333337
#endif
334338

335339
#ifndef HAVE_RANDOM

src/port/strlcpy.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* strlcpy.c
4+
* strncpy done right
5+
*
6+
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7+
*
8+
*
9+
* IDENTIFICATION
10+
* $PostgreSQL: pgsql/src/port/strlcpy.c,v 1.1 2006/09/27 16:29:46 tgl Exp $
11+
*
12+
* This file was taken from OpenBSD and is used on platforms that don't
13+
* provide strlcpy(). The OpenBSD copyright terms follow.
14+
*-------------------------------------------------------------------------
15+
*/
16+
17+
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
18+
19+
/*
20+
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
21+
*
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.
25+
*
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.
33+
*/
34+
35+
#include "c.h"
36+
37+
38+
/*
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+
*/
43+
size_t
44+
strlcpy(char *dst, const char *src, size_t siz)
45+
{
46+
char *d = dst;
47+
const char *s = src;
48+
size_t n = siz;
49+
50+
/* Copy as many bytes as will fit */
51+
if (n != 0) {
52+
while (--n != 0) {
53+
if ((*d++ = *s++) == '\0')
54+
break;
55+
}
56+
}
57+
58+
/* Not enough room in dst, add NUL and traverse rest of src */
59+
if (n == 0) {
60+
if (siz != 0)
61+
*d = '\0'; /* NUL-terminate dst */
62+
while (*s++)
63+
;
64+
}
65+
66+
return(s - src - 1); /* count does not include NUL */
67+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy