popen.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #ifdef __MINGW32__
  7. # include <io.h>
  8. # include <fcntl.h>
  9. # include <process.h>
  10. #endif
  11. #include <grass/gis.h>
  12. #define READ 0
  13. #define WRITE 1
  14. FILE *G_popen(const char *cmd, const char *mode)
  15. {
  16. #ifndef __MINGW32__
  17. return popen(cmd, mode);
  18. #else
  19. int thepipes[2];
  20. FILE *rv = NULL;
  21. fflush(stdout);
  22. fflush(stderr);
  23. /*setvbuf ( stdout, NULL, _IONBF, 0 ); */
  24. if (_pipe(thepipes, 256, O_BINARY) != -1) {
  25. execl("cmd", "cmd", "/c", cmd, (char *)NULL);
  26. close(thepipes[WRITE]);
  27. rv = fdopen(thepipes[READ], mode);
  28. }
  29. return rv;
  30. #endif
  31. }
  32. int G_pclose(FILE *ptr)
  33. {
  34. #ifndef __MINGW32__
  35. return pclose(ptr);
  36. #else
  37. RETSIGTYPE(*sigint)(int);
  38. int status;
  39. int f;
  40. f = fileno(ptr);
  41. fclose(ptr);
  42. sigint = signal(SIGINT, SIG_IGN);
  43. _cwait(&status, popen_pid[f], WAIT_CHILD);
  44. if (0 & status) {
  45. status = -1;
  46. }
  47. signal(SIGINT, sigint);
  48. return status;
  49. #endif
  50. }