dwm-meillo
annotate util.c @ 60:24f9c674d03f
made stdin reader more robust
author | Anselm R. Garbe <garbeam@wmii.de> |
---|---|
date | Fri, 14 Jul 2006 12:08:32 +0200 |
parents | 989178822938 |
children | f14858218641 |
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@43 | 14 #include "dwm.h" |
garbeam@2 | 15 |
garbeam@2 | 16 void |
garbeam@33 | 17 error(const 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@32 | 63 char *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 swap(void **p1, void **p2) |
garbeam@3 | 71 { |
garbeam@3 | 72 void *tmp = *p1; |
garbeam@3 | 73 *p1 = *p2; |
garbeam@3 | 74 *p2 = tmp; |
garbeam@3 | 75 } |
garbeam@5 | 76 |
garbeam@5 | 77 void |
garbeam@49 | 78 spawn(Arg *arg) |
garbeam@5 | 79 { |
garbeam@49 | 80 char **argv = (char **)arg->argv; |
garbeam@14 | 81 if(!argv || !argv[0]) |
garbeam@5 | 82 return; |
garbeam@5 | 83 if(fork() == 0) { |
garbeam@5 | 84 if(fork() == 0) { |
garbeam@5 | 85 if(dpy) |
garbeam@5 | 86 close(ConnectionNumber(dpy)); |
garbeam@9 | 87 setsid(); |
garbeam@14 | 88 execvp(argv[0], argv); |
garbeam@34 | 89 fprintf(stderr, "dwm: execvp %s", argv[0]); |
garbeam@5 | 90 perror(" failed"); |
garbeam@5 | 91 } |
garbeam@5 | 92 exit (0); |
garbeam@5 | 93 } |
garbeam@5 | 94 wait(0); |
garbeam@5 | 95 } |