master ohmu/leeloo / src / util.c
ohmu  ·  2026-10-18
 1/* See LICENSE.dwm file for copyright and license details. */
 2#include <stdarg.h>
 3#include <stdio.h>
 4#include <stdlib.h>
 5#include <string.h>
 6#include <fcntl.h>
 7
 8#include "util.h"
 9
10void
11die(const char *fmt, ...) {
12	va_list ap;
13
14	va_start(ap, fmt);
15	vfprintf(stderr, fmt, ap);
16	va_end(ap);
17
18	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
19		fputc(' ', stderr);
20		perror(NULL);
21	} else {
22		fputc('\n', stderr);
23	}
24
25	exit(1);
26}
27
28void *
29ecalloc(size_t nmemb, size_t size)
30{
31	void *p;
32
33	if (!(p = calloc(nmemb, size)))
34		die("calloc:");
35	return p;
36}
37
38int
39fd_set_nonblock(int fd) {
40	int flags = fcntl(fd, F_GETFL);
41	if (flags < 0) {
42		perror("fcntl(F_GETFL):");
43		return -1;
44	}
45	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
46		perror("fcntl(F_SETFL):");
47		return -1;
48	}
49
50	return 0;
51}