/* redirect.c * last revised at 3, Feb. 1994 * * Copyright (C) 1994, All rights reserved. * written by Gyudong Kim(chilly@eleceng.adelaide.edu.au) * */ #include "findhier.h" FILE *INPUT,*OUTPUT; void redirect(mode,fname) char mode; char *fname; { FILE *iof; char buf[BUFSIZE]; char *cmd; (void)strcpy(buf,fname); switch (mode) { case '>': case 'o': if (OUTPUT != stdout) (void)fclose(OUTPUT); if (!strcmp(buf,"stdout")) { OUTPUT = stdout; return; } iof = fopen(buf,"r"); if (iof!=NULL) { (void)fclose(iof); warning("Output file %s exists. append/replace ?",buf); cmd = get_cmd("[a]"); if (cmd[0]=='r') iof = fopen(buf,"w"); else { iof = fopen(buf,"a"); if (iof!=NULL) (void)fprintf(iof,"\n"); } } else iof = fopen(buf,"w"); if (iof==NULL) { warning("Output file %s Permission Denied.\n",buf); OUTPUT = stdout; } else OUTPUT = iof; break; case '<': case 'f': if (INPUT != stdin) (void)fclose(INPUT); if (!strcmp(buf,"stdin")) { INPUT = stdin; return; } iof = fopen(buf,"r"); if (iof==NULL) { warning("Input file %s Cannot be opened.\n",buf); INPUT = stdin; } else INPUT = iof; break; default: break; } } /* redirect.c */