curs_getstr(3X)
โจ NAME โจ
curs_getstr(3X) โ the library calls that let you read a whole character string off the curses terminal keyboard, bestie: getstr, getnstr, wgetstr, wgetnstr, mvgetstr, mvgetnstr, mvwgetstr, mvwgetnstr. ๐
๐ธ SYNOPSIS ๐ธ
#include <curses.h>
int getstr(char * str);
int wgetstr(WINDOW * win, char * str);
int mvgetstr(int y, int x, char * str);
int mvwgetstr(WINDOW * win, int y, int x, char * str);
int getnstr(char * str, int n);
int wgetnstr(WINDOW * win, char * str, int n);
int mvgetnstr(int y, int x, char * str, int n);
int mvwgetnstr(WINDOW * win, int y, int x, char * str, int n);
๐ DESCRIPTION ๐
okay so wgetstr fills up a string buffer str that you supply โ it keeps calling wgetch(3X) with the win argument over and over until it hits a line feed or a carriage return character. and while it's doing that, the function:
- does not copy that terminating character into
str; - always terminates
strwith a null character; - interprets the screen's erase and kill characters (peep
erasechar(3X)andkillchar(3X)); - only recognizes function keys if the screen's keypad option is enabled (see
keypad(3X)); - treats the function keys
KEY_LEFTandKEY_BACKSPACEexactly the same as the erase character; and - throws away function key inputs that aren't being treated as the erase or kill characters, and calls
beep(3X)about it. ๐ญ
If any characters have already been written into the input buffer, the erase character replaces the character sitting at the current position in the buffer with a null character, then decrements the position by one; the kill character does that same thing repeatedly, backtracking all the way to the beginning of the buffer. iconic behavior honestly.
If the screen's echo option is enabled (see echo(3X)), wgetstr updates win with waddch(3X). and also:
- the erase character and its function key synonyms move the cursor to the left โ unless it's already exactly where it was located when
wgetstrwas called โ and - the kill character returns the cursor to where it was located when
wgetstrwas called. โจ
wgetnstr is the same vibe but it reads at most n characters, which helps your application not overrun the buffer that str points at. curses just ignores any attempt to input more than n characters (other than the terminating line feed or carriage return), and calls beep(3X) instead. and if n is negative, wgetn_wstr reads up to LINE_MAX characters (see sysconf(3)). ๐ฆ
ncurses(3X) describes the variants of these functions.
๐ฅ RETURN VALUE ๐ฅ
These functions return OK when everything slays and ERR when it doesn't. ๐
In ncurses, these functions fail if:
- the curses screen has not been initialized,
- (for functions taking a
WINDOWpointer argument)winis a null pointer, stris a null pointer, or- an internal
wgetch(3X)call fails.
Further, in ncurses, these functions return KEY_RESIZE if a SIGWINCH event interrupts the function.
Functions prefixed with โmvโ first perform cursor movement, and they fail if the position (y, x) is outside the window boundaries.
๐ซถ NOTES ๐ซถ
All of these functions except wgetnstr may be implemented as macros. (iykyk, watch your argument evaluation.)
๐จ Reading input that overruns the buffer pointed to bystrcauses undefined results. Use the n-infixed functions, and allocate sufficient storage forstrโ at leastn+1timessizeof(char).
While these functions conceptually implement a series of calls to wgetch, they also temporarily change properties of the curses screen to permit simple editing of the input buffer. Each function saves the screen's state, calls nl(3X), and, if the screen was in canonical (โcookedโ) mode, cbreak(3X). Before returning, it restores the saved screen state. Other implementations differ in detail, which affects which control characters they can accept in the buffer; see the โPORTABILITYโ section below. ๐
๐ EXTENSIONS ๐
getnstr, wgetnstr, mvgetnstr, and mvwgetnstr's handling of negative n values is an ncurses extension.
The return value KEY_RESIZE is an ncurses extension.
๐ PORTABILITY ๐
Applications employing ncurses extensions should condition their use on the visibility of the NCURSES_VERSION preprocessor macro.
X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them, but indicates that wgetnstr and its variants read โthe entire multi-byte sequence associated with a characterโ and โfailโ if n and str together do not describe a buffer โlarge enough to contain any complete charactersโ. In ncurses, however, wgetch reads only single-byte characters, so this scenario does not arise. ๐
SVr4 describes a successful return value only as โan integer value other than ERRโ.
SVr3 and early SVr4 curses implementations did not reject function keys; the SVr4 documentation asserted that, like the screen's erase and kill characters, they were
interpreted, as well as any special keys (such as function keys, โhomeโ key, โclearโ key, etc.)
without further detail. It lied. ๐ญ The โcharacterโ value appended to the string by those implementations was predictable but not useful โ being, in fact, the low-order eight bits of the key code's KEY_ constant value. (The same language, unchanged except for styling, survived into X/Open Curses Issue 4, Version 2 but disappeared from Issue 7.)
A draft of X/Open Curses Issue 5 (which never saw final release) stated that these functions โread at most n bytesโ but did not state whether the terminating null character counted toward that limit. X/Open Curses Issue 7 changed that to say they โread at most n-1 bytesโ to allow for the terminating null character. As of 2018, some implementations count it, some do not. ๐
- ncurses 6.1 and PDCurses do not count the null character toward the limit, while Solaris and NetBSD curses do.
- Solaris xcurses offers both behaviors: its wide-character
wgetn_wstrreserves room for a wide null character, but its non-widewgetnstrdoes not consistently count a null character toward the limit.
X/Open Curses does not specify what happens if the length n is negative.
- ncurses 6.2 uses
LINE_MAXor a larger (system-dependent) value provided bysysconf(3). If neitherLINE_MAXnorsysconfis available, ncurses uses the POSIX minimum value forLINE_MAX(2048). In either case, it reserves a byte for the terminating null character. - In SVr4 curses, a negative
ntellswgetnstrto assume that the caller's buffer is large enough to hold the result; that is, the function then acts likewgetstr. X/Open Curses does not mention this behavior (or anything related to nonpositivenvalues), however most curses libraries implement it. Most implementations nevertheless enforce an upper limit on the count of bytes they write to the destination bufferstr. - BSD curses lacked
wgetnstr, and itswgetstrwrote tostrunboundedly, as did that in SVr2. - PDCurses, and SVr3 and later, and Solaris curses limit both functions to writing 256 bytes. Other System V-based platforms likely use the same limit.
- Solaris xcurses limits the write to
LINE_MAXbytes (seesysconf(3)). - NetBSD 7 curses imposes no particular limit on the length of the write, but does validate
nto ensure that it is greater than zero. A comment in NetBSD's source code asserts that SUSv2 specifies this.
Implementations vary in their handling of input control characters. ๐ต
- While they may enable the screen's echo option, some do not take it out of raw mode, and may take cbreak mode into account when deciding whether to handle echoing within
wgetnstror to rely on it as a side effect of callingwgetch. - Originally, ncurses, like its progenitor pcurses, had its
wgetnstrcallnorawandcbreakbefore accepting input. That may have been done to make function keys work; it is not necessary with modern ncurses.
Since 1995, ncurses has provided handlers for SIGINTR and SIGQUIT events, which are typically generated at the keyboard with ^C and ^\ respectively. In cbreak mode, those handlers catch a signal and stop the program, whereas other implementations write those characters into the buffer.
- Starting with ncurses 6.3 (2021),
wgetnstrpreserves raw mode if the screen was already in that state, allowing one to enter the characters the terminal interprets as interrupt and quit events into the buffer, for better compatibility with SVr4 curses.
โญ HISTORY โญ
4BSD (1980) introduced wgetstr along with its variants. ๐ฐ๏ธ
SVr3.1 (1987) added wgetnstr, but none of its variants.
X/Open Curses Issue 4 (1995) specified getnstr, mvgetnstr, and mvwgetnstr.
๐ SEE ALSO ๐
curs_get_wstr(3X) describes comparable functions of the ncurses library in its wide-character configuration (ncursesw).
curses(3X), curs_addch(3X), curs_getch(3X), curs_inopts(3X), curs_termattrs(3X)
and that's the whole page bestie, mwah ๐ โ this one's from ncurses 6.6, dated 2025-10-20, and it's the curs_getstr(3X) page. go forth and read strings responsibly ๐ซถโจ
๐ฆ where this page comes from
- alpine 3.24: ncurses-doc 6.6_p20260516-r0 ยท X11
- arch rolling: ncurses 6.6-2 ยท MIT-open-group
- fedora 44: ncurses-devel 6.6-1.fc44 ยท MIT-open-group
- fedora 44: ncurses-devel 6.6-1.fc44 ยท MIT-open-group
- Copyright 2018-2024,2025 Thomas E. Dickey
- Copyright 1998-2010,2017 Free Software Foundation, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms