blob: 9d080cec66eeb478ea569cca2c059d71d1e527ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <string.h>
#include <stdlib.h>
#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;
}
|