]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/strings.c
ncurses 6.1 - patch 20190810
[ncurses.git] / ncurses / tinfo / strings.c
index 5fa68caf75346edb5ad0a366544c790b3e9aeec1..10ec6c8d8ac9f98a27b47823177c1fd4a543d17f 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2000 Free Software Foundation, Inc.                        *
+ * Copyright (c) 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 +35,9 @@
 **/
 
 #include <curses.priv.h>
+#include <tic.h>
 
-MODULE_ID("$Id: strings.c,v 1.3 2000/12/10 02:55:08 tom Exp $")
+MODULE_ID("$Id: strings.c,v 1.9 2017/08/26 13:16:11 tom Exp $")
 
 /****************************************************************************
  * Useful string functions (especially for mvcur)
@@ -44,8 +45,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 +53,7 @@ _nc_strstr
 
     while ((len1 != 0) && (len1-- >= len2)) {
        if (!strncmp(haystack, needle, len2)) {
-           result = haystack;
+           result = (char *) haystack;
            break;
        }
        haystack++;
@@ -63,16 +63,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 +85,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 +94,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 +106,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 +127,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;
        }
     }