-
Notifications
You must be signed in to change notification settings - Fork 5
/
output.c
72 lines (60 loc) · 1.29 KB
/
output.c
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
/*
* Copyright 2022 zhenwei pi
*
* Authors:
* zhenwei pi <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "output.h"
#define OUTBUF_DEF_SIZE (1024 * 1024)
static void output_buf(struct output *op, char *buf, int size)
{
if (!op->ob.buf)
{
op->ob.size = OUTBUF_DEF_SIZE;
op->ob.buf = calloc(1, op->ob.size);
}
/* no enought buf, grow it */
if (op->ob.size - op->ob.offset < size)
{
op->ob.size *= 2;
op->ob.buf = realloc(op->ob.buf, op->ob.size);
}
memcpy(op->ob.buf + op->ob.offset, buf, size);
op->ob.offset += size;
}
void output_samp(struct output *op, char *buf, int size)
{
switch (op->output_type)
{
case OUTPUT_STDOUT:
printf("%s", buf);
break;
case OUTPUT_FD:
write(op->fd, buf, size);
break;
case OUTPUT_BUF:
output_buf(op, buf, size);
break;
default:
fprintf(stderr, "Error, unknown output type\n");
exit(EINVAL);
}
}
void output_samp_done(struct output *op, connection *conn)
{
if (op->done)
op->done(op, conn);
if (op->output_type == OUTPUT_BUF)
{
memset(op->ob.buf, 0x00, op->ob.offset);
op->ob.offset = 0;
}
}