xref: /original-bsd/lib/libcurses/printw.c (revision 3b4109a4)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)printw.c	5.7 (Berkeley) 02/09/91";
10 #endif /* not lint */
11 
12 /*
13  * printw and friends
14  *
15  */
16 
17 # include	<varargs.h>
18 # include	"curses.ext"
19 
20 /*
21  *	This routine implements a printf on the standard screen.
22  */
23 printw(va_alist)
24 va_dcl {
25 
26 	va_list	ap;
27 	int	ret;
28 
29 	va_start(ap);
30 	ret = _sprintw(stdscr, ap);
31 	va_end(ap);
32 	return (ret);
33 }
34 
35 /*
36  *	This routine implements a printf on the given window.
37  */
38 wprintw(va_alist)
39 va_dcl {
40 
41 	va_list	ap;
42 	WINDOW	*win;
43 	int	ret;
44 
45 	va_start(ap);
46 	win = va_arg(ap, WINDOW *);
47 	ret = _sprintw(win, ap);
48 	va_end(ap);
49 	return (ret);
50 }
51 
52 /*
53  *	Internal write-buffer-to-window function.
54  */
55 static int
56 _winwrite(cookie, buf, n)
57 void	*cookie;
58 reg char *buf;
59 int	n; {
60 
61 	reg WINDOW *win = (WINDOW *)cookie;
62 	reg int c = n;
63 
64 	while (--c >= 0) {
65 		if (waddch(win, *buf++) == ERR)
66 			return (-1);
67 	}
68 	return n;
69 }
70 
71 /*
72  *	This routine actually executes the printf and adds it to the window.
73  *	It must not be declared static as it is used in mvprintw.c.
74  */
75 _sprintw(win, ap)
76 WINDOW	*win;
77 va_list	ap; {
78 
79 	FILE	*f;
80 	char	*fmt;
81 
82 	if ((f = fwopen((void *)win, _winwrite)) == NULL)
83 		return ERR;
84 	fmt = va_arg(ap, char *);
85 	(void) vfprintf(f, fmt, ap);
86 	return fclose(f) ? ERR : OK;
87 }
88