diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-05-23 04:42:30 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-05-23 04:42:30 +0000 |
commit | 1690c05075e2d9026403e4f86111a494bd0d391b (patch) | |
tree | 2a0aefed6308148ac40c82df7aa538d5564ca51d | |
parent | fd3e6e84fbc665fa284aa900a2a7269ce89c61d4 (diff) | |
download | tinyproxy-1690c05075e2d9026403e4f86111a494bd0d391b.tar.gz tinyproxy-1690c05075e2d9026403e4f86111a494bd0d391b.zip |
Initial addition to CVS. Moved the three "text" related functions into a
separate file.
-rw-r--r-- | src/text.c | 99 | ||||
-rw-r--r-- | src/text.h | 31 |
2 files changed, 130 insertions, 0 deletions
diff --git a/src/text.c b/src/text.c new file mode 100644 index 0000000..f58302f --- /dev/null +++ b/src/text.c @@ -0,0 +1,99 @@ +/* $Id: text.c,v 1.1 2002-05-23 04:42:30 rjkaes Exp $ + * + * The functions included here are useful for text manipulation. They + * replace or augment the standard C string library. These functions + * are either safer replacements, or they provide services not included + * with the standard C string library. + * + * Copyright (C) 2002 Robert James Kaes (rjkaes@flarenet.com) + * + * 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, 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. + */ + +#include "tinyproxy.h" + +#include "text.h" + +#ifndef HAVE_STRLCPY +/* + * Function API taken from OpenBSD. Like strncpy(), but does not 0 fill the + * buffer, and always NULL terminates the buffer. size is the size of the + * destination buffer. + */ +size_t +strlcpy(char *dst, const char *src, size_t size) +{ + size_t len = strlen(src); + size_t ret = len; + + if (len >= size) + len = size - 1; + + memcpy(dst, src, len); + dst[len] = '\0'; + + return ret; +} +#endif + +#ifndef HAVE_STRLCAT +/* + * Function API taken from OpenBSD. Like strncat(), but does not 0 fill the + * buffer, and always NULL terminates the buffer. size is the length of the + * buffer, which should be one more than the maximum resulting string + * length. + */ +size_t +strlcat(char *dst, const char *src, size_t size) +{ + size_t len1 = strlen(dst); + size_t len2 = strlen(src); + size_t ret = len1 + len2; + + if (len1 + len2 >= size) + len2 = size - len1 - 1; + if (len2 > 0) { + memcpy(dst + len1, src, len2); + dst[len1 + len2] = '\0'; + } + + return ret; +} +#endif + +/* + * Removes any new-line or carriage-return characters from the end of the + * string. This function is named afrer the same function in Perl. + * "length" should be the number of characters in the buffer, not including + * the trailing NULL. + * + * Returns the number of characters removed from the end of the string. + */ +size_t +chomp(char *buffer, size_t length) +{ + size_t chars; + + assert(buffer != NULL); + + chars = 0; + + --length; + while (buffer[length] == '\r' || buffer[length] == '\n') { + buffer[length--] = '\0'; + chars++; + + if (length < 0) + break; + } + + return chars; +} diff --git a/src/text.h b/src/text.h new file mode 100644 index 0000000..fd0940a --- /dev/null +++ b/src/text.h @@ -0,0 +1,31 @@ +/* $Id: text.h,v 1.1 2002-05-23 04:42:30 rjkaes Exp $ + * + * See 'text.c' for a detailed description. + * + * Copyright (C) 2002 Robert James Kaes (rjkaes@flarenet.com) + * + * 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, 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. + */ + +#ifndef TINYPROXY_TEXT_H +#define TINYPROXY_TEXT_H + +#ifndef HAVE_STRLCAT +extern size_t strlcat(char *dst, const char *src, size_t size); +#endif /* HAVE_STRLCAT */ + +#ifndef HAVE_STRLCPY +extern size_t strlcpy(char *dst, const char *src, size_t size); +#endif /* HAVE_STRLCPY */ + +extern size_t chomp(char *buffer, size_t length); + +#endif |