progress.c revision cf5301d7f2c3bbed3d26600335102414cbf0c4ba
1/*
2 * progress.c - Numeric progress meter
3 *
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5 * 	2003, 2004, 2005 by Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include "ext2fs.h"
14#include "ext2fsP.h"
15
16static char spaces[80], backspaces[80];
17
18static int int_log10(unsigned int arg)
19{
20	int	l;
21
22	for (l=0; arg ; l++)
23		arg = arg / 10;
24	return l;
25}
26
27void ext2fs_numeric_progress_init(ext2_filsys fs,
28				  struct ext2fs_numeric_progress_struct * progress,
29				  const char *label, __u64 max)
30{
31	/*
32	 * The PRINT_PROGRESS flag turns on or off ALL
33	 * progress-related messages, whereas the SKIP_PROGRESS
34	 * environment variable prints the start and end messages but
35	 * not the numeric countdown in the middle.
36	 */
37	if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
38		return;
39
40	memset(spaces, ' ', sizeof(spaces)-1);
41	spaces[sizeof(spaces)-1] = 0;
42	memset(backspaces, '\b', sizeof(backspaces)-1);
43	backspaces[sizeof(backspaces)-1] = 0;
44	progress->skip_progress = 0;
45	if (getenv("E2FSPROGS_SKIP_PROGRESS"))
46		progress->skip_progress++;
47
48	memset(progress, 0, sizeof(*progress));
49
50	/*
51	 * Figure out how many digits we need
52	 */
53	progress->max = max;
54	progress->log_max = int_log10(max);
55
56	if (label) {
57		fputs(label, stdout);
58		fflush(stdout);
59	}
60}
61
62void ext2fs_numeric_progress_update(ext2_filsys fs,
63				    struct ext2fs_numeric_progress_struct * progress,
64				    __u64 val)
65{
66	if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
67		return;
68	if (progress->skip_progress)
69		return;
70
71	printf("%*llu/%*llu", progress->log_max, val,
72	       progress->log_max, progress->max);
73	fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
74}
75
76void ext2fs_numeric_progress_close(ext2_filsys fs,
77				   struct ext2fs_numeric_progress_struct * progress,
78				   const char *message)
79{
80	if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
81		return;
82	fprintf(stdout, "%.*s", (2*progress->log_max)+1, spaces);
83	fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
84	if (message)
85		fputs(message, stdout);
86}
87