fmt.c revision 1.9 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.9 mycroft printf("%.*s", *leftp, v);
34 1.9 mycroft *leftp = 0;
35 1.9 mycroft } else {
36 1.9 mycroft printf("%s", v);
37 1.9 mycroft *leftp -= len;
38 1.9 mycroft }
39 1.9 mycroft } else
40 1.9 mycroft printf("%s", v);
41 1.1 cgd }
42 1.1 cgd
43 1.9 mycroft void
44 1.9 mycroft fmt_putc(c, leftp)
45 1.9 mycroft int c;
46 1.9 mycroft int *leftp;
47 1.1 cgd {
48 1.1 cgd
49 1.9 mycroft if (*leftp == 0)
50 1.9 mycroft return;
51 1.9 mycroft if (*leftp != -1)
52 1.9 mycroft *leftp -= 1;
53 1.9 mycroft putchar(c);
54 1.1 cgd }
55