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