-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
69 lines (61 loc) · 1.06 KB
/
reader.c
File metadata and controls
69 lines (61 loc) · 1.06 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
65
66
67
68
69
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "json_types.h"
#include "json_parser.h"
#include "json_debug.h"
# define BUFF_SIZE 4096
char *strjoin(char const *s1, char const *s2)
{
char *ret;
ret = NULL;
if (s1 && s2)
{
if (!(ret = (char*)malloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1))))
return (NULL);
strcpy(ret, s1);
strcat(ret, s2);
}
return (ret);
}
char *read_file(int fd)
{
int ret;
char buf[BUFF_SIZE + 1];
char *str;
char *tmp;
str = NULL;
while ((ret = read(fd, buf, BUFF_SIZE)))
{
buf[ret] = '\0';
tmp = NULL;
if (str)
{
tmp = strdup(str);
free(str);
str = strjoin(tmp, buf);
free(tmp);
}
else
str = strdup(buf);
}
if (ret == -1)
free(str);
return (str);
}
int main(int ac, char **av)
{
int fd;
char *full_file;
if ((fd = open(av[1], O_RDONLY)) == -1)
return (-1);
full_file = read_file(fd);
//printf("\n\n%s\n", full_file);
json_print_value(json_parse(full_file));
free(full_file);
if (close(fd) == -1)
return (-1);
return (0);
}