clang-format
This commit is contained in:
@@ -48,45 +48,40 @@ extern const char *hostcompat_progname;
|
||||
/*
|
||||
* Common routine for all the *err* and *warn* functions.
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
|
||||
{
|
||||
const char *errmsg;
|
||||
static void hostcompat_printerr(int use_errno, const char *fmt, va_list ap) {
|
||||
const char *errmsg;
|
||||
|
||||
/*
|
||||
* Get the error message for the current errno.
|
||||
* Do this early, before doing anything that might change the
|
||||
* value in errno.
|
||||
*/
|
||||
errmsg = strerror(errno);
|
||||
/*
|
||||
* Get the error message for the current errno.
|
||||
* Do this early, before doing anything that might change the
|
||||
* value in errno.
|
||||
*/
|
||||
errmsg = strerror(errno);
|
||||
|
||||
/*
|
||||
* Look up the program name.
|
||||
* Strictly speaking we should pull off the rightmost
|
||||
* path component of argv[0] and use that as the program
|
||||
* name (this is how BSD err* prints) but it doesn't make
|
||||
* much difference.
|
||||
*/
|
||||
if (hostcompat_progname != NULL) {
|
||||
fprintf(stderr, "%s: ", hostcompat_progname);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
|
||||
fprintf(stderr, "libhostcompat-program: ");
|
||||
}
|
||||
/*
|
||||
* Look up the program name.
|
||||
* Strictly speaking we should pull off the rightmost
|
||||
* path component of argv[0] and use that as the program
|
||||
* name (this is how BSD err* prints) but it doesn't make
|
||||
* much difference.
|
||||
*/
|
||||
if (hostcompat_progname != NULL) {
|
||||
fprintf(stderr, "%s: ", hostcompat_progname);
|
||||
} else {
|
||||
fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
|
||||
fprintf(stderr, "libhostcompat-program: ");
|
||||
}
|
||||
|
||||
/* process the printf format and args */
|
||||
vfprintf(stderr, fmt, ap);
|
||||
/* process the printf format and args */
|
||||
vfprintf(stderr, fmt, ap);
|
||||
|
||||
if (use_errno) {
|
||||
/* if we're using errno, print the error string from above. */
|
||||
fprintf(stderr, ": %s\n", errmsg);
|
||||
}
|
||||
else {
|
||||
/* otherwise, just a newline. */
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
if (use_errno) {
|
||||
/* if we're using errno, print the error string from above. */
|
||||
fprintf(stderr, ": %s\n", errmsg);
|
||||
} else {
|
||||
/* otherwise, just a newline. */
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -94,33 +89,21 @@ hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
|
||||
*/
|
||||
|
||||
/* warn/vwarn: use errno, don't exit */
|
||||
void
|
||||
vwarn(const char *fmt, va_list ap)
|
||||
{
|
||||
hostcompat_printerr(1, fmt, ap);
|
||||
}
|
||||
void vwarn(const char *fmt, va_list ap) { hostcompat_printerr(1, fmt, ap); }
|
||||
|
||||
/* warnx/vwarnx: don't use errno, don't exit */
|
||||
void
|
||||
vwarnx(const char *fmt, va_list ap)
|
||||
{
|
||||
hostcompat_printerr(0, fmt, ap);
|
||||
}
|
||||
void vwarnx(const char *fmt, va_list ap) { hostcompat_printerr(0, fmt, ap); }
|
||||
|
||||
/* err/verr: use errno, then exit */
|
||||
void
|
||||
verr(int exitcode, const char *fmt, va_list ap)
|
||||
{
|
||||
hostcompat_printerr(1, fmt, ap);
|
||||
exit(exitcode);
|
||||
void verr(int exitcode, const char *fmt, va_list ap) {
|
||||
hostcompat_printerr(1, fmt, ap);
|
||||
exit(exitcode);
|
||||
}
|
||||
|
||||
/* errx/verrx: don't use errno, but do then exit */
|
||||
void
|
||||
verrx(int exitcode, const char *fmt, va_list ap)
|
||||
{
|
||||
hostcompat_printerr(0, fmt, ap);
|
||||
exit(exitcode);
|
||||
void verrx(int exitcode, const char *fmt, va_list ap) {
|
||||
hostcompat_printerr(0, fmt, ap);
|
||||
exit(exitcode);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -128,40 +111,32 @@ verrx(int exitcode, const char *fmt, va_list ap)
|
||||
* Just hand off to the va_list versions.
|
||||
*/
|
||||
|
||||
void
|
||||
warn(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarn(fmt, ap);
|
||||
va_end(ap);
|
||||
void warn(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarn(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
warnx(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarnx(fmt, ap);
|
||||
va_end(ap);
|
||||
void warnx(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarnx(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
err(int exitcode, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verr(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
void err(int exitcode, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verr(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
errx(int exitcode, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verrx(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
void errx(int exitcode, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verrx(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
#endif /* NEED_ERR */
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef HOSTCOMPAT_ERR_H
|
||||
#define HOSTCOMPAT_ERR_H
|
||||
|
||||
|
||||
@@ -51,188 +51,168 @@ static struct termios hostcompat_savetios;
|
||||
/*
|
||||
* Put the tty state back the way it was.
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_ttyreset(void)
|
||||
{
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_savetios);
|
||||
static void hostcompat_ttyreset(void) {
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_savetios);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the tty state back to the way we want it for running.
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_ttyresume(void)
|
||||
{
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_runtios);
|
||||
static void hostcompat_ttyresume(void) {
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &hostcompat_runtios);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up the tty state stuff.
|
||||
*/
|
||||
static
|
||||
int
|
||||
hostcompat_ttysetup(void)
|
||||
{
|
||||
struct termios tios;
|
||||
static int hostcompat_ttysetup(void) {
|
||||
struct termios tios;
|
||||
|
||||
/* Get the current tty state. */
|
||||
if (tcgetattr(STDIN_FILENO, &tios) < 0) {
|
||||
/* stdin is not a tty */
|
||||
return -1;
|
||||
}
|
||||
/* Get the current tty state. */
|
||||
if (tcgetattr(STDIN_FILENO, &tios) < 0) {
|
||||
/* stdin is not a tty */
|
||||
return -1;
|
||||
}
|
||||
|
||||
hostcompat_savetios = tios;
|
||||
hostcompat_savetios = tios;
|
||||
|
||||
/* Turn off canonical ("cooked") input. */
|
||||
tios.c_lflag &= ~ICANON;
|
||||
/* Turn off canonical ("cooked") input. */
|
||||
tios.c_lflag &= ~ICANON;
|
||||
|
||||
/*
|
||||
* With canonical input off, this says how many characters must be
|
||||
* typed before read() will return.
|
||||
*/
|
||||
tios.c_cc[VMIN] = 1;
|
||||
/*
|
||||
* With canonical input off, this says how many characters must be
|
||||
* typed before read() will return.
|
||||
*/
|
||||
tios.c_cc[VMIN] = 1;
|
||||
|
||||
/* This can be used to set up read timeouts, but we don't need that. */
|
||||
tios.c_cc[VTIME] = 0;
|
||||
/* This can be used to set up read timeouts, but we don't need that. */
|
||||
tios.c_cc[VTIME] = 0;
|
||||
|
||||
/* Turn off echoing of keypresses. */
|
||||
tios.c_lflag &= ~(ECHO|ECHONL|ECHOCTL);
|
||||
/* Turn off echoing of keypresses. */
|
||||
tios.c_lflag &= ~(ECHO | ECHONL | ECHOCTL);
|
||||
|
||||
/* Do not support XON/XOFF flow control. */
|
||||
tios.c_iflag &= ~(IXON|IXOFF);
|
||||
/* Do not support XON/XOFF flow control. */
|
||||
tios.c_iflag &= ~(IXON | IXOFF);
|
||||
|
||||
/* On input, we want no CR/LF translation. */
|
||||
tios.c_iflag &= ~(INLCR|IGNCR|ICRNL);
|
||||
/* On input, we want no CR/LF translation. */
|
||||
tios.c_iflag &= ~(INLCR | IGNCR | ICRNL);
|
||||
|
||||
/* However, on output we want LF ('\n') mapped to CRLF. */
|
||||
#ifdef OCRNL /* missing on OS X */
|
||||
tios.c_oflag &= ~(OCRNL);
|
||||
/* However, on output we want LF ('\n') mapped to CRLF. */
|
||||
#ifdef OCRNL /* missing on OS X */
|
||||
tios.c_oflag &= ~(OCRNL);
|
||||
#endif
|
||||
tios.c_oflag |= OPOST|ONLCR;
|
||||
tios.c_oflag |= OPOST | ONLCR;
|
||||
|
||||
/* Enable keyboard signals (^C, ^Z, etc.) because they're useful. */
|
||||
tios.c_lflag |= ISIG;
|
||||
/* Enable keyboard signals (^C, ^Z, etc.) because they're useful. */
|
||||
tios.c_lflag |= ISIG;
|
||||
|
||||
/* Set the new tty state. */
|
||||
hostcompat_runtios = tios;
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &tios);
|
||||
/* Set the new tty state. */
|
||||
hostcompat_runtios = tios;
|
||||
tcsetattr(STDIN_FILENO, TCSADRAIN, &tios);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Signal handler for all the fatal signals (SIGSEGV, SIGTERM, etc.)
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_die(int sig)
|
||||
{
|
||||
/* Set the tty back to the way we found it */
|
||||
hostcompat_ttyreset();
|
||||
static void hostcompat_die(int sig) {
|
||||
/* Set the tty back to the way we found it */
|
||||
hostcompat_ttyreset();
|
||||
|
||||
/* Make sure the default action will occur when we get another signal*/
|
||||
signal(sig, SIG_DFL);
|
||||
/* Make sure the default action will occur when we get another signal*/
|
||||
signal(sig, SIG_DFL);
|
||||
|
||||
/* Post the signal back to ourselves, to cause the right exit status.*/
|
||||
kill(getpid(), sig);
|
||||
/* Post the signal back to ourselves, to cause the right exit status.*/
|
||||
kill(getpid(), sig);
|
||||
|
||||
/* Just in case. */
|
||||
_exit(255);
|
||||
/* Just in case. */
|
||||
_exit(255);
|
||||
}
|
||||
|
||||
/*
|
||||
* Signal handler for the stop signals (SIGTSTP, SIGTTIN, etc.)
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_stop(int sig)
|
||||
{
|
||||
/* Set the tty back to the way we found it */
|
||||
hostcompat_ttyreset();
|
||||
static void hostcompat_stop(int sig) {
|
||||
/* Set the tty back to the way we found it */
|
||||
hostcompat_ttyreset();
|
||||
|
||||
/* Make sure the default action will occur when we get another signal*/
|
||||
signal(sig, SIG_DFL);
|
||||
/* Make sure the default action will occur when we get another signal*/
|
||||
signal(sig, SIG_DFL);
|
||||
|
||||
/* Post the signal back to ourselves. */
|
||||
kill(getpid(), sig);
|
||||
/* Post the signal back to ourselves. */
|
||||
kill(getpid(), sig);
|
||||
}
|
||||
|
||||
/*
|
||||
* Signal handler for SIGCONT.
|
||||
*/
|
||||
static
|
||||
void
|
||||
hostcompat_cont(int sig)
|
||||
{
|
||||
(void)sig;
|
||||
static void hostcompat_cont(int sig) {
|
||||
(void)sig;
|
||||
|
||||
/* Set the tty to the way we want it for running. */
|
||||
hostcompat_ttyresume();
|
||||
/* Set the tty to the way we want it for running. */
|
||||
hostcompat_ttyresume();
|
||||
|
||||
/*
|
||||
* Reload the signal handlers for stop/continue signals, in case
|
||||
* they were set up with one-shot signals.
|
||||
*/
|
||||
signal(SIGTTIN, hostcompat_stop);
|
||||
signal(SIGTTOU, hostcompat_stop);
|
||||
signal(SIGTSTP, hostcompat_stop);
|
||||
signal(SIGCONT, hostcompat_cont);
|
||||
/*
|
||||
* Reload the signal handlers for stop/continue signals, in case
|
||||
* they were set up with one-shot signals.
|
||||
*/
|
||||
signal(SIGTTIN, hostcompat_stop);
|
||||
signal(SIGTTOU, hostcompat_stop);
|
||||
signal(SIGTSTP, hostcompat_stop);
|
||||
signal(SIGCONT, hostcompat_cont);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the hostcompat library.
|
||||
*/
|
||||
void
|
||||
hostcompat_init(int argc, char *argv[])
|
||||
{
|
||||
/* Set the program name */
|
||||
if (argc > 0 && argv[0] != NULL) {
|
||||
hostcompat_progname = argv[0];
|
||||
}
|
||||
void hostcompat_init(int argc, char *argv[]) {
|
||||
/* Set the program name */
|
||||
if (argc > 0 && argv[0] != NULL) {
|
||||
hostcompat_progname = argv[0];
|
||||
}
|
||||
|
||||
/* Set the tty modes */
|
||||
if (hostcompat_ttysetup() < 0) {
|
||||
return;
|
||||
}
|
||||
/* Set the tty modes */
|
||||
if (hostcompat_ttysetup() < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* When exit() is called, clean up */
|
||||
atexit(hostcompat_ttyreset);
|
||||
/* When exit() is called, clean up */
|
||||
atexit(hostcompat_ttyreset);
|
||||
|
||||
/* stdout/stderr should be unbuffered */
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stderr, NULL, _IONBF, 0);
|
||||
/* stdout/stderr should be unbuffered */
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
setvbuf(stderr, NULL, _IONBF, 0);
|
||||
|
||||
/* Catch all the fatal signals, so we can clean up */
|
||||
signal(SIGHUP, hostcompat_die);
|
||||
signal(SIGINT, hostcompat_die);
|
||||
signal(SIGQUIT, hostcompat_die);
|
||||
signal(SIGILL, hostcompat_die);
|
||||
signal(SIGTRAP, hostcompat_die);
|
||||
signal(SIGABRT, hostcompat_die);
|
||||
/* Catch all the fatal signals, so we can clean up */
|
||||
signal(SIGHUP, hostcompat_die);
|
||||
signal(SIGINT, hostcompat_die);
|
||||
signal(SIGQUIT, hostcompat_die);
|
||||
signal(SIGILL, hostcompat_die);
|
||||
signal(SIGTRAP, hostcompat_die);
|
||||
signal(SIGABRT, hostcompat_die);
|
||||
#ifdef SIGEMT
|
||||
signal(SIGEMT, hostcompat_die);
|
||||
signal(SIGEMT, hostcompat_die);
|
||||
#endif
|
||||
signal(SIGFPE, hostcompat_die);
|
||||
signal(SIGBUS, hostcompat_die);
|
||||
signal(SIGSEGV, hostcompat_die);
|
||||
signal(SIGSYS, hostcompat_die);
|
||||
signal(SIGPIPE, hostcompat_die);
|
||||
signal(SIGALRM, hostcompat_die);
|
||||
signal(SIGTERM, hostcompat_die);
|
||||
signal(SIGXCPU, hostcompat_die);
|
||||
signal(SIGXFSZ, hostcompat_die);
|
||||
signal(SIGVTALRM, hostcompat_die);
|
||||
signal(SIGPROF, hostcompat_die);
|
||||
signal(SIGUSR1, hostcompat_die);
|
||||
signal(SIGUSR2, hostcompat_die);
|
||||
signal(SIGFPE, hostcompat_die);
|
||||
signal(SIGBUS, hostcompat_die);
|
||||
signal(SIGSEGV, hostcompat_die);
|
||||
signal(SIGSYS, hostcompat_die);
|
||||
signal(SIGPIPE, hostcompat_die);
|
||||
signal(SIGALRM, hostcompat_die);
|
||||
signal(SIGTERM, hostcompat_die);
|
||||
signal(SIGXCPU, hostcompat_die);
|
||||
signal(SIGXFSZ, hostcompat_die);
|
||||
signal(SIGVTALRM, hostcompat_die);
|
||||
signal(SIGPROF, hostcompat_die);
|
||||
signal(SIGUSR1, hostcompat_die);
|
||||
signal(SIGUSR2, hostcompat_die);
|
||||
|
||||
/* Catch the stop signals, so we can adjust the tty */
|
||||
signal(SIGTTIN, hostcompat_stop);
|
||||
signal(SIGTTOU, hostcompat_stop);
|
||||
signal(SIGTSTP, hostcompat_stop);
|
||||
/* Catch the stop signals, so we can adjust the tty */
|
||||
signal(SIGTTIN, hostcompat_stop);
|
||||
signal(SIGTTOU, hostcompat_stop);
|
||||
signal(SIGTSTP, hostcompat_stop);
|
||||
|
||||
/* Catch the continue signal, so we can adjust the tty */
|
||||
signal(SIGCONT, hostcompat_cont);
|
||||
/* Catch the continue signal, so we can adjust the tty */
|
||||
signal(SIGCONT, hostcompat_cont);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
@@ -34,20 +34,18 @@
|
||||
|
||||
#ifdef NEED_NTOHLL
|
||||
|
||||
uint64_t
|
||||
ntohll(uint64_t x)
|
||||
{
|
||||
uint32_t x0, x1, y0, y1;
|
||||
uint64_t ntohll(uint64_t x) {
|
||||
uint32_t x0, x1, y0, y1;
|
||||
|
||||
if (ntohl(1) == 1) {
|
||||
return x;
|
||||
}
|
||||
if (ntohl(1) == 1) {
|
||||
return x;
|
||||
}
|
||||
|
||||
x0 = x & 0xffffffff;
|
||||
y0 = ntohl(x0);
|
||||
x1 = x >> 32;
|
||||
y1 = ntohl(x1);
|
||||
return ((uint64_t)y0 << 32) | y1;
|
||||
x0 = x & 0xffffffff;
|
||||
y0 = ntohl(x0);
|
||||
x1 = x >> 32;
|
||||
y1 = ntohl(x1);
|
||||
return ((uint64_t)y0 << 32) | y1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -33,22 +33,20 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <string.h> /* sometimes required for NULL */
|
||||
#include <string.h> /* sometimes required for NULL */
|
||||
|
||||
#include "hostcompat.h"
|
||||
|
||||
time_t
|
||||
__time(time_t *secs, unsigned long *nsecs)
|
||||
{
|
||||
struct timeval tv;
|
||||
if (gettimeofday(&tv, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (secs) {
|
||||
*secs = tv.tv_sec;
|
||||
}
|
||||
if (nsecs) {
|
||||
*nsecs = tv.tv_usec * 1000;
|
||||
}
|
||||
return tv.tv_sec;
|
||||
time_t __time(time_t *secs, unsigned long *nsecs) {
|
||||
struct timeval tv;
|
||||
if (gettimeofday(&tv, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (secs) {
|
||||
*secs = tv.tv_sec;
|
||||
}
|
||||
if (nsecs) {
|
||||
*nsecs = tv.tv_usec * 1000;
|
||||
}
|
||||
return tv.tv_sec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user