diff --git a/vmtouch.c b/vmtouch.c index 8accef3..f976346 100644 --- a/vmtouch.c +++ b/vmtouch.c @@ -44,6 +44,9 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define MAX_NUMBER_OF_IGNORES 1024 #define MAX_NUMBER_OF_FILENAME_FILTERS 1024 #define MAX_FILENAME_LENGTH 1024 +#define UNIT_K 1024 +#define UNIT_M (1024 * 1024) +#define UNIT_G (1024 * 1024 * 1024) #if defined(__linux__) || (defined(__hpux) && !defined(__LP64__)) // Make sure off_t is 64 bits on linux and when creating 32bit programs @@ -216,7 +219,7 @@ static void warning(const char *fmt, ...) { static void reopen_all() { if (freopen("/dev/null", "r", stdin) == NULL || freopen("/dev/null", "w", stdout) == NULL || - freopen("/dev/null", "w", stdout) == NULL) + freopen("/dev/null", "w", stderr) == NULL) fatal("freopen: %s", strerror(errno)); } @@ -267,22 +270,11 @@ void go_daemon() { char *pretty_print_size(int64_t inp) { static char output[100]; - if (inp<1024) { - snprintf(output, sizeof(output), "%" PRId64, inp); - return output; - } - inp /= 1024; - if (inp<1024) { - snprintf(output, sizeof(output), "%" PRId64 "K", inp); - return output; - } - inp /= 1024; - if (inp<1024) { - snprintf(output, sizeof(output), "%" PRId64 "M", inp); - return output; - } - inp /= 1024; - snprintf(output, sizeof(output), "%" PRId64 "G", inp); + if (inp >= UNIT_G) snprintf(output, sizeof(output), "%" PRId64 "G", inp / UNIT_G); + else if (inp >= UNIT_M) snprintf(output, sizeof(output), "%" PRId64 "M", inp / UNIT_M); + else if (inp >= UNIT_K) snprintf(output, sizeof(output), "%" PRId64 "K", inp / UNIT_K); + else snprintf(output, sizeof(output), "%" PRId64, inp); + return output; } @@ -305,9 +297,9 @@ int64_t parse_size(char *inp) { if (isalpha(mult_char)) { switch(mult_char) { - case 'k': mult = 1024; break; - case 'm': mult = 1024*1024; break; - case 'g': mult = 1024*1024*1024; break; + case 'k': mult = UNIT_K; break; + case 'm': mult = UNIT_M; break; + case 'g': mult = UNIT_G; break; default: fatal("unknown size multiplier: %c", mult_char); } inp[len-1] = '\0'; @@ -360,9 +352,7 @@ void parse_range(char *inp) { void parse_ignore_item(char *inp) { - if (inp == NULL) { - return; - } + if (inp == NULL) return; if (strlen(inp) > MAX_FILENAME_LENGTH) { fatal("too long pattern provided to -i: %s", inp); @@ -379,9 +369,7 @@ void parse_ignore_item(char *inp) { } void parse_filename_filter_item(char *inp) { - if (inp == NULL) { - return; - } + if (inp == NULL) return; if (strlen(inp) > MAX_FILENAME_LENGTH) { fatal("too long pattern provided to -I: %s", inp); @@ -769,9 +757,7 @@ void vmtouch_crawl(char *path) { if (path[tp_path_len-1] == '/' && tp_path_len > 1) path[tp_path_len-1] = '\0'; // prevent ugly double slashes when printing path names - if (is_ignored(path)) { - return; - } + if (is_ignored(path)) return; res = o_followsymlinks ? stat(path, &sb) : lstat(path, &sb); @@ -788,11 +774,9 @@ void vmtouch_crawl(char *path) { if (!orig_device_inited) { orig_device = sb.st_dev; orig_device_inited = 1; - } else { - if (sb.st_dev != orig_device) { - warning("not recursing into separate filesystem %s", path); - return; - } + } else if (sb.st_dev != orig_device) { + warning("not recursing into separate filesystem %s", path); + return; } } @@ -994,13 +978,9 @@ int main(int argc, char **argv) { argc -= optind; argv += optind; - if (o_touch) { - if (o_evict) fatal("invalid option combination: -t and -e"); - } + if (o_touch && o_evict) fatal("invalid option combination: -t and -e"); - if (o_evict) { - if (o_lock) fatal("invalid option combination: -e and -l"); - } + if (o_evict && o_lock) fatal("invalid option combination: -e and -l"); if (o_lock && o_lockall) fatal("invalid option combination: -l and -L"); @@ -1028,9 +1008,7 @@ int main(int argc, char **argv) { gettimeofday(&start_time, NULL); - if (o_batch) { - vmtouch_batch_crawl(o_batch); - } + if (o_batch) vmtouch_batch_crawl(o_batch); for (i=0; i