aewl

annotate util.c @ 9:d567f430a81d

fixed several stuff (gridwm gets better and better)
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 12:52:57 +0200
parents e0cefb3981c8
children ea9c08ec4b48
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@6 16 static char *shell = NULL;
garbeam@6 17
garbeam@2 18 void
garbeam@2 19 error(char *errstr, ...) {
garbeam@2 20 va_list ap;
garbeam@2 21 va_start(ap, errstr);
garbeam@2 22 vfprintf(stderr, errstr, ap);
garbeam@2 23 va_end(ap);
garbeam@2 24 exit(1);
garbeam@2 25 }
garbeam@2 26
garbeam@3 27 static void
garbeam@3 28 bad_malloc(unsigned int size)
garbeam@3 29 {
garbeam@3 30 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
garbeam@3 31 (int) size);
garbeam@3 32 exit(1);
garbeam@3 33 }
garbeam@3 34
garbeam@3 35 void *
garbeam@3 36 emallocz(unsigned int size)
garbeam@3 37 {
garbeam@3 38 void *res = calloc(1, size);
garbeam@3 39 if(!res)
garbeam@3 40 bad_malloc(size);
garbeam@3 41 return res;
garbeam@3 42 }
garbeam@3 43
garbeam@3 44 void *
garbeam@3 45 emalloc(unsigned int size)
garbeam@3 46 {
garbeam@3 47 void *res = malloc(size);
garbeam@3 48 if(!res)
garbeam@3 49 bad_malloc(size);
garbeam@3 50 return res;
garbeam@3 51 }
garbeam@3 52
garbeam@3 53 void *
garbeam@3 54 erealloc(void *ptr, unsigned int size)
garbeam@3 55 {
garbeam@3 56 void *res = realloc(ptr, size);
garbeam@3 57 if(!res)
garbeam@3 58 bad_malloc(size);
garbeam@3 59 return res;
garbeam@3 60 }
garbeam@3 61
garbeam@3 62 char *
garbeam@3 63 estrdup(const char *str)
garbeam@3 64 {
garbeam@3 65 void *res = strdup(str);
garbeam@3 66 if(!res)
garbeam@3 67 bad_malloc(strlen(str));
garbeam@3 68 return res;
garbeam@3 69 }
garbeam@3 70
garbeam@3 71 void
garbeam@3 72 failed_assert(char *a, char *file, int line)
garbeam@3 73 {
garbeam@3 74 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
garbeam@3 75 abort();
garbeam@3 76 }
garbeam@3 77
garbeam@3 78 void
garbeam@3 79 swap(void **p1, void **p2)
garbeam@3 80 {
garbeam@3 81 void *tmp = *p1;
garbeam@3 82 *p1 = *p2;
garbeam@3 83 *p2 = tmp;
garbeam@3 84 }
garbeam@5 85
garbeam@5 86 void
garbeam@6 87 spawn(Display *dpy, const char *cmd)
garbeam@5 88 {
garbeam@6 89 if(!shell && !(shell = getenv("SHELL")))
garbeam@6 90 shell = "/bin/sh";
garbeam@6 91
garbeam@6 92 if(!cmd)
garbeam@5 93 return;
garbeam@5 94 if(fork() == 0) {
garbeam@5 95 if(fork() == 0) {
garbeam@5 96 if(dpy)
garbeam@5 97 close(ConnectionNumber(dpy));
garbeam@9 98 setsid();
garbeam@9 99 fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd);
garbeam@9 100 execlp(shell, shell, "-c", cmd, NULL);
garbeam@9 101 fprintf(stderr, "gridwm: execlp %s", cmd);
garbeam@5 102 perror(" failed");
garbeam@5 103 }
garbeam@5 104 exit (0);
garbeam@5 105 }
garbeam@5 106 wait(0);
garbeam@5 107 }
garbeam@6 108
garbeam@6 109 void
garbeam@6 110 pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
garbeam@6 111 {
garbeam@6 112 unsigned int l, n;
garbeam@6 113 int pfd[2];
garbeam@6 114
garbeam@6 115 if(!shell && !(shell = getenv("SHELL")))
garbeam@6 116 shell = "/bin/sh";
garbeam@6 117
garbeam@6 118 if(!cmd)
garbeam@6 119 return;
garbeam@6 120
garbeam@6 121 if(pipe(pfd) == -1) {
garbeam@6 122 perror("pipe");
garbeam@6 123 exit(1);
garbeam@6 124 }
garbeam@6 125
garbeam@6 126 if(fork() == 0) {
garbeam@6 127 if(dpy)
garbeam@6 128 close(ConnectionNumber(dpy));
garbeam@9 129 setsid();
garbeam@6 130 dup2(pfd[1], STDOUT_FILENO);
garbeam@6 131 close(pfd[0]);
garbeam@6 132 close(pfd[1]);
garbeam@9 133 execlp(shell, shell, "-c", cmd, NULL);
garbeam@9 134 fprintf(stderr, "gridwm: execlp %s", cmd);
garbeam@6 135 perror(" failed");
garbeam@6 136 }
garbeam@6 137 else {
garbeam@6 138 n = 0;
garbeam@6 139 close(pfd[1]);
garbeam@6 140 while(l > n) {
garbeam@6 141 if((l = read(pfd[0], buf + n, len - n)) < 1)
garbeam@6 142 break;
garbeam@6 143 n += l;
garbeam@6 144 }
garbeam@6 145 close(pfd[0]);
garbeam@6 146 buf[n - 1] = 0;
garbeam@6 147 }
garbeam@6 148 wait(0);
garbeam@6 149 }