-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
110 lines (89 loc) · 2.46 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
/* This file is part of dwmblocks2.
* 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/>.
*/
#ifndef UTIL_C
#define UTIL_C
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static char *
itoa(long num, char *str) {
int i = 0;
bool negative = false;
if (num < 0) {
negative = true;
num = -num;
}
do {
str[i++] = num % 10 + '0';
num /= 10;
} while (num > 0);
if (negative)
str[i++] = '-';
str[i] = '\0';
for (int j = 0; j < i / 2; j++) {
char temp = str[j];
str[j] = str[i - j - 1];
str[i - j - 1] = temp;
}
return str;
}
static void
error(char *format, ...) {
int 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) {
fprintf(stderr, "Error in vsnprintf()\n");
exit(EXIT_FAILURE);
}
buffer[n] = '\0';
write(STDERR_FILENO, buffer, (size_t) n);
#ifdef DEBUGGING
switch (fork()) {
char *notifiers[2] = { "dunstify", "notify-send" };
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 dunstify.\n");
break;
default:
break;
}
exit(EXIT_FAILURE);
#endif
}
#ifndef TESTING_THIS_FILE
#define TESTING_THIS_FILE 0
#endif
#if TESTING_THIS_FILE
#include <assert.h>
int
main(void) {
assert(true);
exit(EXIT_SUCCESS);
}
#endif
#endif