1*49cdfc7eSAndroid Build Coastguard Worker /* 2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2012-2016 Cyril Hrubis <[email protected]> 3*49cdfc7eSAndroid Build Coastguard Worker * 4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software: you can redistribute it and/or modify 5*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by 6*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation, either version 2 of the License, or 7*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version. 8*49cdfc7eSAndroid Build Coastguard Worker * 9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful, 10*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of 11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12*49cdfc7eSAndroid Build Coastguard Worker * GNU General Public License for more details. 13*49cdfc7eSAndroid Build Coastguard Worker * 14*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License 15*49cdfc7eSAndroid Build Coastguard Worker * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*49cdfc7eSAndroid Build Coastguard Worker */ 17*49cdfc7eSAndroid Build Coastguard Worker 18*49cdfc7eSAndroid Build Coastguard Worker #ifndef SAFE_FILE_OPS_FN 19*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FILE_OPS_FN 20*49cdfc7eSAndroid Build Coastguard Worker 21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h> 22*49cdfc7eSAndroid Build Coastguard Worker #include <time.h> 23*49cdfc7eSAndroid Build Coastguard Worker 24*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/utime.h" 25*49cdfc7eSAndroid Build Coastguard Worker 26*49cdfc7eSAndroid Build Coastguard Worker /* 27*49cdfc7eSAndroid Build Coastguard Worker * Count number of expected assigned conversions. Any conversion starts with '%'. 28*49cdfc7eSAndroid Build Coastguard Worker * The '%%' matches % and no assignment is done. The %*x matches as x would do but 29*49cdfc7eSAndroid Build Coastguard Worker * the assignment is suppressed. 30*49cdfc7eSAndroid Build Coastguard Worker * 31*49cdfc7eSAndroid Build Coastguard Worker * NOTE: This is not 100% correct for complex scanf strings, but will do for 32*49cdfc7eSAndroid Build Coastguard Worker * all of our intended usage. 33*49cdfc7eSAndroid Build Coastguard Worker */ 34*49cdfc7eSAndroid Build Coastguard Worker int tst_count_scanf_conversions(const char *fmt); 35*49cdfc7eSAndroid Build Coastguard Worker 36*49cdfc7eSAndroid Build Coastguard Worker /* 37*49cdfc7eSAndroid Build Coastguard Worker * All-in-one function to scanf value(s) from a file. 38*49cdfc7eSAndroid Build Coastguard Worker */ 39*49cdfc7eSAndroid Build Coastguard Worker int file_scanf(const char *file, const int lineno, 40*49cdfc7eSAndroid Build Coastguard Worker const char *path, const char *fmt, ...) 41*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (scanf, 4, 5))); 42*49cdfc7eSAndroid Build Coastguard Worker 43*49cdfc7eSAndroid Build Coastguard Worker void safe_file_scanf(const char *file, const int lineno, 44*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), 45*49cdfc7eSAndroid Build Coastguard Worker const char *path, const char *fmt, ...) 46*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (scanf, 5, 6))); 47*49cdfc7eSAndroid Build Coastguard Worker 48*49cdfc7eSAndroid Build Coastguard Worker int file_lines_scanf(const char *file, const int lineno, 49*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), int strict, 50*49cdfc7eSAndroid Build Coastguard Worker const char *path, const char *fmt, ...) 51*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (scanf, 6, 7))); 52*49cdfc7eSAndroid Build Coastguard Worker 53*49cdfc7eSAndroid Build Coastguard Worker /* 54*49cdfc7eSAndroid Build Coastguard Worker * All-in-one function that lets you printf directly into a file. 55*49cdfc7eSAndroid Build Coastguard Worker */ 56*49cdfc7eSAndroid Build Coastguard Worker int file_printf(const char *file, const int lineno, 57*49cdfc7eSAndroid Build Coastguard Worker const char *path, const char *fmt, ...) 58*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (printf, 4, 5))); 59*49cdfc7eSAndroid Build Coastguard Worker 60*49cdfc7eSAndroid Build Coastguard Worker void safe_file_printf(const char *file, const int lineno, 61*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), 62*49cdfc7eSAndroid Build Coastguard Worker const char *path, const char *fmt, ...) 63*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (printf, 5, 6))); 64*49cdfc7eSAndroid Build Coastguard Worker 65*49cdfc7eSAndroid Build Coastguard Worker void safe_try_file_printf(const char *file, const int lineno, 66*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), const char *path, const char *fmt, ...) 67*49cdfc7eSAndroid Build Coastguard Worker __attribute__ ((format (printf, 5, 6))); 68*49cdfc7eSAndroid Build Coastguard Worker 69*49cdfc7eSAndroid Build Coastguard Worker /* 70*49cdfc7eSAndroid Build Coastguard Worker * Safe function to copy files, no more system("cp ...") please. 71*49cdfc7eSAndroid Build Coastguard Worker */ 72*49cdfc7eSAndroid Build Coastguard Worker int safe_cp(const char *file, const int lineno, 73*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), 74*49cdfc7eSAndroid Build Coastguard Worker const char *src, const char *dst); 75*49cdfc7eSAndroid Build Coastguard Worker 76*49cdfc7eSAndroid Build Coastguard Worker /* 77*49cdfc7eSAndroid Build Coastguard Worker * Safe function to touch a file. 78*49cdfc7eSAndroid Build Coastguard Worker * 79*49cdfc7eSAndroid Build Coastguard Worker * If the file (pathname) does not exist It will be created with 80*49cdfc7eSAndroid Build Coastguard Worker * the specified permission (mode) and the access/modification times (times). 81*49cdfc7eSAndroid Build Coastguard Worker * 82*49cdfc7eSAndroid Build Coastguard Worker * If mode is 0 then the file is created with (0666 & ~umask) 83*49cdfc7eSAndroid Build Coastguard Worker * permission or (if the file exists) the permission is not changed. 84*49cdfc7eSAndroid Build Coastguard Worker * 85*49cdfc7eSAndroid Build Coastguard Worker * times is a timespec[2] (as for utimensat(2)). If times is NULL then 86*49cdfc7eSAndroid Build Coastguard Worker * the access/modification times of the file is set to the current time. 87*49cdfc7eSAndroid Build Coastguard Worker */ 88*49cdfc7eSAndroid Build Coastguard Worker int safe_touch(const char *file, const int lineno, 89*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), 90*49cdfc7eSAndroid Build Coastguard Worker const char *pathname, 91*49cdfc7eSAndroid Build Coastguard Worker mode_t mode, const struct timespec times[2]); 92*49cdfc7eSAndroid Build Coastguard Worker 93*49cdfc7eSAndroid Build Coastguard Worker #endif /* SAFE_FILE_OPS_FN */ 94