util.c 4.1 KB
Newer Older
1 2 3 4
/* See LICENSE file for copyright and license details. */
#include <arpa/inet.h>

#include <errno.h>
5
#include <limits.h>
Laslo Hunhold's avatar
Laslo Hunhold committed
6
#include <stdarg.h>
7 8 9 10
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
11
#include <sys/types.h>
12 13 14 15 16

#include "util.h"

char *argv0;

Laslo Hunhold's avatar
Laslo Hunhold committed
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
static void
verr(const char *fmt, va_list ap)
{
	if (argv0 && strncmp(fmt, "usage", sizeof("usage") - 1)) {
		fprintf(stderr, "%s: ", argv0);
	}

	vfprintf(stderr, fmt, ap);

	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}
}

void
warn(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	verr(fmt, ap);
	va_end(ap);
}

void
die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	verr(fmt, ap);
	va_end(ap);

	exit(1);
}

56
void
57
ff_read_header(uint32_t *width, uint32_t *height)
58 59 60
{
	uint32_t hdr[4];

61
	efread(hdr, sizeof(*hdr), LEN(hdr), stdin);
62 63

	if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
Laslo Hunhold's avatar
Laslo Hunhold committed
64
		die("Invalid magic value");
65 66 67 68 69 70 71
	}

	*width = ntohl(hdr[2]);
	*height = ntohl(hdr[3]);
}

void
72
ff_write_header(uint32_t width, uint32_t height)
73 74 75 76 77 78
{
	uint32_t tmp;

	fputs("farbfeld", stdout);

	tmp = htonl(width);
79
	efwrite(&tmp, sizeof(tmp), 1, stdout);
80 81

	tmp = htonl(height);
82
	efwrite(&tmp, sizeof(tmp), 1, stdout);
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

int
parse_mask(const char *s, uint16_t mask[3])
{
	size_t slen, i;
	unsigned int col[3], colfac;
	char fmt[] = "%#x%#x%#x";

	slen = strlen(s);
	if (slen != 3 && slen != 6 && slen != 12) {
		return 1;
	}

	fmt[1] = fmt[4] = fmt[7] = ((slen / 3) + '0');
	if (sscanf(s, fmt, col, col + 1, col + 2) != 3) {
		return 1;
	}

	colfac = (slen == 3) ? UINT16_MAX / 0xf :
	         (slen == 6) ? UINT16_MAX / 0xff :
	                       UINT16_MAX / 0xffff;

	for (i = 0; i < 3; i++) {
		mask[i] = col[i] * colfac;
	}

	return 0;
}

113 114 115 116 117 118 119 120 121 122 123 124 125
int
fshut(FILE *fp, const char *fname)
{
	int ret = 0;

	/* fflush() is undefined for input streams by ISO C,
	 * but not POSIX 2008 if you ignore ISO C overrides.
	 * Leave it unchecked and rely on the following
	 * functions to detect errors.
	 */
	fflush(fp);

	if (ferror(fp) && !ret) {
Laslo Hunhold's avatar
Laslo Hunhold committed
126
		warn("ferror '%s':", fname);
127 128 129 130
		ret = 1;
	}

	if (fclose(fp) && !ret) {
Laslo Hunhold's avatar
Laslo Hunhold committed
131
		warn("fclose '%s':", fname);
132 133 134 135 136 137
		ret = 1;
	}

	return ret;
}

138 139 140 141 142
void
efread(void *p, size_t s, size_t n, FILE *f)
{
	if (fread(p, s, n, f) != n) {
		if (ferror(f)) {
Laslo Hunhold's avatar
Laslo Hunhold committed
143
			die("fread:");
144
		} else {
Laslo Hunhold's avatar
Laslo Hunhold committed
145
			die("fread: Unexpected end of file");
146 147 148 149 150 151 152 153
		}
	}
}

void
efwrite(const void *p, size_t s, size_t n, FILE *f)
{
	if (fwrite(p, s, n, f) != n) {
Laslo Hunhold's avatar
Laslo Hunhold committed
154
		die("fwrite:");
155 156 157
	}
}

158 159 160 161 162 163
void *
ereallocarray(void *optr, size_t nmemb, size_t size)
{
	void *p;

	if (!(p = reallocarray(optr, nmemb, size))) {
Laslo Hunhold's avatar
Laslo Hunhold committed
164
		die("reallocarray: Out of memory");
165 166 167 168 169 170 171 172 173 174 175 176 177
	}

	return p;
}

long long
estrtonum(const char *numstr, long long minval, long long maxval)
{
	const char *errstr;
	long long ll;

	ll = strtonum(numstr, minval, maxval, &errstr);
	if (errstr) {
Laslo Hunhold's avatar
Laslo Hunhold committed
178
		die("strtonum '%s': %s", numstr, errstr);
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 234 235 236 237 238 239 240 241 242
	}

	return ll;
}

/*
 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
 */
#define MUL_NO_OVERFLOW	(1UL << (sizeof(size_t) * 4))

void *
reallocarray(void *optr, size_t nmemb, size_t size)
{
	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
	    nmemb > 0 && SIZE_MAX / nmemb < size) {
		errno = ENOMEM;
		return NULL;
	}
	return realloc(optr, size * nmemb);
}

#define	INVALID		1
#define	TOOSMALL	2
#define	TOOLARGE	3

long long
strtonum(const char *numstr, long long minval, long long maxval,
         const char **errstrp)
{
	long long ll = 0;
	int error = 0;
	char *ep;
	struct errval {
		const char *errstr;
		int err;
	} ev[4] = {
		{ NULL,		0 },
		{ "invalid",	EINVAL },
		{ "too small",	ERANGE },
		{ "too large",	ERANGE },
	};

	ev[0].err = errno;
	errno = 0;
	if (minval > maxval) {
		error = INVALID;
	} else {
		ll = strtoll(numstr, &ep, 10);
		if (numstr == ep || *ep != '\0')
			error = INVALID;
		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
			error = TOOSMALL;
		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
			error = TOOLARGE;
	}
	if (errstrp != NULL)
		*errstrp = ev[error].errstr;
	errno = ev[error].err;
	if (error)
		ll = 0;

	return (ll);
}