-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathshared.c
35 lines (32 loc) · 815 Bytes
/
shared.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
#include "shared.h"
#include <stdlib.h>
// TODO: buffering for speed
void transfer_file(FILE* in, FILE* out, size_t bytes) {
char byte;
for(size_t i = 0; i < bytes; i++) {
if(!fread(&byte, 1, 1, in)) {
printf("Could not read all bytes from input!\n");
exit(1);
}
fwrite(&byte, 1, 1, out);
}
}
// returns 0 if file is empty or noexistant, filesize otherwise
size_t file_size(const char* path) {
FILE* f = fopen(path, "rb");
if(!f)
return 0;
fseek(f, 0L, SEEK_END);
size_t size = ftell(f);
fclose(f);
return size;
}
// platform agnostic and proves we can read a file too
int file_exists(const char *path) {
FILE *file;
if ((file = fopen(path, "rb"))) {
fclose(file);
return 1;
}
return 0;
}