#include #include #include "fileutils.h" // direction is either false=input or true=output and used to overload parameter '-' FILE *fd_magic(char *str, bool direction) { static bool usedDir[2] = { false, false }; // check if stdin/-out is wanted if (strcmp(str, "-") == 0) { // check if we already used this if (usedDir[direction]) { fprintf(stderr, "stdin/-out cannot be used twice\n"); exit(-1); } // mark that we used it and return in usedDir[direction] = true; return direction ? stdout : stdin; } // check if stdout is wanted if (strcmp(str, "0") == 0) { // replace filename and proceed str = "/dev/null"; } // open file traditionally FILE *fd = fopen(str, direction ? "w" : "r" ); if (fd == NULL) { fprintf(stderr, "Failed to open %s\n", str); exit(-1); } return fd; }