aboutsummaryrefslogtreecommitdiffstats
path: root/package/webif/src/bstrip.c
blob: 1e17d4e00a9032f9e1ce3d3c6dec6e23c26b2919 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 1024
#define READ_LEN 14

static int read_len = READ_LEN;
static char rbuf[32];
static char rbuflen = 0;

int do_gets(char *buf)
{
	int r = 0, c = 0;
	char *m;

	if (rbuflen > 0)
		memcpy(buf, rbuf, rbuflen);
	c += rbuflen;

	while ((c + read_len < BUF_SIZE) && ((r = read(0, &buf[c], read_len)) > 0)) {
		m = NULL;

		if ((m = memchr(&buf[c], '\n', r)) != NULL) {
			rbuflen = r - (m - &buf[c] + 1);
			if (rbuflen > 0)
				memcpy(rbuf, m + 1, rbuflen);
			c += m - &buf[c] + 1;
		} else {
			rbuflen = 0;
			c += r;
		}

		if ((c > 3) && (memcmp(&buf[c - 3], "---", 3) == 0))
			read_len = 1;

		if (m != NULL)
			return c;
	}

	return c;
}

int main(int argc, char **argv)
{
	char buf[BUF_SIZE];
	char buf1[BUF_SIZE];
	char *tmp;
	int len, r = 0, r1 = 0;

	if (argc != 2) {
		fprintf(stderr, "Syntax: %s (name|data <boundary>)\n", argv[0]);
		exit(1);
	}
	while (tmp = strchr(argv[1], '\r'))
		*tmp = 0;

	len = strlen(argv[1]);

	*buf = 0;
	while ((strncmp(buf, argv[1], len) != 0) &&
		(strncmp(buf + 2, argv[1], len) != 0)) {
		if (r > 0) {
			if (r1 > 0)
				write (1, buf1, r1);
			r1 = r;
			memcpy(buf1, buf, r);
		}
		if ((r = do_gets(buf)) <= 0)
			exit(1);
	}

	if (r1 > 2)
		write(1, buf1, r1 - 2);
}