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