-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.c
60 lines (48 loc) · 1.01 KB
/
util.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
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
#include <stdio.h>
#include <unistd.h>
int file_write_uint(const char* const path, const unsigned int value) {
FILE* fp = fopen(path, "w");
if (fp == NULL) {
return -1;
}
int ret = fprintf(fp, "%u", value);
fclose(fp);
return ret;
}
int file_read_uint(const char* const path, unsigned int* const value) {
char buffer[256];
FILE* fp = fopen(path, "r");
if (fp == NULL) {
return -1;
}
char* ptr = fgets(buffer, 256, fp);
fclose(fp);
if (ptr == NULL) {
return -1;
}
int read = sscanf(buffer, "%u", value);
if (read == EOF) {
return -1;
}
return read;
}
int file_read_int(const char* const path, int* const value) {
char buffer[256];
FILE* fp = fopen(path, "r");
if (fp == NULL) {
return -1;
}
char* ptr = fgets(buffer, 256, fp);
fclose(fp);
if (ptr == NULL) {
return -1;
}
int read = sscanf(buffer, "%d", value);
if (read == EOF) {
return -1;
}
return read;
}
int file_exists(const char* const path) {
return access(path, F_OK);
}