2006-08-01 15:43:28 +02:00
|
|
|
/*-
|
2009-05-27 11:58:24 +02:00
|
|
|
* Copyright (c) 2006, 2008, 2009
|
2008-07-07 14:59:54 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2006-08-01 15:43:28 +02:00
|
|
|
*
|
2009-05-16 20:57:54 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-08-01 15:43:28 +02:00
|
|
|
*/
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
#include "sh.h"
|
2006-08-01 15:43:28 +02:00
|
|
|
|
2009-06-10 20:12:51 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/strlcpy.c,v 1.7 2009/06/10 18:12:50 tg Rel $");
|
2006-11-08 23:54:55 +01:00
|
|
|
|
2006-08-01 15:43:28 +02:00
|
|
|
/*
|
2009-06-10 20:12:51 +02:00
|
|
|
* Copy src to string dst of size siz. At most siz-1 characters
|
|
|
|
* will be copied. Always NUL terminates (unless siz == 0).
|
2006-08-01 15:43:28 +02:00
|
|
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
|
|
|
*/
|
|
|
|
size_t
|
2009-04-07 21:15:48 +02:00
|
|
|
strlcpy(char *dst, const char *src, size_t siz)
|
2006-08-01 15:43:28 +02:00
|
|
|
{
|
2009-04-07 21:15:48 +02:00
|
|
|
const char *s = src;
|
2006-08-01 15:43:28 +02:00
|
|
|
|
2009-04-07 21:15:48 +02:00
|
|
|
if (siz == 0)
|
2006-08-01 15:43:28 +02:00
|
|
|
goto traverse_src;
|
|
|
|
|
|
|
|
/* copy as many chars as will fit */
|
2006-11-08 23:54:55 +01:00
|
|
|
while (--siz && (*dst++ = *s++))
|
2006-08-01 15:43:28 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
/* not enough room in dst */
|
2009-04-07 21:15:48 +02:00
|
|
|
if (siz == 0) {
|
2006-11-08 23:54:55 +01:00
|
|
|
/* safe to NUL-terminate dst since we copied <= siz-1 chars */
|
2009-04-07 21:15:48 +02:00
|
|
|
*dst = '\0';
|
2006-08-01 15:43:28 +02:00
|
|
|
traverse_src:
|
|
|
|
/* traverse rest of src */
|
|
|
|
while (*s++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2008-07-07 14:59:54 +02:00
|
|
|
/* count does not include NUL */
|
2009-05-27 11:58:24 +02:00
|
|
|
return ((size_t)(s - src - 1));
|
2006-08-01 15:43:28 +02:00
|
|
|
}
|