117 lines
2.2 KiB
C
117 lines
2.2 KiB
C
/*
|
|
* fractal
|
|
* Written in 2020 by Lucas
|
|
* CC0 1.0 Universal/Public domain - No rights reserved
|
|
*
|
|
* To the extent possible under law, the author(s) have dedicated all
|
|
* copyright and related and neighboring rights to this software to the
|
|
* public domain worldwide. This software is distributed without any
|
|
* warranty. You should have received a copy of the CC0 Public Domain
|
|
* Dedication along with this software. If not, see
|
|
* <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
*/
|
|
/* srand48 visibility for GLIBC */
|
|
#if !defined(__OpenBSD__)
|
|
#define _DEFAULT_SOURCE
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "util.h"
|
|
|
|
void
|
|
err(int eval, const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
int saved_errno;
|
|
|
|
saved_errno = errno;
|
|
va_start(ap, fmt);
|
|
fprintf(stderr, "%s: ", xgetprogname());
|
|
if (fmt != NULL) {
|
|
vfprintf(stderr, fmt, ap);
|
|
fprintf(stderr, ": ");
|
|
}
|
|
fprintf(stderr, "%s\n", strerror(saved_errno));
|
|
va_end(ap);
|
|
|
|
exit(eval);
|
|
}
|
|
|
|
void
|
|
errx(int eval, const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
if (fmt != NULL) {
|
|
fprintf(stderr, "%s: ", xgetprogname());
|
|
vfprintf(stderr, fmt, ap);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
va_end(ap);
|
|
|
|
exit(eval);
|
|
}
|
|
|
|
long double
|
|
parse_double(const char *nptr)
|
|
{
|
|
char *end;
|
|
long double v;
|
|
|
|
errno = 0;
|
|
v = strtold(nptr, &end);
|
|
if (nptr == end || *end != '\0' || (v == 0 && errno == EINVAL))
|
|
errx(1, "invalid number: %s", nptr);
|
|
if (errno == ERANGE)
|
|
errx(1, "number out of range: %s", nptr);
|
|
|
|
return v;
|
|
}
|
|
|
|
long long
|
|
parse_integer(const char *nptr, long long minval, long long maxval)
|
|
{
|
|
char *end;
|
|
long long v;
|
|
|
|
errno = 0;
|
|
v = strtoll(nptr, &end, 10);
|
|
if (nptr == end || *end != '\0' || (v == 0 && errno == EINVAL))
|
|
errx(1, "invalid number: %s", nptr);
|
|
if (v == 0 && errno == ERANGE)
|
|
errx(1, "number out of range: %s", nptr);
|
|
if (v < minval)
|
|
errx(1, "number too low: %s", nptr);
|
|
if (v > maxval)
|
|
errx(1, "number too high: %s", nptr);
|
|
|
|
return v;
|
|
}
|
|
|
|
const char *
|
|
xgetprogname(void)
|
|
{
|
|
#if defined(__OpenBSD__)
|
|
return getprogname();
|
|
#else
|
|
extern char *__progname;
|
|
return __progname;
|
|
#endif
|
|
}
|
|
|
|
void
|
|
xsrand48(long seed)
|
|
{
|
|
#if defined(__OpenBSD__)
|
|
srand48_deterministic(seed);
|
|
#else
|
|
srand48(seed);
|
|
#endif
|
|
}
|