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