aewl

annotate util.c @ 7:49e2fc9fb94f

new stuff
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 11:27:56 +0200
parents e5018cae273f
children d567f430a81d
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@6 96 setsid();
garbeam@5 97 if(dpy)
garbeam@5 98 close(ConnectionNumber(dpy));
garbeam@6 99 execlp(shell, "shell", "-c", cmd, NULL);
garbeam@6 100 fprintf(stderr, "gridwm: execvp %s", cmd);
garbeam@5 101 perror(" failed");
garbeam@5 102 }
garbeam@5 103 exit (0);
garbeam@5 104 }
garbeam@5 105 wait(0);
garbeam@5 106 }
garbeam@6 107
garbeam@6 108 void
garbeam@6 109 pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
garbeam@6 110 {
garbeam@6 111 unsigned int l, n;
garbeam@6 112 int pfd[2];
garbeam@6 113
garbeam@6 114 if(!shell && !(shell = getenv("SHELL")))
garbeam@6 115 shell = "/bin/sh";
garbeam@6 116
garbeam@6 117 if(!cmd)
garbeam@6 118 return;
garbeam@6 119
garbeam@6 120 if(pipe(pfd) == -1) {
garbeam@6 121 perror("pipe");
garbeam@6 122 exit(1);
garbeam@6 123 }
garbeam@6 124
garbeam@6 125 if(fork() == 0) {
garbeam@6 126 setsid();
garbeam@6 127 if(dpy)
garbeam@6 128 close(ConnectionNumber(dpy));
garbeam@6 129 dup2(pfd[1], STDOUT_FILENO);
garbeam@6 130 close(pfd[0]);
garbeam@6 131 close(pfd[1]);
garbeam@6 132 execlp(shell, "shell", "-c", cmd, NULL);
garbeam@6 133 fprintf(stderr, "gridwm: execvp %s", cmd);
garbeam@6 134 perror(" failed");
garbeam@6 135 }
garbeam@6 136 else {
garbeam@6 137 n = 0;
garbeam@6 138 close(pfd[1]);
garbeam@6 139 while(l > n) {
garbeam@6 140 if((l = read(pfd[0], buf + n, len - n)) < 1)
garbeam@6 141 break;
garbeam@6 142 n += l;
garbeam@6 143 }
garbeam@6 144 close(pfd[0]);
garbeam@6 145 buf[n - 1] = 0;
garbeam@6 146 }
garbeam@6 147 wait(0);
garbeam@6 148 }