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