83c2ee87f4
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
58 lines
1.8 KiB
C
58 lines
1.8 KiB
C
/* $MirOS: src/bin/mksh/strlcpy.c,v 1.1 2007/03/04 03:04:28 tg Exp $ */
|
|
/* $miros: src/lib/libc/string/strlfun.c,v 1.14 2007/01/07 02:11:40 tg Exp $ */
|
|
/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
|
|
|
|
/*-
|
|
* Copyright (c) 2006, 2007
|
|
* Thorsten Glaser <tg@mirbsd.de>
|
|
* 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.
|
|
*
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
* damage or existence of a defect, except proven that it results out
|
|
* of said person's immediate fault when using the work as intended.
|
|
*/
|
|
|
|
#include "sh.h"
|
|
|
|
__RCSID("$MirOS: src/bin/mksh/strlcpy.c,v 1.1 2007/03/04 03:04:28 tg Exp $");
|
|
|
|
/*
|
|
* Copy src to string dst of size siz. At most siz-1 characters
|
|
* will be copied. Always NUL terminates (unless siz == 0).
|
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
|
*/
|
|
size_t
|
|
strlcpy(char *dst, const char *src, size_t siz)
|
|
{
|
|
const char *s = src;
|
|
|
|
if (siz == 0)
|
|
goto traverse_src;
|
|
|
|
/* copy as many chars as will fit */
|
|
while (--siz && (*dst++ = *s++))
|
|
;
|
|
|
|
/* not enough room in dst */
|
|
if (siz == 0) {
|
|
/* safe to NUL-terminate dst since we copied <= siz-1 chars */
|
|
*dst = '\0';
|
|
traverse_src:
|
|
/* traverse rest of src */
|
|
while (*s++)
|
|
;
|
|
}
|
|
|
|
/* count doesn't include NUL */
|
|
return (s - src - 1);
|
|
}
|