generalized die()

using fprintf() instead of fputs() to allow string concatenation, die() 
now requires a error message which can be NULL and a error message which 
will be passed to strerror(), this ensures that errno is fresh when we 
use it
xkbcommon
Alessandro Mauri 5 years ago
parent 58aceac802
commit 58c01fb9d1
  1. 34
      macrod.c

@ -44,7 +44,7 @@ const char ev_root[] = "/dev/input/";
int pressBufferAdd (struct pressed_buffer*, unsigned short); int pressBufferAdd (struct pressed_buffer*, unsigned short);
int pressBufferRemove (struct pressed_buffer*, unsigned short); int pressBufferRemove (struct pressed_buffer*, unsigned short);
void termHandler (int signum); void termHandler (int signum);
void die (void); void die (const char *, int);
void execCommand(const char *); void execCommand(const char *);
// TODO: use getopts() to parse command line options // TODO: use getopts() to parse command line options
@ -60,7 +60,7 @@ int main (void)
/* Open the event directory */ /* Open the event directory */
DIR *ev_dir = opendir(ev_root); DIR *ev_dir = opendir(ev_root);
if (!ev_dir) if (!ev_dir)
die(); die("opendir", errno);
int fd_num = 0; int fd_num = 0;
struct pollfd *fds = NULL; struct pollfd *fds = NULL;
@ -105,7 +105,7 @@ int main (void)
tmp_p = realloc(fds, sizeof(struct pollfd) * (fd_num + 1)); tmp_p = realloc(fds, sizeof(struct pollfd) * (fd_num + 1));
if (!tmp_p) if (!tmp_p)
die(); die("realloc file descriptors", errno);
fds = tmp_p; fds = tmp_p;
fds[fd_num].events = POLLIN; fds[fd_num].events = POLLIN;
@ -134,10 +134,10 @@ int main (void)
epoll_read_ev.events = EPOLLIN; epoll_read_ev.events = EPOLLIN;
int ev_fd = epoll_create(1); int ev_fd = epoll_create(1);
if (ev_fd < 0) if (ev_fd < 0)
die(); die("epoll_create", errno);
for (int i = 0; i < fd_num; i++) for (int i = 0; i < fd_num; i++)
if (epoll_ctl(ev_fd, EPOLL_CTL_ADD, fds[i].fd, &epoll_read_ev) < 0) if (epoll_ctl(ev_fd, EPOLL_CTL_ADD, fds[i].fd, &epoll_read_ev) < 0)
die(); die("epoll_ctl", errno);
#endif #endif
for (;;) { for (;;) {
@ -214,7 +214,8 @@ int main (void)
if (!term) if (!term)
fputs("An error occured\n", stderr); fputs("An error occured\n", stderr);
for (int i = 0; i < fd_num; i++) { for (int i = 0; i < fd_num; i++) {
if (close(fds[i].fd) == -1) die(); if (close(fds[i].fd) == -1)
die("close file descriptors", errno);
} }
return 0; return 0;
} }
@ -234,10 +235,8 @@ int pressBufferAdd (struct pressed_buffer *pb, unsigned short key)
unsigned short *b; unsigned short *b;
b = realloc(pb->buf, sizeof(unsigned short) * (pb->size + 1)); b = realloc(pb->buf, sizeof(unsigned short) * (pb->size + 1));
if (!b) { if (!b)
fprintf(stderr, "realloc failed in pressBufferAdd: %s", strerror(errno)); die("realloc failed in pressBufferAdd", errno);
exit(errno);
}
pb->buf = b; pb->buf = b;
pb->buf[pb->size++] = key; pb->buf[pb->size++] = key;
@ -257,11 +256,8 @@ int pressBufferRemove (struct pressed_buffer *pb, unsigned short key)
unsigned short *b; unsigned short *b;
b = realloc(pb->buf, sizeof(unsigned short) * pb->size); b = realloc(pb->buf, sizeof(unsigned short) * pb->size);
/* if realloc failed but the buffer is populated throw an error */ /* if realloc failed but the buffer is populated throw an error */
if (!b && pb->size) { if (!b && pb->size)
fprintf(stderr, "realloc failed in pressBufferRemove: %s", die("realloc failed in pressBufferRemove: %s", errno);
strerror(errno));
exit(errno);
}
pb->buf = b; pb->buf = b;
return 0; return 0;
} }
@ -275,17 +271,17 @@ void termHandler (int signum)
term = 1; term = 1;
} }
void die (void) void die (const char *msg, int err)
{ {
fputs(strerror(errno), stderr); fprintf(stderr, "%s: %s", msg != NULL ? msg : "error", err ? strerror(err): "exiting");
exit(errno); exit(err);
} }
void execCommand (const char *path) void execCommand (const char *path)
{ {
switch (fork()) { switch (fork()) {
case -1: case -1:
fprintf(stderr, "Could not fork: %s", strerror(errno)); die("Could not fork: %s", errno);
break; break;
case 0: case 0:
/* we are the child */ /* we are the child */

Loading…
Cancel
Save