1 /* $NetBSD: fmt.c,v 1.17 2002/02/14 06:54:41 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 strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); 40 if (*leftp != -1) { 41 len = strlen(v); 42 if (len > *leftp) { 43 v[*leftp] = '\0'; 44 *leftp = 0; 45 } else 46 *leftp -= len; 47 } 48 printf("%s", v); 49 } 50 51 void 52 fmt_putc(c, leftp) 53 int c; 54 int *leftp; 55 { 56 57 if (*leftp == 0) 58 return; 59 if (*leftp != -1) 60 *leftp -= 1; 61 putchar(c); 62 } 63