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