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