]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_insnstr.c
ncurses 6.4 - patch 20231118
[ncurses.git] / ncurses / base / lib_insnstr.c
index dd51a9cd18f9ab41436d57c3b8aa7a33230e7128..338a96b322ff1d1063b0a4f94a94ab9be7ac9f97 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2018-2020,2022 Thomas E. Dickey                                *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
  * Copyright 2004-2009,2016 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -41,7 +41,7 @@
 #include <curses.priv.h>
 #include <ctype.h>
 
-MODULE_ID("$Id: lib_insnstr.c,v 1.8 2022/05/21 17:35:15 tom Exp $")
+MODULE_ID("$Id: lib_insnstr.c,v 1.9 2023/11/18 23:47:19 tom Exp $")
 
 NCURSES_EXPORT(int)
 winsnstr(WINDOW *win, const char *s, int n)
@@ -51,7 +51,7 @@ winsnstr(WINDOW *win, const char *s, int n)
 
     T((T_CALLED("winsnstr(%p,%s,%d)"), (void *) win, _nc_visbufn(s, n), n));
 
-    if (win != 0 && str != 0) {
+    if (win != 0 && str != 0 && n != 0) {
        SCREEN *sp = _nc_screen_of(win);
 #if USE_WIDEC_SUPPORT
        /*
@@ -62,7 +62,7 @@ winsnstr(WINDOW *win, const char *s, int n)
         * call to the wins_nwstr() function.
         */
        if (sp->_screen_unicode) {
-           size_t nn = (n > 0) ? (size_t) n : strlen(s);
+           size_t nn = (n >= 0) ? (size_t) n : strlen(s);
            wchar_t *buffer = typeMalloc(wchar_t, nn + 1);
            if (buffer != 0) {
                mbstate_t state;
@@ -83,7 +83,7 @@ winsnstr(WINDOW *win, const char *s, int n)
            NCURSES_SIZE_T ox = win->_curx;
            const unsigned char *cp;
 
-           for (cp = str; (n <= 0 || (cp - str) < n) && *cp; cp++) {
+           for (cp = str; (n < 0 || (cp - str) < n) && *cp; cp++) {
                _nc_insert_ch(sp, win, (chtype) UChar(*cp));
            }
            win->_curx = ox;