dwl/util.c

54 lines
779 B
C
Raw Normal View History

/* See LICENSE.dwm file for copyright and license details. */
2026-04-21 14:11:09 +00:00
#include "util.h"
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
2026-04-21 14:11:09 +00:00
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
2026-04-21 14:11:09 +00:00
if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
exit(1);
}
2022-10-29 14:55:03 -05:00
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}
2024-06-27 13:19:16 -06:00
int
2026-04-21 14:11:09 +00:00
fd_set_nonblock(int fd)
{
2024-06-27 13:19:16 -06:00
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
2024-06-27 13:19:16 -06:00
perror("fcntl(F_GETFL):");
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
2024-06-27 13:19:16 -06:00
perror("fcntl(F_SETFL):");
return -1;
}
2024-06-27 13:19:16 -06:00
return 0;
}