]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_addstr.c
ncurses 6.4 - patch 20240420
[ncurses.git] / ncurses / base / lib_addstr.c
index 19ebfcd5b337fe4b90fb0cadf8174d8e657ce6d4..a3753af0d53e80cf90a3706abc4e3aecb7f7f725 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2019,2020 Thomas E. Dickey                                     *
+ * Copyright 2019-2022,2023 Thomas E. Dickey                                *
  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -45,7 +45,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_addstr.c,v 1.56 2020/02/02 23:34:34 tom Exp $")
+MODULE_ID("$Id: lib_addstr.c,v 1.62 2023/11/21 21:47:23 tom Exp $")
 
 NCURSES_EXPORT(int)
 waddnstr(WINDOW *win, const char *astr, int n)
@@ -55,14 +55,17 @@ waddnstr(WINDOW *win, const char *astr, int n)
 
     T((T_CALLED("waddnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(astr, n), n));
 
-    if (win && (str != 0)) {
+    if (win && (str != 0) && (n != 0)) {
+       bool explicit = (n > 0);
+
        TR(TRACE_VIRTPUT | TRACE_ATTRS,
           ("... current %s", _traceattr(WINDOW_ATTRS(win))));
        code = OK;
-       if (n < 0)
-           n = (int) strlen(astr);
 
-       TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
+       TR(TRACE_VIRTPUT, ("str is not null, length = %d",
+                          (explicit ? n : (int) strlen(str))));
+       if (!explicit)
+           n = INT_MAX;
        while ((n-- > 0) && (*str != '\0')) {
            NCURSES_CH_T ch;
            TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str)));
@@ -143,7 +146,7 @@ wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n)
        _nc_viscbuf(astr, n),
        n));
 
-    if (!win)
+    if (!win || !astr)
        returnCode(ERR);
 
     y = win->_cury;
@@ -227,14 +230,17 @@ waddnwstr(WINDOW *win, const wchar_t *str, int n)
 
     T((T_CALLED("waddnwstr(%p,%s,%d)"), (void *) win, _nc_viswbufn(str, n), n));
 
-    if (win && (str != 0)) {
+    if (win && (str != 0) && (n != 0)) {
+       bool explicit = (n > 0);
+
        TR(TRACE_VIRTPUT | TRACE_ATTRS,
           ("... current %s", _traceattr(WINDOW_ATTRS(win))));
        code = OK;
-       if (n < 0)
-           n = (int) wcslen(str);
 
-       TR(TRACE_VIRTPUT, ("str is not null, length = %d", n));
+       TR(TRACE_VIRTPUT, ("str is not null, length = %d",
+                          (explicit ? n : (int) wcslen(str))));
+       if (!explicit)
+           n = INT_MAX;
        while ((n-- > 0) && (*str != L('\0'))) {
            NCURSES_CH_T ch;
            TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str));