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