grocat.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /****************************************************************************
  2. *
  3. * MODULE: grocat
  4. * AUTHOR(S): Paul Kelly
  5. * PURPOSE: Copies stdin to stdout in line-buffered mode until end
  6. * of file is received.
  7. * Used with Tcl/Tk gronsole system to merge stdout and
  8. * stderr streams to be caught by Tcl "open" command.
  9. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. int main(void)
  19. {
  20. int inchar, outchar;
  21. char inbuff[1024], outbuff[1024];
  22. /* stdin and stdout both line-buffered */
  23. if (setvbuf(stdin, inbuff, _IOLBF, sizeof(inbuff))) {
  24. fprintf(stderr, "grocat: Can't set stdin to line-buffered mode!\n");
  25. exit(EXIT_FAILURE);
  26. }
  27. if (setvbuf(stdout, outbuff, _IOLBF, sizeof(outbuff))) {
  28. fprintf(stderr, "grocat: Can't set stdout to line-buffered mode!\n");
  29. exit(EXIT_FAILURE);
  30. }
  31. while ((inchar = getc(stdin)) != EOF) {
  32. /* Read a character at a time from stdin until EOF
  33. * and copy to stdout */
  34. outchar = putc(inchar, stdout);
  35. if (outchar != inchar) {
  36. fprintf(stderr, "grocat: Error writing to stdout!\n");
  37. exit(EXIT_FAILURE);
  38. }
  39. }
  40. /* Flush in case last line wasn't terminated properly or something */
  41. fflush(stdout);
  42. exit(EXIT_SUCCESS);
  43. }