Home | History | Annotate | Line # | Download | only in ps
fmt.c revision 1.11
      1   1.1       cgd #include <stdio.h>
      2   1.1       cgd #include <stdlib.h>
      3   1.1       cgd #include <string.h>
      4   1.9   mycroft #include <unistd.h>
      5   1.1       cgd #include <vis.h>
      6  1.11  christos #include <sys/resource.h>
      7  1.11  christos #include "ps.h"
      8  1.11  christos #include "extern.h"
      9   1.1       cgd 
     10   1.9   mycroft void
     11   1.9   mycroft fmt_puts(s, leftp)
     12   1.9   mycroft 	char *s;
     13   1.9   mycroft 	int *leftp;
     14   1.1       cgd {
     15   1.9   mycroft 	static char *v = 0, *nv;
     16   1.9   mycroft 	static int maxlen = 0;
     17   1.9   mycroft 	int len;
     18   1.1       cgd 
     19   1.9   mycroft 	if (*leftp == 0)
     20   1.9   mycroft 		return;
     21   1.9   mycroft 	len = strlen(s) * 4 + 1;
     22   1.9   mycroft 	if (len > maxlen) {
     23   1.9   mycroft 		if (maxlen == 0)
     24   1.9   mycroft 			maxlen = getpagesize();
     25   1.9   mycroft 		while (len > maxlen)
     26   1.9   mycroft 			maxlen *= 2;
     27   1.9   mycroft 		nv = realloc(v, maxlen);
     28   1.9   mycroft 		if (nv == 0)
     29   1.9   mycroft 			return;
     30   1.9   mycroft 		v = nv;
     31   1.1       cgd 	}
     32   1.9   mycroft 	strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
     33   1.9   mycroft 	if (*leftp != -1) {
     34   1.9   mycroft 		len = strlen(v);
     35   1.9   mycroft 		if (len > *leftp) {
     36  1.10   mycroft 			v[*leftp] = '\0';
     37   1.9   mycroft 			*leftp = 0;
     38  1.10   mycroft 		} else
     39   1.9   mycroft 			*leftp -= len;
     40  1.10   mycroft 	}
     41  1.10   mycroft 	printf("%s", v);
     42   1.1       cgd }
     43   1.1       cgd 
     44   1.9   mycroft void
     45   1.9   mycroft fmt_putc(c, leftp)
     46   1.9   mycroft 	int c;
     47   1.9   mycroft 	int *leftp;
     48   1.1       cgd {
     49   1.1       cgd 
     50   1.9   mycroft 	if (*leftp == 0)
     51   1.9   mycroft 		return;
     52   1.9   mycroft 	if (*leftp != -1)
     53   1.9   mycroft 		*leftp -= 1;
     54   1.9   mycroft 	putchar(c);
     55   1.1       cgd }
     56