util.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /* static */
  12. static void
  13. badmalloc(unsigned int size)
  14. {
  15. eprint("fatal: could not malloc() %u bytes\n", size);
  16. }
  17. /* extern */
  18. void *
  19. emallocz(unsigned int size)
  20. {
  21. void *res = calloc(1, size);
  22. if(!res)
  23. badmalloc(size);
  24. return res;
  25. }
  26. void
  27. eprint(const char *errstr, ...)
  28. {
  29. va_list ap;
  30. va_start(ap, errstr);
  31. vfprintf(stderr, errstr, ap);
  32. va_end(ap);
  33. exit(EXIT_FAILURE);
  34. }
  35. void *
  36. erealloc(void *ptr, unsigned int size)
  37. {
  38. void *res = realloc(ptr, size);
  39. if(!res)
  40. badmalloc(size);
  41. return res;
  42. }
  43. void
  44. spawn(Arg *arg)
  45. {
  46. static char *shell = NULL;
  47. if(!shell && !(shell = getenv("SHELL")))
  48. shell = "/bin/sh";
  49. if(!arg->cmd)
  50. return;
  51. if(fork() == 0) {
  52. if(fork() == 0) {
  53. if(dpy)
  54. close(ConnectionNumber(dpy));
  55. setsid();
  56. execl(shell, shell, "-c", arg->cmd, NULL);
  57. fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg->cmd);
  58. perror(" failed");
  59. }
  60. exit(0);
  61. }
  62. wait(0);
  63. }