From bf667e7e9c45b0b0c3787c1023c3b0f6de284cd8 Mon Sep 17 00:00:00 2001 From: erin_yueh Date: Wed, 13 Feb 2008 03:48:28 +0000 Subject: gsmd: add strlcpy, strlcat functions (Paulius Zaleckas) git-svn-id: http://svn.openmoko.org/trunk/src/target/gsm@4060 99fdad57-331a-0410-800a-d7fa5415bdb3 --- src/gsmd/strl.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/gsmd/strl.c (limited to 'src/gsmd/strl.c') diff --git a/src/gsmd/strl.c b/src/gsmd/strl.c new file mode 100644 index 0000000..cf20ef0 --- /dev/null +++ b/src/gsmd/strl.c @@ -0,0 +1,86 @@ +/* safe strcpy and strcat versions + * + * Copyright (C) 1991, 1992 Linus Torvalds + * Copyright (C) 2008 by Paulius Zaleckas, JSC Teltonika + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +#include "gsmd.h" +#include + +/** + * strlcpy - Copy a %NUL terminated string into a sized buffer + * @dest: Where to copy the string to + * @src: Where to copy the string from + * @size: size of destination buffer + * + * Compatible with *BSD: the result is always a valid + * NUL-terminated string that fits in the buffer (unless, + * of course, the buffer size is zero). It does not pad + * out the result like strncpy() does. + */ +size_t strlcpy(char *dest, const char *src, size_t size) +{ + size_t ret = strlen(src); + + if (size) { + size_t len; + if (ret >= size) { + len = size - 1; + DEBUGP("\"%s\" was truncated by %i characters\n", src, + ret - len); + } + else + len = ret; + memcpy(dest, src, len); + dest[len] = '\0'; + } + return ret; +} + +/** + * strlcat - Append a length-limited, %NUL-terminated string to another + * @dest: The string to be appended to + * @src: The string to append to it + * @count: The size of the destination buffer. + */ +size_t strlcat(char *dest, const char *src, size_t count) +{ + size_t dsize = strlen(dest); + size_t len = strlen(src); + size_t res = dsize + len; + + /* This would be a bug */ + if (dsize >= count) { + DEBUGP("Length of destination string > provided buffer size!\n"); + return 0; + } + + dest += dsize; + count -= dsize; + if (len >= count) { + len = count - 1; + DEBUGP("\"%s\" was truncated by %i characters\n", src, + res - len); + } + memcpy(dest, src, len); + dest[len] = 0; + return res; +} + -- cgit v1.2.3