util.c 1.2 KB

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