Configuration of dwm for Mac Computers
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.

140 lines
2.1 KiB

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. void
  14. error(char *errstr, ...) {
  15. va_list ap;
  16. va_start(ap, errstr);
  17. vfprintf(stderr, errstr, ap);
  18. va_end(ap);
  19. exit(1);
  20. }
  21. static void
  22. bad_malloc(unsigned int size)
  23. {
  24. fprintf(stderr, "fatal: could not malloc() %d bytes\n",
  25. (int) size);
  26. exit(1);
  27. }
  28. void *
  29. emallocz(unsigned int size)
  30. {
  31. void *res = calloc(1, size);
  32. if(!res)
  33. bad_malloc(size);
  34. return res;
  35. }
  36. void *
  37. emalloc(unsigned int size)
  38. {
  39. void *res = malloc(size);
  40. if(!res)
  41. bad_malloc(size);
  42. return res;
  43. }
  44. void *
  45. erealloc(void *ptr, unsigned int size)
  46. {
  47. void *res = realloc(ptr, size);
  48. if(!res)
  49. bad_malloc(size);
  50. return res;
  51. }
  52. char *
  53. estrdup(const char *str)
  54. {
  55. void *res = strdup(str);
  56. if(!res)
  57. bad_malloc(strlen(str));
  58. return res;
  59. }
  60. void
  61. failed_assert(char *a, char *file, int line)
  62. {
  63. fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
  64. abort();
  65. }
  66. void
  67. swap(void **p1, void **p2)
  68. {
  69. void *tmp = *p1;
  70. *p1 = *p2;
  71. *p2 = tmp;
  72. }
  73. void
  74. spawn(Display *dpy, char *argv[])
  75. {
  76. if(!argv || !argv[0])
  77. return;
  78. if(fork() == 0) {
  79. if(fork() == 0) {
  80. if(dpy)
  81. close(ConnectionNumber(dpy));
  82. setsid();
  83. execvp(argv[0], argv);
  84. fprintf(stderr, "gridwm: execvp %s", argv[0]);
  85. perror(" failed");
  86. }
  87. exit (0);
  88. }
  89. wait(0);
  90. }
  91. void
  92. pipe_spawn(char *buf, unsigned int len, Display *dpy, char *argv[])
  93. {
  94. unsigned int l, n;
  95. int pfd[2];
  96. if(!argv || !argv[0])
  97. return;
  98. if(pipe(pfd) == -1) {
  99. perror("pipe");
  100. exit(1);
  101. }
  102. if(fork() == 0) {
  103. if(dpy)
  104. close(ConnectionNumber(dpy));
  105. setsid();
  106. dup2(pfd[1], STDOUT_FILENO);
  107. close(pfd[0]);
  108. close(pfd[1]);
  109. execvp(argv[0], argv);
  110. fprintf(stderr, "gridwm: execvp %s", argv[0]);
  111. perror(" failed");
  112. }
  113. else {
  114. n = 0;
  115. close(pfd[1]);
  116. while(l > n) {
  117. if((l = read(pfd[0], buf + n, len - n)) < 1)
  118. break;
  119. n += l;
  120. }
  121. close(pfd[0]);
  122. buf[n - 1] = 0;
  123. }
  124. wait(0);
  125. }