This repository was archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
85 lines (70 loc) · 2.32 KB
/
utils.c
File metadata and controls
85 lines (70 loc) · 2.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "utils.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void* realloc_safe(void** ptr, size_t new_size, const char* func, int line) {
if ((*ptr = realloc(*ptr, new_size)) == NULL) {
fprintf(stderr, "%s:%d: Could not allocate %zu bytes of memory\n", func,
line, new_size);
exit(ENOMEM);
}
LOG("func=%s line=%d allocated=%zu\n", func, line, new_size);
return *ptr;
}
void read_stdin(char** content, size_t* content_len) {
char buf[BUF_LEN] = "";
ssize_t effectivily_read = 0;
while ((effectivily_read = read(0, buf, BUF_LEN)) > 0) {
*content_len += (size_t)effectivily_read;
REALLOC_SAFE(content, *content_len);
memcpy(*content + *content_len - effectivily_read, buf,
(size_t)effectivily_read);
}
if (effectivily_read == -1) {
fprintf(stderr, "Error reading from stdin: errno=%s error=%d\n",
strerror(errno), errno);
exit(errno);
}
LOG("content_len=%zu\n", *content_len);
}
void read_file(const char path[], char** content, size_t* content_len) {
FILE* file = NULL;
if ((file = fopen(path, "r")) == NULL) {
fprintf(stderr, "Could not open the file `%s`: errno=%d error=%s\n",
path, errno, strerror(errno));
exit(errno);
}
int ret = 0;
if ((ret = fseek(file, 0, SEEK_END)) != 0) {
fprintf(stderr,
"Could not move the file cursor to the end of the file `%s`: "
"errno=%d error=%s\n",
path, errno, strerror(errno));
exit(errno);
}
const size_t file_size = (size_t)ftell(file);
rewind(file);
REALLOC_SAFE(content, file_size + 1);
(*content)[file_size] = '\0';
const size_t bytes_read = fread(*content, 1, file_size, file);
if (bytes_read != file_size) {
fprintf(stderr,
"Could not read whole file: bytes_read=%zu file_size=%zu\n",
bytes_read, file_size);
exit(EIO);
}
*content_len = bytes_read;
fclose(file);
}
bool str_eq(const char* restrict a, size_t a_len, const char* restrict b,
size_t b_len) {
if (!a || !b) {
return false;
}
if (a_len != b_len) {
return false;
}
return memcmp(a, b, a_len) == 0;
}