xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/stpncpy.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
198b9484cSchristos /* Implement the stpncpy function.
2*5173eb0aSchristos    Copyright (C) 2003-2024 Free Software Foundation, Inc.
398b9484cSchristos    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
498b9484cSchristos 
598b9484cSchristos This file is part of the libiberty library.
698b9484cSchristos Libiberty is free software; you can redistribute it and/or
798b9484cSchristos modify it under the terms of the GNU Library General Public
898b9484cSchristos License as published by the Free Software Foundation; either
998b9484cSchristos version 2 of the License, or (at your option) any later version.
1098b9484cSchristos 
1198b9484cSchristos Libiberty is distributed in the hope that it will be useful,
1298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1498b9484cSchristos Library General Public License for more details.
1598b9484cSchristos 
1698b9484cSchristos You should have received a copy of the GNU Library General Public
1798b9484cSchristos License along with libiberty; see the file COPYING.LIB.  If
1898b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1998b9484cSchristos Boston, MA 02110-1301, USA.  */
2098b9484cSchristos 
2198b9484cSchristos /*
2298b9484cSchristos 
2398b9484cSchristos @deftypefn Supplemental char* stpncpy (char *@var{dst}, const char *@var{src}, @
2498b9484cSchristos   size_t @var{len})
2598b9484cSchristos 
2698b9484cSchristos Copies the string @var{src} into @var{dst}, copying exactly @var{len}
2798b9484cSchristos and padding with zeros if necessary.  If @var{len} < strlen(@var{src})
2898b9484cSchristos then return @var{dst} + @var{len}, otherwise returns @var{dst} +
2998b9484cSchristos strlen(@var{src}).
3098b9484cSchristos 
3198b9484cSchristos @end deftypefn
3298b9484cSchristos 
3398b9484cSchristos */
3498b9484cSchristos 
3598b9484cSchristos #include <ansidecl.h>
3698b9484cSchristos #include <stddef.h>
3798b9484cSchristos 
3898b9484cSchristos extern size_t strlen (const char *);
3998b9484cSchristos extern char *strncpy (char *, const char *, size_t);
4098b9484cSchristos 
4198b9484cSchristos char *
4298b9484cSchristos stpncpy (char *dst, const char *src, size_t len)
4398b9484cSchristos {
4498b9484cSchristos   size_t n = strlen (src);
4598b9484cSchristos   if (n > len)
4698b9484cSchristos     n = len;
4798b9484cSchristos   return strncpy (dst, src, len) + n;
4898b9484cSchristos }
49