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