Home | History | Annotate | Line # | Download | only in ps
fmt.c revision 1.20
      1  1.20    simonb /*	$NetBSD: fmt.c,v 1.20 2004/03/27 12:44:08 simonb 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.19    simonb fmt_puts(char *s, int *leftp)
     15   1.1       cgd {
     16  1.17     enami 	static char *v = 0;
     17   1.9   mycroft 	static int maxlen = 0;
     18  1.17     enami 	char *nv;
     19  1.15    itojun 	int len, nlen;
     20   1.1       cgd 
     21   1.9   mycroft 	if (*leftp == 0)
     22   1.9   mycroft 		return;
     23   1.9   mycroft 	len = strlen(s) * 4 + 1;
     24   1.9   mycroft 	if (len > maxlen) {
     25   1.9   mycroft 		if (maxlen == 0)
     26  1.15    itojun 			nlen = getpagesize();
     27  1.16     enami 		else
     28  1.16     enami 			nlen = maxlen;
     29  1.15    itojun 		while (len > nlen)
     30  1.15    itojun 			nlen *= 2;
     31  1.15    itojun 		nv = realloc(v, nlen);
     32   1.9   mycroft 		if (nv == 0)
     33   1.9   mycroft 			return;
     34   1.9   mycroft 		v = nv;
     35  1.15    itojun 		maxlen = nlen;
     36   1.1       cgd 	}
     37  1.18     enami 	len = strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
     38   1.9   mycroft 	if (*leftp != -1) {
     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.20    simonb 	(void)printf("%s", v);
     46   1.1       cgd }
     47   1.1       cgd 
     48   1.9   mycroft void
     49  1.19    simonb fmt_putc(int c, int *leftp)
     50   1.1       cgd {
     51   1.1       cgd 
     52   1.9   mycroft 	if (*leftp == 0)
     53   1.9   mycroft 		return;
     54   1.9   mycroft 	if (*leftp != -1)
     55   1.9   mycroft 		*leftp -= 1;
     56   1.9   mycroft 	putchar(c);
     57   1.1       cgd }
     58