xref: /openbsd-src/gnu/lib/libiberty/src/stpncpy.c (revision 20fce977aadac3358da45d5027d7d19cdc03b0fe)
132d62142Sespie /* Implement the stpncpy function.
232d62142Sespie    Copyright (C) 2003 Free Software Foundation, Inc.
332d62142Sespie    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
432d62142Sespie 
532d62142Sespie This file is part of the libiberty library.
632d62142Sespie Libiberty is free software; you can redistribute it and/or
732d62142Sespie modify it under the terms of the GNU Library General Public
832d62142Sespie License as published by the Free Software Foundation; either
932d62142Sespie version 2 of the License, or (at your option) any later version.
1032d62142Sespie 
1132d62142Sespie Libiberty is distributed in the hope that it will be useful,
1232d62142Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1332d62142Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1432d62142Sespie Library General Public License for more details.
1532d62142Sespie 
1632d62142Sespie You should have received a copy of the GNU Library General Public
1732d62142Sespie License along with libiberty; see the file COPYING.LIB.  If
18*20fce977Smiod not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*20fce977Smiod Boston, MA 02110-1301, USA.  */
2032d62142Sespie 
2132d62142Sespie /*
2232d62142Sespie 
2332d62142Sespie @deftypefn Supplemental char* stpncpy (char *@var{dst}, const char *@var{src}, size_t @var{len})
2432d62142Sespie 
2532d62142Sespie Copies the string @var{src} into @var{dst}, copying exactly @var{len}
2632d62142Sespie and padding with zeros if necessary.  If @var{len} < strlen(@var{src})
2732d62142Sespie then return @var{dst} + @var{len}, otherwise returns @var{dst} +
2832d62142Sespie strlen(@var{src}).
2932d62142Sespie 
3032d62142Sespie @end deftypefn
3132d62142Sespie 
3232d62142Sespie */
3332d62142Sespie 
3432d62142Sespie #include <ansidecl.h>
3532d62142Sespie #include <stddef.h>
3632d62142Sespie 
37*20fce977Smiod extern size_t strlen (const char *);
38*20fce977Smiod extern char *strncpy (char *, const char *, size_t);
3932d62142Sespie 
4032d62142Sespie char *
stpncpy(char * dst,const char * src,size_t len)41*20fce977Smiod stpncpy (char *dst, const char *src, size_t len)
4232d62142Sespie {
4332d62142Sespie   size_t n = strlen (src);
4432d62142Sespie   if (n > len)
4532d62142Sespie     n = len;
4632d62142Sespie   return strncpy (dst, src, len) + n;
4732d62142Sespie }
48