util.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. /* extern */
  11. void *
  12. emallocz(unsigned int size) {
  13. void *res = calloc(1, size);
  14. if(!res)
  15. eprint("fatal: could not malloc() %u bytes\n", size);
  16. return res;
  17. }
  18. void
  19. eprint(const char *errstr, ...) {
  20. va_list ap;
  21. va_start(ap, errstr);
  22. vfprintf(stderr, errstr, ap);
  23. va_end(ap);
  24. exit(EXIT_FAILURE);
  25. }
  26. void *
  27. erealloc(void *ptr, unsigned int size) {
  28. void *res = realloc(ptr, size);
  29. if(!res)
  30. eprint("fatal: could not malloc() %u bytes\n", size);
  31. return res;
  32. }
  33. void
  34. spawn(Arg *arg) {
  35. static char *shell = NULL;
  36. if(!shell && !(shell = getenv("SHELL")))
  37. shell = "/bin/sh";
  38. if(!arg->cmd)
  39. return;
  40. /* The double-fork construct avoids zombie processes and keeps the code
  41. * clean from stupid signal handlers. */
  42. if(fork() == 0) {
  43. if(fork() == 0) {
  44. if(dpy)
  45. close(ConnectionNumber(dpy));
  46. setsid();
  47. execl(shell, shell, "-c", arg->cmd, (char *)NULL);
  48. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
  49. perror(" failed");
  50. }
  51. exit(0);
  52. }
  53. wait(0);
  54. }