all repos — dwm @ 439e15d09f6fa9271d3b49ef97194f0c80ebe161

fork of suckless dynamic window manager

util.c (view raw)

  1/*
  2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3 * See LICENSE file for license details.
  4 */
  5
  6#include <stdarg.h>
  7#include <stdio.h>
  8#include <stdlib.h>
  9#include <string.h>
 10#include <sys/types.h>
 11#include <sys/wait.h>
 12#include <unistd.h>
 13
 14#include "util.h"
 15
 16void
 17error(char *errstr, ...) {
 18	va_list ap;
 19	va_start(ap, errstr);
 20	vfprintf(stderr, errstr, ap);
 21	va_end(ap);
 22	exit(1);
 23}
 24
 25static void
 26bad_malloc(unsigned int size)
 27{
 28	fprintf(stderr, "fatal: could not malloc() %d bytes\n",
 29			(int) size);
 30	exit(1);
 31}
 32
 33void *
 34emallocz(unsigned int size)
 35{
 36	void *res = calloc(1, size);
 37	if(!res)
 38		bad_malloc(size);
 39	return res;
 40}
 41
 42void *
 43emalloc(unsigned int size)
 44{
 45	void *res = malloc(size);
 46	if(!res)
 47		bad_malloc(size);
 48	return res;
 49}
 50
 51void *
 52erealloc(void *ptr, unsigned int size)
 53{
 54	void *res = realloc(ptr, size);
 55	if(!res)
 56		bad_malloc(size);
 57	return res;
 58}
 59
 60char *
 61estrdup(const char *str)
 62{
 63	void *res = strdup(str);
 64	if(!res)
 65		bad_malloc(strlen(str));
 66	return res;
 67}
 68
 69void
 70failed_assert(char *a, char *file, int line)
 71{
 72	fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
 73	abort();
 74}
 75
 76void
 77swap(void **p1, void **p2)
 78{
 79	void *tmp = *p1;
 80	*p1 = *p2;
 81	*p2 = tmp;
 82}
 83
 84void
 85spawn(Display *dpy, const char *shell, const char *cmd)
 86{
 87	if(!cmd || !shell)
 88		return;
 89	if(fork() == 0) {
 90		if(fork() == 0) {
 91			if(dpy)
 92				close(ConnectionNumber(dpy));
 93			execl(shell, shell, "-c", cmd, (const char *)0);
 94			fprintf(stderr, "gridwm: execl %s", shell);
 95			perror(" failed");
 96		}
 97		exit (0);
 98	}
 99	wait(0);
100}