xref: /netbsd-src/bin/ps/fmt.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <vis.h>
6 
7 void
8 fmt_puts(s, leftp)
9 	char *s;
10 	int *leftp;
11 {
12 	static char *v = 0, *nv;
13 	static int maxlen = 0;
14 	int len;
15 
16 	if (*leftp == 0)
17 		return;
18 	len = strlen(s) * 4 + 1;
19 	if (len > maxlen) {
20 		if (maxlen == 0)
21 			maxlen = getpagesize();
22 		while (len > maxlen)
23 			maxlen *= 2;
24 		nv = realloc(v, maxlen);
25 		if (nv == 0)
26 			return;
27 		v = nv;
28 	}
29 	strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
30 	if (*leftp != -1) {
31 		len = strlen(v);
32 		if (len > *leftp) {
33 			v[*leftp] = '\0';
34 			*leftp = 0;
35 		} else
36 			*leftp -= len;
37 	}
38 	printf("%s", v);
39 }
40 
41 void
42 fmt_putc(c, leftp)
43 	int c;
44 	int *leftp;
45 {
46 
47 	if (*leftp == 0)
48 		return;
49 	if (*leftp != -1)
50 		*leftp -= 1;
51 	putchar(c);
52 }
53