Whamcloud - gitweb
e2fsck: add support for xattrs in external inodes
[tools/e2fsprogs.git] / lib / ext2fs / progress.c
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 "config.h"
14 #include "ext2fs.h"
15 #include "ext2fsP.h"
16
17 static char spaces[80], backspaces[80];
18
19 static int int_log10(unsigned int arg)
20 {
21         int     l;
22
23         for (l=0; arg ; l++)
24                 arg = arg / 10;
25         return l;
26 }
27
28 void ext2fs_numeric_progress_init(ext2_filsys fs,
29                                   struct ext2fs_numeric_progress_struct * progress,
30                                   const char *label, __u64 max)
31 {
32         /*
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.
37          */
38         if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
39                 return;
40
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++;
48
49         memset(progress, 0, sizeof(*progress));
50
51         /*
52          * Figure out how many digits we need
53          */
54         progress->max = max;
55         progress->log_max = int_log10(max);
56
57         if (label) {
58                 fputs(label, stdout);
59                 fflush(stdout);
60         }
61 }
62
63 void ext2fs_numeric_progress_update(ext2_filsys fs,
64                                     struct ext2fs_numeric_progress_struct * progress,
65                                     __u64 val)
66 {
67         if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
68                 return;
69         if (progress->skip_progress)
70                 return;
71
72         printf("%*llu/%*llu", progress->log_max, val,
73                progress->log_max, progress->max);
74         fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
75 }
76
77 void ext2fs_numeric_progress_close(ext2_filsys fs,
78                                    struct ext2fs_numeric_progress_struct * progress,
79                                    const char *message)
80 {
81         if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
82                 return;
83         fprintf(stdout, "%.*s", (2*progress->log_max)+1, spaces);
84         fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
85         if (message)
86                 fputs(message, stdout);
87 }