]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_inchstr.c
ncurses 6.2 - patch 20210213
[ncurses.git] / ncurses / base / lib_inchstr.c
index 884ea51fdb3bc8f0d4a21e825b464b6041b657d8..38af2393b1f84bf9830a79745c42ee1d7046c5db 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2001,2009 Free Software Foundation, Inc.              *
+ * Copyright 2020 Thomas E. Dickey                                          *
+ * Copyright 1998-2010,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            *
@@ -29,6 +30,7 @@
 /****************************************************************************
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ *     and: Thomas E. Dickey                        1996-on                 *
  ****************************************************************************/
 
 /*
@@ -40,7 +42,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_inchstr.c,v 1.11 2009/10/24 22:49:33 tom Exp $")
+MODULE_ID("$Id: lib_inchstr.c,v 1.17 2020/02/02 23:34:34 tom Exp $")
 
 NCURSES_EXPORT(int)
 winchnstr(WINDOW *win, chtype *str, int n)
@@ -49,16 +51,19 @@ winchnstr(WINDOW *win, chtype *str, int n)
 
     T((T_CALLED("winchnstr(%p,%p,%d)"), (void *) win, (void *) str, n));
 
-    if (!str)
-       returnCode(0);
+    if (!win || !str) {
+       i = ERR;
+    } else {
+       int row = win->_cury;
+       int col = win->_curx;
+       NCURSES_CH_T *text = win->_line[row].text;
 
-    if (win) {
-       for (; (n < 0 || (i < n)) && (win->_curx + i <= win->_maxx); i++)
-           str[i] =
-               CharOf(win->_line[win->_cury].text[win->_curx + i]) |
-               AttrOf(win->_line[win->_cury].text[win->_curx + i]);
+       for (; (n < 0 || (i < n)) && (col + i <= win->_maxx); i++) {
+           str[i] = (((chtype) CharOf(text[col + i]) & A_CHARTEXT) |
+                     AttrOf(text[col + i]));
+       }
+       str[i] = (chtype) 0;
     }
-    str[i] = (chtype) 0;
 
     returnCode(i);
 }