summaryrefslogtreecommitdiff
path: root/bin/ps/fmt.c
blob: db64d2e18ba7603effe67ec586514b2d04434d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*	$OpenBSD: fmt.c,v 1.2 1996/06/23 14:20:49 deraadt Exp $	*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>

void
fmt_puts(s, leftp)
	char *s;
	int *leftp;
{
	static char *v = 0, *nv;
	static int maxlen = 0;
	int len;

	if (*leftp == 0)
		return;
	len = strlen(s) * 4 + 1;
	if (len > maxlen) {
		if (maxlen == 0)
			maxlen = getpagesize();
		while (len > maxlen)
			maxlen *= 2;
		nv = realloc(v, maxlen);
		if (nv == 0)
			return;
		v = nv;
	}
	strvis(v, s, VIS_TAB | VIS_NL | VIS_CSTYLE);
	if (*leftp != -1) {
		len = strlen(v);
		if (len > *leftp) {
			v[*leftp] = '\0';
			*leftp = 0;
		} else
			*leftp -= len;
	}
	printf("%s", v);
}

void
fmt_putc(c, leftp)
	int c;
	int *leftp;
{

	if (*leftp == 0)
		return;
	if (*leftp != -1)
		*leftp -= 1;
	putchar(c);
}