| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #include "monetdb_config.h" |
| 10 | #include "helpers.h" |
| 11 | #include <ctype.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | #ifndef DIR_SEP |
| 15 | # define DIR_SEP '/' |
| 16 | #endif |
| 17 | |
| 18 | void |
| 19 | ErrXit(char *text1, char *text2, int num) |
| 20 | { |
| 21 | fprintf(stderr, "ERROR: %s%s\n" , text1, text2); |
| 22 | exit(num); |
| 23 | } |
| 24 | |
| 25 | /* ErrXit */ |
| 26 | |
| 27 | |
| 28 | FILE * |
| 29 | Rfopen(char *name) |
| 30 | { |
| 31 | FILE *fp; |
| 32 | |
| 33 | if (!strcmp(name, "-" )) |
| 34 | fp = stdin; |
| 35 | else if (!(fp = fopen(name, "r" ))) |
| 36 | ErrXit("could not read file " , name, 1); |
| 37 | return fp; |
| 38 | } |
| 39 | |
| 40 | /* Rfopen */ |
| 41 | |
| 42 | |
| 43 | FILE * |
| 44 | Wfopen(char *name) |
| 45 | { |
| 46 | FILE *fp; |
| 47 | |
| 48 | if (!strcmp(name, "-" )) |
| 49 | fp = stdout; |
| 50 | else if (!(fp = fopen(name, "w" ))) |
| 51 | ErrXit("could not write file " , name, 1); |
| 52 | return fp; |
| 53 | } |
| 54 | |
| 55 | /* Wfopen */ |
| 56 | |
| 57 | |
| 58 | FILE * |
| 59 | Afopen(char *name) |
| 60 | { |
| 61 | FILE *fp; |
| 62 | |
| 63 | if (!strcmp(name, "-" )) |
| 64 | fp = stdout; |
| 65 | else if (!(fp = fopen(name, "a" ))) |
| 66 | ErrXit("could not append file " , name, 1); |
| 67 | return fp; |
| 68 | } |
| 69 | |
| 70 | /* Afopen */ |
| 71 | |
| 72 | |
| 73 | char * |
| 74 | filename(char *path) |
| 75 | { |
| 76 | char *fn = strrchr(path, (int) DIR_SEP); |
| 77 | |
| 78 | if (fn) |
| 79 | return (fn + 1); |
| 80 | else |
| 81 | return path; |
| 82 | } |
| 83 | |
| 84 | #if DIR_SEP == '/' |
| 85 | char *default_tmpdir = "/tmp" ; |
| 86 | |
| 87 | #define TMPDIR_ENV "TMPDIR" |
| 88 | #else |
| 89 | char *default_tmpdir = "C:\\Temp" ; |
| 90 | |
| 91 | #define TMPDIR_ENV "TEMP" |
| 92 | #endif |
| 93 | |
| 94 | char * |
| 95 | tmpdir(void) |
| 96 | { |
| 97 | char *rtrn = getenv(TMPDIR_ENV); |
| 98 | |
| 99 | if (!rtrn) |
| 100 | rtrn = default_tmpdir; |
| 101 | return rtrn; |
| 102 | } |
| 103 | |