Skip to content

Commit 0246e04

Browse files
author
jasonzxpan
committed
Simplify the code; Replace size unit with macro; Repair a possible bug.
1 parent 8f6898e commit 0246e04

File tree

1 file changed

+23
-47
lines changed

1 file changed

+23
-47
lines changed

vmtouch.c

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4444
#define MAX_NUMBER_OF_IGNORES 1024
4545
#define MAX_NUMBER_OF_FILENAME_FILTERS 1024
4646
#define MAX_FILENAME_LENGTH 1024
47+
#define UNIT_K 1024
48+
#define UNIT_M (1024 * 1024)
49+
#define UNIT_G (1024 * 1024 * 1024)
4750

4851
#if defined(__linux__) || (defined(__hpux) && !defined(__LP64__))
4952
// Make sure off_t is 64 bits on linux and when creating 32bit programs
@@ -216,7 +219,7 @@ static void warning(const char *fmt, ...) {
216219
static void reopen_all() {
217220
if (freopen("/dev/null", "r", stdin) == NULL ||
218221
freopen("/dev/null", "w", stdout) == NULL ||
219-
freopen("/dev/null", "w", stdout) == NULL)
222+
freopen("/dev/null", "w", stderr) == NULL)
220223
fatal("freopen: %s", strerror(errno));
221224
}
222225

@@ -267,22 +270,11 @@ void go_daemon() {
267270
char *pretty_print_size(int64_t inp) {
268271
static char output[100];
269272

270-
if (inp<1024) {
271-
snprintf(output, sizeof(output), "%" PRId64, inp);
272-
return output;
273-
}
274-
inp /= 1024;
275-
if (inp<1024) {
276-
snprintf(output, sizeof(output), "%" PRId64 "K", inp);
277-
return output;
278-
}
279-
inp /= 1024;
280-
if (inp<1024) {
281-
snprintf(output, sizeof(output), "%" PRId64 "M", inp);
282-
return output;
283-
}
284-
inp /= 1024;
285-
snprintf(output, sizeof(output), "%" PRId64 "G", inp);
273+
if (inp > UNIT_G) snprintf(output, sizeof(output), "%" PRId64 "G", inp / UNIT_G);
274+
else if (inp > UNIT_M) snprintf(output, sizeof(output), "%" PRId64 "M", inp / UNIT_M);
275+
else if (inp > UNIT_K) snprintf(output, sizeof(output), "%" PRId64 "K", inp / UNIT_K);
276+
else snprintf(output, sizeof(output), "%" PRId64, inp);
277+
286278
return output;
287279
}
288280

@@ -305,9 +297,9 @@ int64_t parse_size(char *inp) {
305297

306298
if (isalpha(mult_char)) {
307299
switch(mult_char) {
308-
case 'k': mult = 1024; break;
309-
case 'm': mult = 1024*1024; break;
310-
case 'g': mult = 1024*1024*1024; break;
300+
case 'k': mult = UNIT_K; break;
301+
case 'm': mult = UNIT_M; break;
302+
case 'g': mult = UNIT_G; break;
311303
default: fatal("unknown size multiplier: %c", mult_char);
312304
}
313305
inp[len-1] = '\0';
@@ -360,9 +352,7 @@ void parse_range(char *inp) {
360352

361353

362354
void parse_ignore_item(char *inp) {
363-
if (inp == NULL) {
364-
return;
365-
}
355+
if (inp == NULL) return;
366356

367357
if (strlen(inp) > MAX_FILENAME_LENGTH) {
368358
fatal("too long pattern provided to -i: %s", inp);
@@ -379,9 +369,7 @@ void parse_ignore_item(char *inp) {
379369
}
380370

381371
void parse_filename_filter_item(char *inp) {
382-
if (inp == NULL) {
383-
return;
384-
}
372+
if (inp == NULL) return;
385373

386374
if (strlen(inp) > MAX_FILENAME_LENGTH) {
387375
fatal("too long pattern provided to -I: %s", inp);
@@ -769,9 +757,7 @@ void vmtouch_crawl(char *path) {
769757

770758
if (path[tp_path_len-1] == '/' && tp_path_len > 1) path[tp_path_len-1] = '\0'; // prevent ugly double slashes when printing path names
771759

772-
if (is_ignored(path)) {
773-
return;
774-
}
760+
if (is_ignored(path)) return;
775761

776762
res = o_followsymlinks ? stat(path, &sb) : lstat(path, &sb);
777763

@@ -788,11 +774,9 @@ void vmtouch_crawl(char *path) {
788774
if (!orig_device_inited) {
789775
orig_device = sb.st_dev;
790776
orig_device_inited = 1;
791-
} else {
792-
if (sb.st_dev != orig_device) {
793-
warning("not recursing into separate filesystem %s", path);
794-
return;
795-
}
777+
} else if (sb.st_dev != orig_device) {
778+
warning("not recursing into separate filesystem %s", path);
779+
return;
796780
}
797781
}
798782

@@ -994,13 +978,9 @@ int main(int argc, char **argv) {
994978
argc -= optind;
995979
argv += optind;
996980

997-
if (o_touch) {
998-
if (o_evict) fatal("invalid option combination: -t and -e");
999-
}
981+
if (o_touch && o_evict) fatal("invalid option combination: -t and -e");
1000982

1001-
if (o_evict) {
1002-
if (o_lock) fatal("invalid option combination: -e and -l");
1003-
}
983+
if (o_evict && o_lock) fatal("invalid option combination: -e and -l");
1004984

1005985
if (o_lock && o_lockall) fatal("invalid option combination: -l and -L");
1006986

@@ -1028,9 +1008,7 @@ int main(int argc, char **argv) {
10281008

10291009
gettimeofday(&start_time, NULL);
10301010

1031-
if (o_batch) {
1032-
vmtouch_batch_crawl(o_batch);
1033-
}
1011+
if (o_batch) vmtouch_batch_crawl(o_batch);
10341012

10351013
for (i=0; i<argc; i++) vmtouch_crawl(argv[i]);
10361014

@@ -1042,10 +1020,8 @@ int main(int argc, char **argv) {
10421020
double elapsed = (end_time.tv_sec - start_time.tv_sec) + (double)(end_time.tv_usec - start_time.tv_usec)/1000000.0;
10431021

10441022
if (o_lock || o_lockall) {
1045-
if (o_lockall) {
1046-
if (mlockall(MCL_CURRENT))
1047-
fatal("unable to mlockall (%s)", strerror(errno));
1048-
}
1023+
if (o_lockall && mlockall(MCL_CURRENT))
1024+
fatal("unable to mlockall (%s)", strerror(errno));
10491025

10501026
if (o_pidfile) {
10511027
register_signals_for_pidfile();

0 commit comments

Comments
 (0)