Home | History | Annotate | Line # | Download | only in ps
fmt.c revision 1.12
      1  1.12     perry /*	$NetBSD: fmt.c,v 1.12 1998/01/09 07:00:57 perry Exp $	*/
      2  1.12     perry 
      3   1.1       cgd #include <stdio.h>
      4   1.1       cgd #include <stdlib.h>
      5   1.1       cgd #include <string.h>
      6   1.9   mycroft #include <unistd.h>
      7   1.1       cgd #include <vis.h>
      8  1.11  christos #include <sys/resource.h>
      9  1.11  christos #include "ps.h"
     10  1.11  christos #include "extern.h"
     11   1.1       cgd 
     12   1.9   mycroft void
     13   1.9   mycroft fmt_puts(s, leftp)
     14   1.9   mycroft 	char *s;
     15   1.9   mycroft 	int *leftp;
     16   1.1       cgd {
     17   1.9   mycroft 	static char *v = 0, *nv;
     18   1.9   mycroft 	static int maxlen = 0;
     19   1.9   mycroft 	int len;
     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.9   mycroft 			maxlen = getpagesize();
     27   1.9   mycroft 		while (len > maxlen)
     28   1.9   mycroft 			maxlen *= 2;
     29   1.9   mycroft 		nv = realloc(v, maxlen);
     30   1.9   mycroft 		if (nv == 0)
     31   1.9   mycroft 			return;
     32   1.9   mycroft 		v = nv;
     33   1.1       cgd 	}
     34   1.9   mycroft 	strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
     35   1.9   mycroft 	if (*leftp != -1) {
     36   1.9   mycroft 		len = strlen(v);
     37   1.9   mycroft 		if (len > *leftp) {
     38  1.10   mycroft 			v[*leftp] = '\0';
     39   1.9   mycroft 			*leftp = 0;
     40  1.10   mycroft 		} else
     41   1.9   mycroft 			*leftp -= len;
     42  1.10   mycroft 	}
     43  1.10   mycroft 	printf("%s", v);
     44   1.1       cgd }
     45   1.1       cgd 
     46   1.9   mycroft void
     47   1.9   mycroft fmt_putc(c, leftp)
     48   1.9   mycroft 	int c;
     49   1.9   mycroft 	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