-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.c
More file actions
64 lines (53 loc) · 1 KB
/
read.c
File metadata and controls
64 lines (53 loc) · 1 KB
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
// SPDX-License-Identifier: MIT
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "write.h"
#include "read.h"
static bool search_string(char *haystack, size_t hn, char *needle, size_t nn)
{
if(nn > hn)
return false;
if(strstr(haystack, needle) == NULL)
return false;
return true;
}
int read_stdin(struct uartlog_options *opts)
{
char *buffer = calloc(opts->buffsize, sizeof(char));
size_t i = 0;
int c;
int rc = 0;
struct write_context ctx = {0};
ctx.fmt = opts->filefmt;
ctx.link = opts->link;
while((c = fgetc(stdin)) != EOF)
{
buffer[i] = (c & 0xff);
if(buffer[i] == '\n')
{
ctx.renew = search_string(buffer, i + 1, opts->needle, opts->nn);
dump_line(&ctx, buffer, i + 1);
i = 0;
memset(buffer, '\0', opts->buffsize);
}
else
{
i++;
if(i >= opts->buffsize)
{
rc = UARTLOG_EOVER_BUFFER;
goto end;
}
}
}
/* dump remain buffer. */
if(i != 0)
{
buffer[i] = '\n';
dump_line(&ctx, buffer, i + 1);
}
end:
free(buffer);
return 0;
}