2 * progress.c - Numeric progress meter
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 2003, 2004, 2005 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
19 static char spaces[80], backspaces[80];
20 static time_t last_update;
22 struct ext2fs_progress_ops ext2fs_numeric_progress_ops = {
23 .init = ext2fs_numeric_progress_init,
24 .update = ext2fs_numeric_progress_update,
25 .close = ext2fs_numeric_progress_close,
28 static int int_log10(unsigned int arg)
37 void ext2fs_numeric_progress_init(ext2_filsys fs,
38 struct ext2fs_numeric_progress_struct * progress,
39 const char *label, __u64 max)
42 * The PRINT_PROGRESS flag turns on or off ALL
43 * progress-related messages, whereas the SKIP_PROGRESS
44 * environment variable prints the start and end messages but
45 * not the numeric countdown in the middle.
47 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
50 memset(spaces, ' ', sizeof(spaces)-1);
51 spaces[sizeof(spaces)-1] = 0;
52 memset(backspaces, '\b', sizeof(backspaces)-1);
53 backspaces[sizeof(backspaces)-1] = 0;
55 memset(progress, 0, sizeof(*progress));
56 if (getenv("E2FSPROGS_SKIP_PROGRESS"))
57 progress->skip_progress++;
61 * Figure out how many digits we need
64 progress->log_max = int_log10(max);
73 void ext2fs_numeric_progress_update(ext2_filsys fs,
74 struct ext2fs_numeric_progress_struct * progress,
79 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
81 if (progress->skip_progress)
84 if (now == last_update)
88 printf("%*llu/%*llu", progress->log_max, val,
89 progress->log_max, progress->max);
90 fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
93 void ext2fs_numeric_progress_close(ext2_filsys fs,
94 struct ext2fs_numeric_progress_struct * progress,
97 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
99 fprintf(stdout, "%.*s", (2*progress->log_max)+1, spaces);
100 fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
102 fputs(message, stdout);