-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
233 lines (202 loc) · 6.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This file is part of clipsim.
* Copyright (C) 2024 Lucas Mior
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clipsim.h"
#include <stdarg.h>
static char *notifiers[2] = { "dunstify", "notify-send" };
void *
util_malloc(const usize size) {
void *p;
if ((p = malloc(size)) == NULL) {
error("Error allocating %zu bytes.\n", size);
exit(EXIT_FAILURE);
}
return p;
}
void *
util_memdup(const void *source, const usize size) {
void *p;
if ((p = malloc(size)) == NULL) {
error("Error allocating %zu bytes.\n", size);
exit(EXIT_FAILURE);
}
memcpy(p, source, size);
return p;
}
char *
util_strdup(const char *string) {
char *p = strdup(string);
if (p == NULL) {
error("Error duplicating string \"%s\".\n", string);
exit(EXIT_FAILURE);
}
return p;
}
void *
util_realloc(void *old, const usize size) {
void *p;
if ((p = realloc(old, size)) == NULL) {
error("Error reallocating %zu bytes.\n", size);
error("Reallocating from: %p\n", old);
exit(EXIT_FAILURE);
}
return p;
}
void *
util_calloc(const usize nmemb, const usize size) {
void *p;
if ((p = calloc(nmemb, size)) == NULL) {
error("Error allocating %zu members of %zu bytes each.\n",
nmemb, size);
exit(EXIT_FAILURE);
}
return p;
}
int32
util_string_int32(int32 *number, const char *string) {
char *endptr;
long x;
errno = 0;
x = strtol(string, &endptr, 10);
if ((errno != 0) || (string == endptr) || (*endptr != 0)) {
return -1;
} else if ((x > INT32_MAX) || (x < INT32_MIN)) {
return -1;
} else {
*number = (int32) x;
return 0;
}
}
void
util_die_notify(const char *format, ...) {
int32 n;
va_list args;
char buffer[BUFSIZ];
va_start(args, format);
n = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
va_end(args);
if (n < 0)
exit(EXIT_FAILURE);
buffer[n] = '\0';
(void) write(STDERR_FILENO, buffer, (usize) n + 1);
for (uint i = 0; i < LENGTH(notifiers); i += 1) {
execlp(notifiers[i], notifiers[i], "-u", "critical",
"clipsim", buffer, NULL);
}
exit(EXIT_FAILURE);
}
void
util_segv_handler(int32 unused) {
(void) unused;
char *message = "Memory error. Please send a bug report.\n";
(void) write(STDERR_FILENO, message, strlen(message));
for (uint i = 0; i < LENGTH(notifiers); i += 1) {
execlp(notifiers[i], notifiers[i], "-u", "critical",
"clipsim", message, NULL);
}
_exit(EXIT_FAILURE);
}
void
util_close(File *file) {
if (file->fd >= 0) {
if (close(file->fd) < 0)
error("Error closing %s: %s\n", file->name, strerror(errno));
file->fd = -1;
}
if (file->file != NULL) {
if (fclose(file->file) != 0)
error("Error closing %s: %s\n", file->name, strerror(errno));
file->file = NULL;
}
return;
}
int32
util_open(File *file, const int32 flag) {
if ((file->fd = open(file->name, flag)) < 0) {
error("Error opening %s: %s\n", file->name, strerror(errno));
return -1;
} else {
return 0;
}
}
int32
util_copy_file(const char *destination, const char *source) {
int32 source_fd;
int32 destination_fd;
char buffer[BUFSIZ];
isize r = 0;
isize w = 0;
if ((source_fd = open(source, O_RDONLY)) < 0) {
error("Error opening %s for reading: %s.\n", source, strerror(errno));
return -1;
}
if ((destination_fd = open(destination, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR)) < 0) {
error("Error opening %s for writing: %s.\n",
destination, strerror(errno));
close(source_fd);
return -1;
}
errno = 0;
while ((r = read(source_fd, buffer, BUFSIZ)) > 0) {
w = write(destination_fd, buffer, (usize) r);
if (w != r) {
fprintf(stderr, "Error writing data to %s", destination);
if (errno)
fprintf(stderr, ": %s", strerror(errno));
fprintf(stderr, ".\n");
close(source_fd);
close(destination_fd);
return -1;
}
}
if (r < 0) {
error("Error reading data from %s: %s.\n", source, strerror(errno));
close(source_fd);
close(destination_fd);
return -1;
}
close(source_fd);
close(destination_fd);
return 0;
}
void error(char *format, ...) {
char buffer[BUFSIZ];
va_list args;
int32 n;
va_start(args, format);
n = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
va_end(args);
if (n < 0 || n > (int32)sizeof(buffer)) {
fprintf(stderr, "Error in vsnprintf()\n");
exit(EXIT_FAILURE);
}
buffer[n] = '\0';
write(STDERR_FILENO, buffer, (usize) n);
#ifdef CLIPSIM_DEBUG
switch (fork()) {
case -1:
fprintf(stderr, "Error forking: %s\n", strerror(errno));
break;
case 0:
for (uint i = 0; i < LENGTH(notifiers); i += 1) {
execlp(notifiers[i], notifiers[i], "-u", "critical",
program, buffer, NULL);
fprintf(stderr, "Error trying to exec %s.\n", notifiers[i]);
}
exit(EXIT_FAILURE);
default:
break;
}
#endif
}