xref: /netbsd-src/bin/ps/fmt.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: fmt.c,v 1.16 2001/03/23 01:06:02 enami 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(s, leftp)
15 	char *s;
16 	int *leftp;
17 {
18 	static char *v = 0, *nv;
19 	static int maxlen = 0;
20 	int len, nlen;
21 
22 	if (*leftp == 0)
23 		return;
24 	len = strlen(s) * 4 + 1;
25 	if (len > maxlen) {
26 		if (maxlen == 0)
27 			nlen = getpagesize();
28 		else
29 			nlen = maxlen;
30 		while (len > nlen)
31 			nlen *= 2;
32 		nv = realloc(v, nlen);
33 		if (nv == 0)
34 			return;
35 		v = nv;
36 		maxlen = nlen;
37 	}
38 	strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
39 	if (*leftp != -1) {
40 		len = strlen(v);
41 		if (len > *leftp) {
42 			v[*leftp] = '\0';
43 			*leftp = 0;
44 		} else
45 			*leftp -= len;
46 	}
47 	printf("%s", v);
48 }
49 
50 void
51 fmt_putc(c, leftp)
52 	int c;
53 	int *leftp;
54 {
55 
56 	if (*leftp == 0)
57 		return;
58 	if (*leftp != -1)
59 		*leftp -= 1;
60 	putchar(c);
61 }
62