1 1.16 enami /* $NetBSD: fmt.c,v 1.16 2001/03/23 01:06:02 enami Exp $ */ 2 1.12 perry 3 1.14 simonb #include <kvm.h> 4 1.1 cgd #include <stdio.h> 5 1.1 cgd #include <stdlib.h> 6 1.1 cgd #include <string.h> 7 1.9 mycroft #include <unistd.h> 8 1.1 cgd #include <vis.h> 9 1.13 perry #include <sys/time.h> 10 1.11 christos #include <sys/resource.h> 11 1.11 christos #include "ps.h" 12 1.1 cgd 13 1.9 mycroft void 14 1.9 mycroft fmt_puts(s, leftp) 15 1.9 mycroft char *s; 16 1.9 mycroft int *leftp; 17 1.1 cgd { 18 1.9 mycroft static char *v = 0, *nv; 19 1.9 mycroft static int maxlen = 0; 20 1.15 itojun int len, nlen; 21 1.1 cgd 22 1.9 mycroft if (*leftp == 0) 23 1.9 mycroft return; 24 1.9 mycroft len = strlen(s) * 4 + 1; 25 1.9 mycroft if (len > maxlen) { 26 1.9 mycroft if (maxlen == 0) 27 1.15 itojun nlen = getpagesize(); 28 1.16 enami else 29 1.16 enami nlen = maxlen; 30 1.15 itojun while (len > nlen) 31 1.15 itojun nlen *= 2; 32 1.15 itojun nv = realloc(v, nlen); 33 1.9 mycroft if (nv == 0) 34 1.9 mycroft return; 35 1.9 mycroft v = nv; 36 1.15 itojun maxlen = nlen; 37 1.1 cgd } 38 1.9 mycroft strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); 39 1.9 mycroft if (*leftp != -1) { 40 1.9 mycroft len = strlen(v); 41 1.9 mycroft if (len > *leftp) { 42 1.10 mycroft v[*leftp] = '\0'; 43 1.9 mycroft *leftp = 0; 44 1.10 mycroft } else 45 1.9 mycroft *leftp -= len; 46 1.10 mycroft } 47 1.10 mycroft printf("%s", v); 48 1.1 cgd } 49 1.1 cgd 50 1.9 mycroft void 51 1.9 mycroft fmt_putc(c, leftp) 52 1.9 mycroft int c; 53 1.9 mycroft int *leftp; 54 1.1 cgd { 55 1.1 cgd 56 1.9 mycroft if (*leftp == 0) 57 1.9 mycroft return; 58 1.9 mycroft if (*leftp != -1) 59 1.9 mycroft *leftp -= 1; 60 1.9 mycroft putchar(c); 61 1.1 cgd } 62