-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathf5ar_utils.c
More file actions
49 lines (39 loc) · 1.08 KB
/
Copy pathf5ar_utils.c
File metadata and controls
49 lines (39 loc) · 1.08 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* file_read(const char *path, size_t *size) {
FILE *infile = fopen(path, "rb");
if (!infile)
return NULL;
fseek(infile, 0L, SEEK_END),
*size += ftell(infile),
fseek(infile, 0L, SEEK_SET);
char *buffer = malloc(*size);
if (buffer == NULL)
return NULL;
const size_t expected = *size;
if (fread(buffer, 1, expected, infile) != expected)
free(buffer), buffer = NULL;
fclose(infile);
return buffer;
}
int file_write(const char *path, const char *data, size_t size) {
FILE *infile = fopen(path, "wb");
if (!infile)
return -1;
const size_t wrote = fwrite(data, 1, size, infile);
fclose(infile);
if (wrote != size)
return -2;
return 0;
}
void inline extract_dir_path(char* dest, const char* src) {
unsigned up_to = 0;
for (unsigned j = 0; j < FILENAME_MAX && src[j]; ++j)
if (src[j] == '/')
up_to = j + 1;
if (up_to > 0)
strncpy(dest, src, up_to);
else
dest[0] = '.';
}