X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fstrings.c;h=a1d8beb63f31e6db47f6e6c6b58df25588b93acc;hp=5fa68caf75346edb5ad0a366544c790b3e9aeec1;hb=a28e782d7794ddeec23e7cb212dc455f0d93dc22;hpb=46722468f47c2b77b3987729b4bcf2321cccfd01 diff --git a/ncurses/tinfo/strings.c b/ncurses/tinfo/strings.c index 5fa68caf..a1d8beb6 100644 --- a/ncurses/tinfo/strings.c +++ b/ncurses/tinfo/strings.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2000 Free Software Foundation, Inc. * + * Copyright 2020 Thomas E. Dickey * + * Copyright 2000-2012,2017 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 * @@ -35,8 +36,9 @@ **/ #include +#include -MODULE_ID("$Id: strings.c,v 1.3 2000/12/10 02:55:08 tom Exp $") +MODULE_ID("$Id: strings.c,v 1.10 2020/02/02 23:34:34 tom Exp $") /**************************************************************************** * Useful string functions (especially for mvcur) @@ -44,8 +46,7 @@ MODULE_ID("$Id: strings.c,v 1.3 2000/12/10 02:55:08 tom Exp $") #if !HAVE_STRSTR NCURSES_EXPORT(char *) -_nc_strstr -(const char *haystack, const char *needle) +_nc_strstr(const char *haystack, const char *needle) { size_t len1 = strlen(haystack); size_t len2 = strlen(needle); @@ -53,7 +54,7 @@ _nc_strstr while ((len1 != 0) && (len1-- >= len2)) { if (!strncmp(haystack, needle, len2)) { - result = haystack; + result = (char *) haystack; break; } haystack++; @@ -63,16 +64,18 @@ _nc_strstr #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. */ NCURSES_EXPORT(string_desc *) -_nc_str_init -(string_desc * dst, char *src, size_t len) +_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; } @@ -83,8 +86,7 @@ _nc_str_init * Initialize the descriptor for only tracking the amount of memory used. */ NCURSES_EXPORT(string_desc *) -_nc_str_null -(string_desc * dst, size_t len) +_nc_str_null(string_desc * dst, size_t len) { return _nc_str_init(dst, 0, len); } @@ -93,8 +95,7 @@ _nc_str_null * Copy a descriptor */ NCURSES_EXPORT(string_desc *) -_nc_str_copy -(string_desc * dst, string_desc * src) +_nc_str_copy(string_desc * dst, string_desc * src) { *dst = *src; return dst; @@ -106,12 +107,12 @@ _nc_str_copy NCURSES_EXPORT(bool) _nc_safe_strcat(string_desc * dst, const char *src) { - if (src != 0) { + if (PRESENT(src)) { size_t len = strlen(src); 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; @@ -127,15 +128,15 @@ _nc_safe_strcat(string_desc * dst, const char *src) NCURSES_EXPORT(bool) _nc_safe_strcpy(string_desc * dst, const char *src) { - if (src != 0) { + if (PRESENT(src)) { size_t len = strlen(src); 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; } }