1 1.18 enami /* $NetBSD: fmt.c,v 1.18 2002/02/14 06:57:19 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.17 enami static char *v = 0; 19 1.9 mycroft static int maxlen = 0; 20 1.17 enami char *nv; 21 1.15 itojun int len, nlen; 22 1.1 cgd 23 1.9 mycroft if (*leftp == 0) 24 1.9 mycroft return; 25 1.9 mycroft len = strlen(s) * 4 + 1; 26 1.9 mycroft if (len > maxlen) { 27 1.9 mycroft if (maxlen == 0) 28 1.15 itojun nlen = getpagesize(); 29 1.16 enami else 30 1.16 enami nlen = maxlen; 31 1.15 itojun while (len > nlen) 32 1.15 itojun nlen *= 2; 33 1.15 itojun nv = realloc(v, nlen); 34 1.9 mycroft if (nv == 0) 35 1.9 mycroft return; 36 1.9 mycroft v = nv; 37 1.15 itojun maxlen = nlen; 38 1.1 cgd } 39 1.18 enami len = strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE); 40 1.9 mycroft if (*leftp != -1) { 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