xref: /netbsd/bin/ps/fmt.c (revision bf9ec67e)
1 /*	$NetBSD: fmt.c,v 1.18 2002/02/14 06:57:19 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;
19 	static int maxlen = 0;
20 	char *nv;
21 	int len, nlen;
22 
23 	if (*leftp == 0)
24 		return;
25 	len = strlen(s) * 4 + 1;
26 	if (len > maxlen) {
27 		if (maxlen == 0)
28 			nlen = getpagesize();
29 		else
30 			nlen = maxlen;
31 		while (len > nlen)
32 			nlen *= 2;
33 		nv = realloc(v, nlen);
34 		if (nv == 0)
35 			return;
36 		v = nv;
37 		maxlen = nlen;
38 	}
39 	len = strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
40 	if (*leftp != -1) {
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