-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
150 lines (129 loc) · 3.54 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "igzip_wrapper.h"
#ifdef __cplusplus
extern "C" {
#endif
void log_print(int log_type, char *format, ...) {
va_list args;
va_start(args, format);
switch (log_type) {
case INFORM:
vfprintf(stdout, format, args);
break;
case WARN:
if ((_IGZIP_VERBOSE_LEVEL) > 1)
vfprintf(stderr, format, args);
break;
case ERROR:
if ((_IGZIP_VERBOSE_LEVEL) > 0)
vfprintf(stderr, format, args);
break;
case VERBOSE:
if ((_IGZIP_VERBOSE_LEVEL) > 2)
vfprintf(stderr, format, args);
break;
}
va_end(args);
}
void *malloc_safe(size_t size) {
void *ptr = NULL;
if (size == 0)
return ptr;
ptr = malloc(size);
if (ptr == NULL) {
log_print(ERROR, "igzip: Failed to allocate memory\n");
exit(MALLOC_FAILED);
}
return ptr;
}
FILE *fopen_safe(const char *file_name, char *mode) {
FILE *file;
int answer = 0, tmp;
/* Assumes write mode always starts with w */
if (mode[0] == 'w') {
if (access(file_name, F_OK) == 0) {
log_print(WARN, "igzip: %s already exists;", file_name);
if (_IGZIP_IS_INTERACTIVE) {
log_print(WARN, " do you wish to overwrite (y/n)?");
answer = getchar();
tmp = answer;
while (tmp != '\n' && tmp != EOF)
tmp = getchar();
if (answer != 'y' && answer != 'Y') {
log_print(WARN, " not overwritten\n");
return NULL;
}
} else if (!(_IGZIP_FILE_FORCE_OVERRITTEN)) {
log_print(WARN, " not overwritten\n");
return NULL;
}
if (access(file_name, W_OK) != 0) {
log_print(ERROR, "igzip: %s: Permission denied\n", file_name);
return NULL;
}
}
}
/* Assumes read mode always starts with r */
if (mode[0] == 'r') {
if (access(file_name, F_OK) != 0) {
log_print(ERROR, "igzip: %s does not exist\n", file_name);
return NULL;
}
if (access(file_name, R_OK) != 0) {
log_print(ERROR, "igzip: %s: Permission denied\n", file_name);
return NULL;
}
}
file = fopen(file_name, mode);
if (!file) {
log_print(ERROR, "igzip: Failed to open %s\n", file_name);
return NULL;
}
return file;
}
void open_in_file(FILE **in, const char *infile_name) {
*in = NULL;
if (infile_name == NULL)
*in = stdin;
else
*in = fopen_safe(infile_name, "rb");
}
void open_out_file(FILE **out, const char *outfile_name) {
*out = NULL;
// Pass NULL outfile name to use stdout for compression output
if (outfile_name == NULL)
*out = stdout;
else if (outfile_name != NULL)
*out = fopen_safe(outfile_name, "wb");
else if (!isatty(fileno(stdout)) || (_IGZIP_FILE_FORCE_OVERRITTEN))
*out = stdout;
else {
log_print(WARN,
"igzip: No output location. Use -c to output to terminal\n");
exit(FILE_OPEN_ERROR);
}
}
size_t fread_safe(void *buf, size_t word_size, size_t buf_size, FILE *in,
const char *file_name) {
size_t read_size;
read_size = fread(buf, word_size, buf_size, in);
if (ferror(in)) {
log_print(ERROR, "igzip: Error encountered while reading file %s\n",
file_name);
exit(FILE_READ_ERROR);
}
return read_size;
}
size_t fwrite_safe(void *buf, size_t word_size, size_t buf_size, FILE *out,
const char *file_name) {
size_t write_size;
write_size = fwrite(buf, word_size, buf_size, out);
if (ferror(out)) {
log_print(ERROR, "igzip: Error encountered while writing to file %s\n",
file_name);
exit(FILE_WRITE_ERROR);
}
return write_size;
}
#ifdef __cplusplus
} // extern "C"
#endif