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
17 static char spaces[80], backspaces[80];
19 static int int_log10(unsigned int arg)
28 void ext2fs_numeric_progress_init(ext2_filsys fs,
29 struct ext2fs_numeric_progress_struct * progress,
30 const char *label, __u64 max)
33 * The PRINT_PROGRESS flag turns on or off ALL
34 * progress-related messages, whereas the SKIP_PROGRESS
35 * environment variable prints the start and end messages but
36 * not the numeric countdown in the middle.
38 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
41 memset(spaces, ' ', sizeof(spaces)-1);
42 spaces[sizeof(spaces)-1] = 0;
43 memset(backspaces, '\b', sizeof(backspaces)-1);
44 backspaces[sizeof(backspaces)-1] = 0;
45 progress->skip_progress = 0;
46 if (getenv("E2FSPROGS_SKIP_PROGRESS"))
47 progress->skip_progress++;
49 memset(progress, 0, sizeof(*progress));
52 * Figure out how many digits we need
55 progress->log_max = int_log10(max);
63 void ext2fs_numeric_progress_update(ext2_filsys fs,
64 struct ext2fs_numeric_progress_struct * progress,
67 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
69 if (progress->skip_progress)
72 printf("%*llu/%*llu", progress->log_max, val,
73 progress->log_max, progress->max);
74 fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
77 void ext2fs_numeric_progress_close(ext2_filsys fs,
78 struct ext2fs_numeric_progress_struct * progress,
81 if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
83 fprintf(stdout, "%.*s", (2*progress->log_max)+1, spaces);
84 fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
86 fputs(message, stdout);