util(3NCURSES)

πŸŽ€ util β€” the little grab-bag of curses helpers πŸŽ€

okay so this is the util(3NCURSES) page, bestie. it's the library calls corner of the ncurses manual, and it's where all the odds-and-ends live β€” the functions that didn't fit anywhere else but are lowkey essential. grab a snack, we're going through all of them. πŸ’…


✨ NAME ✨

delay_output, filter, flushinp, getwin, key_name, keyname, nofilter, putwin, unctrl, use_env, use_tioctl, wunctrl β€” miscellaneous curses utility routines 🧰✨


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <curses.h>

const char * unctrl(chtype ch);
wchar_t * wunctrl(cchar_t * wch);

const char * keyname(int c);
const char * key_name(wchar_t wc);

void filter(void);
/* extension */
void nofilter(void);

void use_env(bool bf);
/* extension */
void use_tioctl(bool bf);

int putwin(WINDOW * win, FILE * filep);
WINDOW * getwin(FILE * filep);

int delay_output(int ms);

int flushinp(void);

πŸ’œ DESCRIPTION πŸ’œ

🌸 unctrl, wunctrl 🌸

unctrl hands you back a null-terminated character string that printably represents the curses character ch β€” and ngl, that ch usually came from keyboard input. see getch(3NCURSES) for the receipts.

here's the whole vibe check on how it renders stuff:

  • Printable characters just represent themselves, as a one-character string. no drama. πŸ’…
  • Control characters get the ^X treatment, where X is the printable symbol of the control code's value plus 32 in the ISO 646 / β€œASCII” character set.
  • DEL (character code 127) shows up as ^?. iconic honestly.
  • A character code greater than 127 gets represented one of two ways:
    • if the screen has not been initialized or is in meta mode (peep meta(3NCURSES)), it's written in M-X notation, where X is the representation of the code's value minus 128, described the same way as above.
    • if the screen is not in meta mode, the character code is assumed to represent itself. but heads up β€” it might not actually be printable! that's exactly the case for character codes 128–159 in ISO 8859 encodings. 😭

ncurses's use_legacy_coding(3NCURSES) function is what configures how unctrl handles these character codes.

wunctrl returns a null-terminated wide-character string that printably represents the curses complex character wch.

both of these functions ignore the attributes and color pair selection of their argument. they do not care about your aesthetic choices, only the character itself. πŸ™ˆ


🌈 keyname, key_name 🌈

the keyname routine returns a character string corresponding to the key c. and listen β€” key codes are different from character codes. do not conflate them, bestie.

  • Key codes below 256 are characters. they get displayed using unctrl.
  • Values above 256 may be the codes for function keys, in which case the function key name gets displayed.
  • Otherwise β€” like, if there's no corresponding name and the key isn't a character β€” the function returns null, which is how it denotes an error. X/Open also lists an β€œUNKNOWN KEY” return value, and some implementations return that instead of null. so watch out for that.

the matching key_name returns a multibyte character string corresponding to the wide-character value wc. and here's the tea: these two functions (keyname and key_name) do not return the same set of strings:

  • keyname returns null in the places where key_name would display a meta character.
  • key_name does not return the name of a function key.

🧚 filter, nofilter 🧚

the filter routine β€” if you're gonna use it β€” must be called before initscr or newterm are called. calling filter causes these changes at initialization:

  • LINES is set to 1;
  • the capabilities clear, cud1, cud, cup, cuu1, cuu, vpa are disabled;
  • the capability ed is disabled if bce is set;
  • and the home string is set to the value of cr.

the nofilter routine cancels the effect of a preceding filter call. that's what lets the caller initialize a screen on a different device, using a different value of $TERM. the limitation arises because filter modifies the in-memory copy of the terminal information. so it's not un-doable by just changing your env, iykyk.


🌟 use_env 🌟

the use_env routine, if you're using it, should be called before initscr or newterm are called β€” because those are the ones that compute the screen size. it modifies the way ncurses treats environment variables when it's figuring out how big your screen is.

  • Normally, ncurses looks first at the terminal database for the screen size.

if use_env was called with FALSE for parameter, it stops right there β€” unless use_tioctl was also called with TRUE for parameter.

  • Then it asks for the screen size via operating system calls. if that works, it overrides the values from the terminal database.
  • Finally (unless use_env was called with FALSE parameter), ncurses examines the LINES or COLUMNS environment variables, and uses a value in those to override the results from the operating system or the terminal database.

curses also updates the screen size in response to SIGWINCH, unless it's overridden by the LINES or COLUMNS environment variables,


πŸ‘‘ use_tioctl πŸ‘‘

the use_tioctl routine, if used, should be called before initscr or newterm are called β€” same reason, those are the size-computing ones. after use_tioctl is called with TRUE as an argument, ncurses modifies that last step in its computation of screen size like this:

  • it checks whether the LINES and COLUMNS environment variables are set to a number greater than zero.
  • for each one, ncurses updates the corresponding environment variable with the value it obtained via operating system call or from the terminal database.
  • ncurses re-fetches the value of the environment variables, so that it's still the environment variables that set the screen size.

now the way use_env and use_tioctl combine is giving combinatorics, so let's lay it out:

  • use_env = TRUE, use_tioctl = FALSE β€” ncurses uses operating system calls unless overridden by LINES or COLUMNS environment variables. this is the default. ⭐
  • use_env = TRUE, use_tioctl = TRUE β€” ncurses updates LINES and COLUMNS based on operating system calls.
  • use_env = FALSE, use_tioctl = TRUE β€” ncurses ignores LINES and COLUMNS, using operating system calls to obtain size.

πŸ“ putwin, getwin πŸ“

the putwin routine writes all data associated with window (or pad) win into the file that filep points to. you can pull that information back out later using the getwin function. save it for later, bestie. πŸ’Ύ

the getwin routine reads the window-related data that was stored in the file by putwin. then it creates and initializes a new window using that data, and returns a pointer to the new window. but there are a few caveats, and you know i'm gonna tell you all of them:

  • the data written is a copy of the WINDOW structure, plus its associated character cells. the format differs between the wide-character (ncursesw) and non-wide (ncurses) libraries. you can transfer data between the two, however.
  • the retrieved window is always created as a top-level window (or pad), rather than a subwindow.
  • the window's character cells contain the color pair value, but not the actual color numbers. so if cells in the retrieved window use color pairs that haven't been created in the application using init_pair, they will not be colored when the window is refreshed. tragic, but that's the deal. 😭

⏳ delay_output ⏳

the delay_output routine inserts an ms millisecond pause in output.

employ this function judiciously when terminal output uses padding β€” because ncurses transmits null characters (which eats up CPU and I/O resources) instead of sleeping and requesting resumption from the operating system. it's giving busy-wait energy and it's not cute.

padding is used unless:

  • the terminal description has the npc (no_pad_char) capability, or
  • the environment variable NCURSES_NO_PADDING is set.

if padding is not in use, ncurses uses napms to perform the delay. and if the value of ms exceeds 30,000 (thirty seconds), it gets capped at that value. so no, you cannot ask for a five-minute nap. πŸ™ˆ


πŸ’… flushinp πŸ’…

the flushinp routine throws away any typeahead that the user has typed but that the program hasn't read yet. bye bye, keystrokes. πŸ‘‹


⭐ RETURN VALUE ⭐

except for flushinp, functions that return integers return ERR upon failure and OK upon success.

functions that return pointers return a null pointer on failure.

in ncurses specifically:

  • flushinp returns ERR if the terminal was not initialized, and
  • putwin returns ERR if its associated write(2) calls return ERR.

🫢 NOTES 🫢

wunctrl is part of ncurses's wide-character API, and it is not available in its non-wide-character configuration. so if you're on the narrow build, that one's just not in the group chat. πŸ’€

🌈 PORTABILITY 🌈

X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them. Like, none. It just vibes.

SVr4 describes a successful return value only as β€œan integer value other than ERR”. That's it. That's the whole description. so helpful bestie πŸ’€


✨ filter ✨

The SVr4 documentation describes the action of filter only in the vaguest terms. The description here is adapted from X/Open Curses (which erroneously fails to describe the disabling of cuu). yikes πŸ™ˆ

✨ delay_output padding ✨

The limitation to 30 seconds and the use of napms differ from other implementations.

  • SVr4 curses does not delay if no padding character is available.
  • NetBSD curses uses napms when no padding character is available, but does not take timing into account when using the padding character.

Neither limits the delay. no cap, they just let it run 😭

✨ keyname ✨

The keyname function may return the names of user-defined string capabilities that are defined in the terminfo entry via the -x option of tic. This implementation automatically assigns at run-time key codes to user-defined strings that begin with β€œk”. The key codes start at KEY_MAX, but are not guaranteed to be the same value for different runs because user-defined codes are merged from all terminal descriptions that have been loaded. The use_extended_names(3NCURSES) function controls whether this data is loaded when the terminal description is read by the library. iykyk πŸ’…

✨ nofilter, use_tioctl ✨

The nofilter and use_tioctl routines are specific to ncurses. They were not supported on Version 7, BSD or System V implementations. It is recommended that any code depending on ncurses extensions be conditioned using NCURSES_VERSION. guard those calls bestie 🫢

✨ putwin/getwin file-format ✨

The putwin and getwin functions have several issues with portability:

  • The files written and read by these functions use an implementation-specific format. Although the format is an obvious target for standardization, it has been overlooked.

Interestingly enough, according to the copyright dates in Solaris source, the functions (along with scr_init, etc.) originated with the University of California, Berkeley (in 1982) and were later (in 1988) incorporated into SVr4. Oddly, there are no such functions in the 4.3BSD curses sources.

  • Most implementations simply dump the binary WINDOW structure to the file. These include SVr4 curses, NetBSD curses, and PDCurses, as well as older ncurses versions. This implementation (as well as xcurses, the X/Open variant of Solaris curses, dated 1995) uses textual dumps.

The implementations that use binary dumps use block I/O (write(2) and read(2) functions). Those that use textual dumps use buffered I/O. A few applications may happen to write extra data in the file using these functions. Doing that can run into problems mixing block and buffered I/O. This implementation reduces the problem on writes by flushing the output. However, reading from a file written using mixed schemes may not be successful. so be careful fr fr 😭

✨ unctrl, wunctrl ✨

X/Open Curses Issue 4 describes these functions. It specifies no error conditions for them. It states that unctrl and wunctrl will return a null pointer if unsuccessful. This implementation checks for three cases:

  • the parameter is a 7-bit US-ASCII code. This is the case that X/Open Curses documented.
  • the parameter is in the range 128-159, i.e., a C1 control code. If use_legacy_coding(3NCURSES) has been called with a 2 parameter, unctrl returns the parameter, i.e., a one-character string with the parameter as the first character. Otherwise, it returns β€œ~@”, β€œ~A”, etc., analogous to β€œ^@”, β€œ^A”, C0 controls.

X/Open Curses does not document whether unctrl can be called before initializing curses. This implementation permits that, and returns the β€œ~@”, etc., values in that case.

  • parameter values outside the 0 to 255 range. unctrl returns a null pointer.

The strings returned by unctrl in this implementation are determined at compile time, showing C1 controls from the upper-128 codes with a β€œ~” prefix rather than β€œ^”. Other implementations have different conventions. For example, they may show both sets of control characters with β€œ^”, and strip the parameter to 7 bits. Or they may ignore C1 controls and treat all of the upper-128 codes as printable. This implementation uses 8 bits but does not modify the string to reflect locale. The use_legacy_coding(3NCURSES) function allows the caller to change the output of unctrl.

Likewise, the meta(3NCURSES) function allows the caller to change the output of keyname, i.e., it determines whether to use the β€œM-” prefix for β€œmeta” keys (codes in the range 128 to 255). Both use_legacy_coding(3NCURSES) and meta(3NCURSES) succeed only after curses is initialized. X/Open Curses does not document the treatment of codes 128 to 159. When treating them as β€œmeta” keys (or if keyname is called before initializing curses), this implementation returns strings β€œM-^@”, β€œM-^A”, etc.

X/Open Curses documents unctrl as declared in unctrl.h, which ncurses does. However, ncurses's curses.h includes unctrl.h, matching the behavior of SVr4 curses. Other implementations may not do that. sneaky include behavior πŸ‘€

✨ use_env, use_tioctl ✨

If ncurses is configured to provide the sp-functions extension, the state of use_env and use_tioctl may be updated before creating each screen rather than once only (sp_funcs(3NCURSES)). This feature of use_env is not provided by other implementations of curses. ncurses staying iconic as always πŸ’…


πŸ“œ HISTORY πŸ“œ

4BSD (1980) introduced unctrl, defining it as a macro in unctrl.h.

SVr2 (1984) added delay_output, flushinp, and keyname.

SVr3 (1987) supplied filter. Later that year, SVr3.1 brought getwin and putwin, reading and writing window dumps with fread(3) and fwrite(3), respectively.

SVr4 (1989) furnished use_env.

X/Open Curses Issue 4 (1995) specified key_name and wunctrl.

ncurses 5.6 (2006) added nofilter, and 6.0 (2015) added use_tioctl. the glow-up timeline bestie ✨


πŸ‘€ SEE ALSO πŸ‘€

  • ncurses(3NCURSES)
  • initscr(3NCURSES)
  • inopts(3NCURSES)
  • kernel(3NCURSES)
  • scr_dump(3NCURSES)
  • sp_funcs(3NCURSES)
  • curses_variables(3NCURSES)
  • legacy_coding(3NCURSES)

and that's the whole page, bestie!! this was ncurses 6.6, dated 2025-11-11 β€” util(3NCURSES) says bye and ily, go read the other sections too πŸ’–πŸ¦‹πŸŒΈπŸ’…βœ¨

πŸ“¦ where this page comes from