Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Cyril Hrubis chrubis@suse.cz |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | |
| 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "safe_file_ops.h" |
| 28 | |
| 29 | /* |
| 30 | * Count number of expected assigned conversions. Any conversion starts with '%'. |
| 31 | * The '%%' matches % and no assigment is done. The %*x matches as x would do but |
| 32 | * the assigment is supressed. |
| 33 | * |
| 34 | * NOTE: This is not 100% correct for complex scanf strings, but will do for |
| 35 | * all of our intended usage. |
| 36 | */ |
| 37 | static int count_scanf_conversions(const char *fmt) |
| 38 | { |
| 39 | unsigned int cnt = 0; |
| 40 | int flag = 0; |
| 41 | |
| 42 | while (*fmt) { |
| 43 | switch (*fmt) { |
| 44 | case '%': |
| 45 | if (flag) { |
| 46 | cnt--; |
| 47 | flag = 0; |
| 48 | } else { |
| 49 | flag = 1; |
| 50 | cnt++; |
| 51 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | break; |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 53 | case '*': |
| 54 | if (flag) { |
| 55 | cnt--; |
| 56 | flag = 0; |
| 57 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | break; |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 59 | default: |
| 60 | flag = 0; |
| 61 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 63 | fmt++; |
| 64 | } |
| 65 | |
| 66 | return cnt; |
| 67 | } |
| 68 | |
| 69 | void safe_file_scanf(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 70 | void (*cleanup_fn) (void), |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 71 | const char *path, const char *fmt, ...) |
| 72 | { |
| 73 | va_list va; |
| 74 | FILE *f; |
| 75 | int exp_convs, ret; |
| 76 | |
| 77 | f = fopen(path, "r"); |
| 78 | |
| 79 | if (f == NULL) { |
| 80 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | "Failed to open FILE '%s' for reading at %s:%d", |
| 82 | path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 83 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 85 | exp_convs = count_scanf_conversions(fmt); |
| 86 | |
| 87 | va_start(va, fmt); |
| 88 | ret = vfscanf(f, fmt, va); |
| 89 | va_end(va); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 91 | if (ret == EOF) { |
| 92 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | "The FILE '%s' ended prematurely at %s:%d", |
| 94 | path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (ret != exp_convs) { |
| 98 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | "Expected %i conversions got %i FILE '%s' at %s:%d", |
| 100 | exp_convs, ret, path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } |
| 104 | |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 105 | void safe_file_printf(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | void (*cleanup_fn) (void), |
| 107 | const char *path, const char *fmt, ...) |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 108 | { |
| 109 | va_list va; |
| 110 | FILE *f; |
| 111 | |
| 112 | f = fopen(path, "w"); |
| 113 | |
| 114 | if (f == NULL) { |
| 115 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | "Failed to open FILE '%s' for writing at %s:%d", |
| 117 | path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | va_start(va, fmt); |
| 121 | |
| 122 | if (vfprintf(f, fmt, va) < 0) { |
| 123 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | "Failed to print to FILE '%s' at %s:%d", |
| 125 | path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | va_end(va); |
| 129 | |
| 130 | if (fclose(f)) { |
| 131 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | "Failed to close FILE '%s' at %s:%d", |
| 133 | path, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | //TODO: C implementation? better error condition reporting? |
| 138 | void safe_cp(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | void (*cleanup_fn) (void), const char *src, const char *dst) |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 140 | { |
| 141 | size_t len = strlen(src) + strlen(dst) + 16; |
| 142 | char buf[len]; |
| 143 | int ret; |
| 144 | |
| 145 | snprintf(buf, sizeof(buf), "cp \"%s\" \"%s\"", src, dst); |
| 146 | |
| 147 | ret = system(buf); |
| 148 | |
| 149 | if (ret) { |
| 150 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | "Failed to copy '%s' to '%s' at %s:%d", |
| 152 | src, dst, file, lineno); |
Cyril Hrubis | 4274895 | 2012-11-27 19:26:45 +0100 | [diff] [blame] | 153 | } |
| 154 | } |