GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /**************************************************************************** 00003 * 00004 * MODULE: grocat 00005 * AUTHOR(S): Paul Kelly 00006 * PURPOSE: Copies stdin to stdout in line-buffered mode until end 00007 * of file is received. 00008 * Used with Tcl/Tk gronsole system to merge stdout and 00009 * stderr streams to be caught by Tcl "open" command. 00010 * COPYRIGHT: (C) 2006 by the GRASS Development Team 00011 * 00012 * This program is free software under the GNU General Public 00013 * License (>=v2). Read the file COPYING that comes with GRASS 00014 * for details. 00015 * 00016 *****************************************************************************/ 00017 00018 #include <stdio.h> 00019 #include <stdlib.h> 00020 00021 int main(void) 00022 { 00023 int inchar, outchar; 00024 char inbuff[1024], outbuff[1024]; 00025 00026 /* stdin and stdout both line-buffered */ 00027 if (setvbuf(stdin, inbuff, _IOLBF, sizeof(inbuff))) { 00028 fprintf(stderr, "grocat: Can't set stdin to line-buffered mode!\n"); 00029 exit(EXIT_FAILURE); 00030 } 00031 if (setvbuf(stdout, outbuff, _IOLBF, sizeof(outbuff))) { 00032 fprintf(stderr, "grocat: Can't set stdout to line-buffered mode!\n"); 00033 exit(EXIT_FAILURE); 00034 } 00035 00036 while ((inchar = getc(stdin)) != EOF) { 00037 /* Read a character at a time from stdin until EOF 00038 * and copy to stdout */ 00039 outchar = putc(inchar, stdout); 00040 if (outchar != inchar) { 00041 fprintf(stderr, "grocat: Error writing to stdout!\n"); 00042 exit(EXIT_FAILURE); 00043 } 00044 } 00045 00046 /* Flush in case last line wasn't terminated properly or something */ 00047 fflush(stdout); 00048 00049 exit(EXIT_SUCCESS); 00050 }