~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/misc.c -rw-r--r-- 4.2 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 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
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "log.h"
#include "misc.h"


bool isbigendian(void)
{
	uint8_t test[2] = {1, 0};
	return *(uint16_t *)test != 1;
}

uint64_t file_size(const char *path)
{
	static struct stat st;
	if (stat(path, &st) == -1)
		return UINT64_MAX;
	return st.st_size;
}

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

	if (read != -1 && (*lineptr)[read - 1] == delim)
		(*lineptr)[--read] = '\0';
	return read;
}

float xstrtof(const char *str)
{
	if (*str == '\0')
		LOG_DIE("%s: empty string argument", __func__);
	errno = 0;
	char *end;
	const float ret = strtof(str, &end);
	if (*end != '\0')
		LOG_DIE("%s: trailing text found in \"%s\"", __func__, str);
	if (errno == ERANGE)
	{
		LOG_DIE("%s: float conversion %s with \"%s\"", __func__,
			ret == HUGE_VAL ? "overflow" : "underflow", str);
	}
	return ret;
}

long xstrtol(const char *str)
{
	if (*str == '\0')
		LOG_DIE("%s: empty string argument", __func__);
	errno = 0;
	char *end;
	const long ret = strtol(str, &end, 10);
	if (*end != '\0')
		LOG_DIE("%s: trailing text found in \"%s\"", __func__, str);
	if (errno == EINVAL)
		LOG_DIE("%s: no long conversion possible with \"%s\"", __func__, str);
	if (errno == ERANGE)
	{
		LOG_DIE("%s: long conversion %s with \"%s\"", __func__,
			ret == LONG_MAX ? "overflow" : "underflow", str);
	}
	return ret;
}

int xstrtoi(const char *str)
{
	const long ret = xstrtol(str);
	if (ret > INT_MAX || ret < INT_MIN)
	{
		LOG_DIE("%s: int conversion %s with \"%s\"", __func__,
			ret > INT_MAX ? "overflow" : "underflow", str);
	}
	return (int)ret;
}

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))
			LOG_DIE("signal %d: %s", sigs[i], strerror(errno));
	}
}

bool read_full(int fd, void *buf, size_t nbyte)
{
	for (ssize_t ret; nbyte; nbyte -= ret)
	{
		if ((ret = read(fd, buf, nbyte)) <= 0)
			return false;
		buf = (char *)buf + ret;
	}
	return true;
}

bool write_full(int fd, const void *buf, size_t nbyte)
{
	for (ssize_t ret; nbyte; nbyte -= ret)
	{
		if ((ret = write(fd, buf, nbyte)) == -1)
			return false;
		buf = (char *)buf + ret;
	}
	return true;
}

ssize_t recv_full(int sockfd, void *buf, size_t len)
{
	static char peek_buf[1];
	const size_t orig_len = len;
	ssize_t ret;

	while (len)
	{
		ret = recv(sockfd, buf, len, 0);
		switch (ret)
		{
			case -1:
				if (errno != ECONNRESET)
					return -1;
				break;

			case 0:
				return orig_len - len;

			default:
				len -= ret;
				buf = (char *)buf + ret;
		}
	}

	do
	{
#ifdef MSG_DONTWAIT
		ret = recv(sockfd, peek_buf, sizeof(peek_buf),
			MSG_PEEK | MSG_DONTWAIT);
#else
		const int flags = fcntl(sockfd, F_GETFL);
		fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
		ret = recv(sockfd, peek_buf, sizeof(peek_buf), MSG_PEEK);
		fcntl(sockfd, F_SETFL, flags & ~O_NONBLOCK);
#endif
	}
	while (ret == -1 && errno == ECONNRESET);

	if (ret == -1)
		return -1;
	return ret ? orig_len + 1 : orig_len - len;
}

bool send_full(int sockfd, void *buf, size_t len)
{
	for (ssize_t ret; len; len -= ret)
	{
		ret = send(sockfd, buf, len, MSG_NOSIGNAL);
		if (ret == -1)
			return false;
		buf = (char *)buf + ret;
	}
	return true;
}

int xopen(const char *path, int oflag)
{
	int fd = open(path, oflag);
	if (fd == -1)
		LOG_DIE("%s: %s", path, strerror(errno));
	return fd;
}

void xclose(int fd)
{
	if (close(fd) == -1)
		LOG_DIE("close: %s", strerror(errno));
}

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

void * xstrdup(const char *s)
{
	char *p;
    if ((p = strdup(s)) == NULL)
		LOG_DIE("xstrdup(%s)", s);
	return p;
}

void * xmalloc(size_t size)
{
	void *p;
    if ((p = malloc(size)) == NULL)
		LOG_DIE("xmalloc(%zu)", size);
	return p;
}