~q3cpma/misc-tools

ref: 313cd570e4b81c5f6749356698ce617c65181b61 misc-tools/misc.c -rw-r--r-- 4.1 KiB
313cd570q3cpma README nit 10 months ago
                                                                                
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
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "misc.h"


void die(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	fputc('\n', stderr);
	va_end(args);
	exit(EXIT_FAILURE);
}

ssize_t xgetdelim(char **lineptr, size_t *n, int delim, FILE *stream)
{
	errno = 0;
	ssize_t read = getdelim(lineptr, n, delim, stream);

	if (read != -1)
	{
		if ((*lineptr)[read - 1] == delim)
		{
			(*lineptr)[--read] = '\0';
		}
	}
	else if (errno)
	{
		die("getdelim: %s", strerror(errno));
	}
	return read;
}

void signals_nointerrupt(void)
{
	static int sigs[] = {SIGCHLD, SIGCONT, SIGTSTP, SIGURG, SIGWINCH};
	struct sigaction cur;

	for (size_t i = 0; i < sizeof(sigs) / sizeof(sigs[0]); ++i)
	{
		sigaction(sigs[i], NULL, &cur);
		cur.sa_flags |= SA_RESTART;
		if (sigaction(sigs[i], &cur, NULL))
		{
			die("signal %d: %s", sigs[i], strerror(errno));
		}
	}
}

uint32_t log2_u32(uint32_t x)
{
#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
	static const uint8_t log2lut[256] =
	{
		0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
		LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
		LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
	};
#undef LT
	const uint32_t mask[] =  {0xffff0000, 0xff00};
	const uint32_t shift[] = {16,         8};

	uint32_t log = 0;
	for (uint8_t i = 0; i < 2; ++i)
	{
		if (x & mask[i])
		{
			x >>= shift[i];
			log |= shift[i];
		}
	}
	return log + log2lut[x];
}

void fprintf_cstrlit(FILE *stream, const char *str, size_t len)
{
	static char buf[BUFSIZ];
	size_t bufcapa = BUFSIZ;
	char *p = buf;
	bool utf8 = false;

	for (size_t i = 0; i < len; ++i)
	{
		if (iscntrl(str[i]) || str[i] == '"' || str[i] == '\\')
		{
			// snprintf can only fail if n > INT_MAX (cf POSIX)
			size_t ret = snprintf(p, bufcapa, "\\x%02x", (uint8_t)str[i]);
			p += ret;
			bufcapa -= ret;
		}
		else
		{
			if (str[i] > '~')
			{
				utf8 = true;
			}
			*p++ = str[i];
			--bufcapa;
		}
	}
	*p = '\0';
	fprintf(stream, "%s\"%s\"", utf8 ? "u8" : "", buf);
}

unsigned long xstrtoul(const char *str, int base)
{
	char *end;
	unsigned long ret = strtoul(str, &end, base);
	if (*str == '\0' || *end != '\0' || (errno == ERANGE && ret == ULONG_MAX))
	{
		die("%s: invalid base%d unsigned long number", str, base);
	}
	return ret;
}

int pasprintf(char **s, const char *fmt, ...)
{
	va_list args[2];
	va_start(args[0], fmt);
	va_copy(args[1], args[0]);
	size_t len = vsnprintf(NULL, 0, fmt, args[0]) + 1;
	*s = xmalloc(len);
	int ret = vsprintf(*s, fmt, args[1]);
	va_end(args[0]);
	va_end(args[1]);
	return ret;
}

float xstrtof(const char *str)
{
    if (*str == '\0')
	{
        die("empty string argument");
	}
    char *end;
    const float ret = strtof(str, &end);
    if (*end != '\0')
	{
        die("%s: trailing text found during conversion to float", str);
	}
	if (errno == EINVAL)
	{
		die("%s: %s", str, strerror(errno));
	}
    if (errno == ERANGE)
    {
        die("%s: float conversion %s", str,
            ret == HUGE_VAL || ret == -HUGE_VAL ? "overflow" : "underflow");
    }
    return ret;
}

FILE * xfopen(const char *path, const char *mode)
{
	FILE *f = fopen(path, mode);
	if (f == NULL)
	{
		die("fopen(%s, %s): %s", path, mode, strerror(errno));
	}
	return f;
}

void xfclose(FILE *stream)
{
	if (fclose(stream) == EOF)
	{
		die("fclose: %s", strerror(errno));
	}
}

void * xmalloc(size_t size)
{
    void *p = malloc(size);
	if (p == NULL)
	{
		die("malloc(%zu): %s", size, strerror(errno));
	}
    return p;
}

void * xcalloc(size_t nmemb, size_t size)
{
    void *p = calloc(nmemb, size);
	if (p == NULL)
	{
		die("calloc(%zu, %zu): %s", nmemb, size, strerror(errno));
	}
    return p;
}

void * xrealloc(void *ptr, size_t size)
{
	void *p = realloc(ptr, size);
	if (p == NULL)
	{
		die("realloc(%p, %zu): %s", ptr, size, strerror(errno));
	}
    return p;
}

void * xreallocarray(void *ptr, size_t nmemb, size_t size)
{
	if (nmemb > SIZE_MAX / size)
	{
		die("reallocarray(%p, %zu, %zu): overflow detected", ptr, nmemb, size);
	}
    return xrealloc(ptr, nmemb * size);
}