Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / bin / ps / fmt.c
blobcaa59147ff2618b3fffc9f594e8cf42a0c7e2b45
1 /* $NetBSD: fmt.c,v 1.20 2004/03/27 12:44:08 simonb Exp $ */
3 #include <sys/cdefs.h>
4 __RCSID("$NetBSD$");
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"
16 void
17 fmt_puts(char *s, int *leftp)
19 static char *v = 0;
20 static int maxlen = 0;
21 char *nv;
22 int len, nlen;
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;
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;
48 (void)printf("%s", v);
51 void
52 fmt_putc(int c, int *leftp)
55 if (*leftp == 0)
56 return;
57 if (*leftp != -1)
58 *leftp -= 1;
59 putchar(c);