1 /* $NetBSD: fmt.c,v 1.15 2001/03/20 19:05:11 itojun 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 while (len > nlen) 29 nlen *= 2; 30 nv = realloc(v, nlen); 31 if (nv == 0) 32 return; 33 v = nv; 34 maxlen = nlen; 35 } 36 strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); 37 if (*leftp != -1) { 38 len = strlen(v); 39 if (len > *leftp) { 40 v[*leftp] = '\0'; 41 *leftp = 0; 42 } else 43 *leftp -= len; 44 } 45 printf("%s", v); 46 } 47 48 void 49 fmt_putc(c, leftp) 50 int c; 51 int *leftp; 52 { 53 54 if (*leftp == 0) 55 return; 56 if (*leftp != -1) 57 *leftp -= 1; 58 putchar(c); 59 } 60