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