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.

149 lines
2.3 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. if(dpy)
  84. close(ConnectionNumber(dpy));
  85. setsid();
  86. fprintf(stderr, "gridwm: execlp %s %s -c %s", shell, shell, cmd);
  87. execlp(shell, shell, "-c", cmd, NULL);
  88. fprintf(stderr, "gridwm: execlp %s", cmd);
  89. perror(" failed");
  90. }
  91. exit (0);
  92. }
  93. wait(0);
  94. }
  95. void
  96. pipe_spawn(char *buf, unsigned int len, Display *dpy, const char *cmd)
  97. {
  98. unsigned int l, n;
  99. int pfd[2];
  100. if(!shell && !(shell = getenv("SHELL")))
  101. shell = "/bin/sh";
  102. if(!cmd)
  103. return;
  104. if(pipe(pfd) == -1) {
  105. perror("pipe");
  106. exit(1);
  107. }
  108. if(fork() == 0) {
  109. if(dpy)
  110. close(ConnectionNumber(dpy));
  111. setsid();
  112. dup2(pfd[1], STDOUT_FILENO);
  113. close(pfd[0]);
  114. close(pfd[1]);
  115. execlp(shell, shell, "-c", cmd, NULL);
  116. fprintf(stderr, "gridwm: execlp %s", cmd);
  117. perror(" failed");
  118. }
  119. else {
  120. n = 0;
  121. close(pfd[1]);
  122. while(l > n) {
  123. if((l = read(pfd[0], buf + n, len - n)) < 1)
  124. break;
  125. n += l;
  126. }
  127. close(pfd[0]);
  128. buf[n - 1] = 0;
  129. }
  130. wait(0);
  131. }