• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * sim_progress.c --- simple progress meter
3  *
4  * Copyright (C) 1997, 1998 by Theodore Ts'o and
5  * 	PowerQuest, Inc.
6  *
7  * Copyright (C) 1999, 2000 by Theodore Ts'o
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License.
12  * %End-Header%
13  */
14 
15 #include "config.h"
16 #include "resize2fs.h"
17 
18 struct ext2_sim_progress {
19 	FILE	*f;
20 	char	*label;
21 	int	labelwidth;
22 	int	barwidth;
23 	__u32	maxdone;
24 	__u32	current;
25 	int	shown;
26 	int	flags;
27 };
28 
ext2fs_progress_display(ext2_sim_progmeter prog)29 static errcode_t ext2fs_progress_display(ext2_sim_progmeter prog)
30 {
31 	int	i, width;
32 
33 	fputs(prog->label, prog->f);
34 	width = prog->labelwidth - strlen(prog->label);
35 	while (width-- > 0)
36 		putc(' ', prog->f);
37 	if (prog->labelwidth + prog->barwidth > 80) {
38 		fputs("\n", prog->f);
39 		for (width = prog->labelwidth; width > 0; width--)
40 			putc(' ', prog->f);
41 	}
42 	for (i=0; i < prog->barwidth; i++)
43 		putc('-', prog->f);
44 	for (i=0; i < prog->barwidth; i++)
45 		putc('\b', prog->f);
46 	fflush(prog->f);
47 	return 0;
48 }
49 
50 
ext2fs_progress_update(ext2_sim_progmeter prog,__u32 current)51 void ext2fs_progress_update(ext2_sim_progmeter prog, __u32 current)
52 {
53 	int		old_level, level, num, i;
54 
55 	level = prog->barwidth * current / prog->maxdone;
56 	old_level = prog->barwidth * prog->current / prog->maxdone;
57 	prog->current = current;
58 
59 	num = level - old_level;
60 	if (num == 0)
61 		return;
62 
63 	if (num > 0) {
64 		for (i=0; i < num; i++)
65 			putc('X', prog->f);
66 	} else {
67 		num = -num;
68 		for (i=0; i < num; i++)
69 			putc('\b', prog->f);
70 		for (i=0; i < num; i++)
71 			putc('-', prog->f);
72 		for (i=0; i < num; i++)
73 			putc('\b', prog->f);
74 	}
75 	fflush(prog->f);
76 }
77 
ext2fs_progress_init(ext2_sim_progmeter * ret_prog,const char * label,int labelwidth,int barwidth,__u32 maxdone,int flags)78 errcode_t ext2fs_progress_init(ext2_sim_progmeter *ret_prog,
79 			       const char *label,
80 			       int labelwidth, int barwidth,
81 			       __u32 maxdone, int flags)
82 {
83 	ext2_sim_progmeter	prog;
84 	errcode_t		retval;
85 
86 	retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), &prog);
87 	if (retval)
88 		return retval;
89 	memset(prog, 0, sizeof(struct ext2_sim_progress));
90 
91 	retval = ext2fs_get_mem(strlen(label)+1, &prog->label);
92 	if (retval) {
93 		free(prog);
94 		return retval;
95 	}
96 	strcpy(prog->label, label);
97 	prog->labelwidth = labelwidth;
98 	prog->barwidth = barwidth;
99 	prog->flags = flags;
100 	prog->maxdone = maxdone;
101 	prog->current = 0;
102 	prog->shown = 0;
103 	prog->f = stdout;
104 
105 	*ret_prog = prog;
106 
107 	return ext2fs_progress_display(prog);
108 }
109 
ext2fs_progress_close(ext2_sim_progmeter prog)110 void ext2fs_progress_close(ext2_sim_progmeter prog)
111 {
112 
113 	if (prog->label)
114 		ext2fs_free_mem(&prog->label);
115 	ext2fs_free_mem(&prog);
116 	printf("\n");
117 	return;
118 }
119