Configuration file for DWM on MacBook Air
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
2.2 KiB

18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. #include "util.h"
  13. static char *shell = NULL;
  14. void
  15. error(char *errstr, ...) {
  16. va_list ap;
  17. va_start(ap, errstr);
  18. vfprintf(stderr, errstr, ap);
  19. va_end(ap);
  20. exit(1);
  21. }
  22. static void
  23. bad_malloc(unsigned int size)
  24. {
  25. fprintf(stderr, "fatal: could not malloc() %d bytes\n",
  26. (int) size);
  27. exit(1);
  28. }
  29. void *
  30. emallocz(unsigned int size)
  31. {
  32. void *res = calloc(1, size);
  33. if(!res)
  34. bad_malloc(size);
  35. return res;
  36. }
  37. void *
  38. emalloc(unsigned int size)
  39. {
  40. void *res = malloc(size);
  41. if(!res)
  42. bad_malloc(size);
  43. return res;
  44. }
  45. void *
  46. erealloc(void *ptr, unsigned int size)
  47. {
  48. void *res = realloc(ptr, size);
  49. if(!res)
  50. bad_malloc(size);
  51. return res;
  52. }
  53. char *
  54. estrdup(const char *str)
  55. {
  56. void *res = strdup(str);
  57. if(!res)
  58. bad_malloc(strlen(str));
  59. return res;
  60. }
  61. void
  62. failed_assert(char *a, char *file, int line)
  63. {
  64. fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
  65. abort();
  66. }
  67. void
  68. swap(void **p1, void **p2)
  69. {
  70. void *tmp = *p1;
  71. *p1 = *p2;
  72. *p2 = tmp;
  73. }
  74. void
  75. spawn(Display *dpy, const char *cmd)
  76. {
  77. if(!shell && !(shell = getenv("SHELL")))
  78. shell = "/bin/sh";
  79. if(!cmd)
  80. return;
  81. if(fork() == 0) {
  82. if(fork() == 0) {
  83. setsid();
  84. if(dpy)
  85. close(ConnectionNumber(dpy));
  86. execlp(shell, "shell", "-c", cmd, NULL);
  87. fprintf(stderr, "gridwm: execvp %s", cmd);
  88. perror(" failed");
  89. }
  90. exit (0);
  91. }
  92. wait(0);
  93. }
  94. void
  95. pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
  96. {
  97. unsigned int l, n;
  98. int pfd[2];
  99. if(!shell && !(shell = getenv("SHELL")))
  100. shell = "/bin/sh";
  101. if(!cmd)
  102. return;
  103. if(pipe(pfd) == -1) {
  104. perror("pipe");
  105. exit(1);
  106. }
  107. if(fork() == 0) {
  108. setsid();
  109. if(dpy)
  110. close(ConnectionNumber(dpy));
  111. dup2(pfd[1], STDOUT_FILENO);
  112. close(pfd[0]);
  113. close(pfd[1]);
  114. execlp(shell, "shell", "-c", cmd, NULL);
  115. fprintf(stderr, "gridwm: execvp %s", cmd);
  116. perror(" failed");
  117. }
  118. else {
  119. n = 0;
  120. close(pfd[1]);
  121. while(l > n) {
  122. if((l = read(pfd[0], buf + n, len - n)) < 1)
  123. break;
  124. n += l;
  125. }
  126. close(pfd[0]);
  127. buf[n - 1] = 0;
  128. }
  129. wait(0);
  130. }