/****************************************************************************
- * Copyright (c) 2000 Free Software Foundation, Inc. *
+ * Copyright (c) 2000-2007,2012 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
#include <curses.priv.h>
-MODULE_ID("$Id")
+MODULE_ID("$Id: strings.c,v 1.8 2012/02/22 22:34:31 tom Exp $")
/****************************************************************************
* Useful string functions (especially for mvcur)
****************************************************************************/
#if !HAVE_STRSTR
-char *
+NCURSES_EXPORT(char *)
_nc_strstr(const char *haystack, const char *needle)
{
size_t len1 = strlen(haystack);
while ((len1 != 0) && (len1-- >= len2)) {
if (!strncmp(haystack, needle, len2)) {
- result = haystack;
+ result = (char *) haystack;
break;
}
haystack++;
#endif
/*
- * Initialize the descriptor so we can append to it.
+ * Initialize the descriptor so we can append to it. Note that 'src' may
+ * be a null pointer (see _nc_str_null), so the corresponding strcat and
+ * strcpy calls have to allow for this.
*/
-string_desc *
+NCURSES_EXPORT(string_desc *)
_nc_str_init(string_desc * dst, char *src, size_t len)
{
if (dst != 0) {
dst->s_head = src;
dst->s_tail = src;
dst->s_size = len - 1;
+ dst->s_init = dst->s_size;
if (src != 0)
*src = 0;
}
/*
* Initialize the descriptor for only tracking the amount of memory used.
*/
-string_desc *
+NCURSES_EXPORT(string_desc *)
_nc_str_null(string_desc * dst, size_t len)
{
return _nc_str_init(dst, 0, len);
/*
* Copy a descriptor
*/
-string_desc *
+NCURSES_EXPORT(string_desc *)
_nc_str_copy(string_desc * dst, string_desc * src)
{
*dst = *src;
/*
* Replaces strcat into a fixed buffer, returning false on failure.
*/
-bool
+NCURSES_EXPORT(bool)
_nc_safe_strcat(string_desc * dst, const char *src)
{
if (src != 0) {
if (len < dst->s_size) {
if (dst->s_tail != 0) {
- strcpy(dst->s_tail, src);
+ _nc_STRCPY(dst->s_tail, src, dst->s_size);
dst->s_tail += len;
}
dst->s_size -= len;
/*
* Replaces strcpy into a fixed buffer, returning false on failure.
*/
-bool
+NCURSES_EXPORT(bool)
_nc_safe_strcpy(string_desc * dst, const char *src)
{
if (src != 0) {
if (len < dst->s_size) {
if (dst->s_head != 0) {
- strcpy(dst->s_head, src);
+ _nc_STRCPY(dst->s_head, src, dst->s_size);
dst->s_tail = dst->s_head + len;
}
- dst->s_size -= len;
+ dst->s_size = dst->s_init - len;
return TRUE;
}
}