aewl

annotate util.c @ 5:e5018cae273f

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