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 Theosore 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 "resize2fs.h" 16 17 struct ext2_sim_progress { 18 FILE *f; 19 char *label; 20 int labelwidth; 21 int barwidth; 22 __u32 maxdone; 23 __u32 current; 24 int shown; 25 int flags; 26 }; 27 28 static errcode_t ext2fs_progress_display(ext2_sim_progmeter prog) 29 { 30 int i, width; 31 32 fputs(prog->label, prog->f); 33 width = prog->labelwidth - strlen(prog->label); 34 while (width-- > 0) 35 putc(' ', prog->f); 36 if (prog->labelwidth + prog->barwidth > 80) { 37 fputs("\n", prog->f); 38 for (width = prog->labelwidth; width > 0; width--) 39 putc(' ', prog->f); 40 } 41 for (i=0; i < prog->barwidth; i++) 42 putc('-', prog->f); 43 for (i=0; i < prog->barwidth; i++) 44 putc('\b', prog->f); 45 fflush(prog->f); 46 return 0; 47 } 48 49 50 void ext2fs_progress_update(ext2_sim_progmeter prog, __u32 current) 51 { 52 int old_level, level, num, i; 53 54 level = prog->barwidth * current / prog->maxdone; 55 old_level = prog->barwidth * prog->current / prog->maxdone; 56 prog->current = current; 57 58 num = level - old_level; 59 if (num == 0) 60 return; 61 62 if (num > 0) { 63 for (i=0; i < num; i++) 64 putc('X', prog->f); 65 } else { 66 num = -num; 67 for (i=0; i < num; i++) 68 putc('\b', prog->f); 69 for (i=0; i < num; i++) 70 putc('-', prog->f); 71 for (i=0; i < num; i++) 72 putc('\b', prog->f); 73 } 74 fflush(prog->f); 75 } 76 77 errcode_t ext2fs_progress_init(ext2_sim_progmeter *ret_prog, 78 const char *label, 79 int labelwidth, int barwidth, 80 __u32 maxdone, int flags) 81 { 82 ext2_sim_progmeter prog; 83 errcode_t retval; 84 85 retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), &prog); 86 if (retval) 87 return retval; 88 memset(prog, 0, sizeof(struct ext2_sim_progress)); 89 90 retval = ext2fs_get_mem(strlen(label)+1, &prog->label); 91 if (retval) { 92 free(prog); 93 return retval; 94 } 95 strcpy(prog->label, label); 96 prog->labelwidth = labelwidth; 97 prog->barwidth = barwidth; 98 prog->flags = flags; 99 prog->maxdone = maxdone; 100 prog->current = 0; 101 prog->shown = 0; 102 prog->f = stdout; 103 104 *ret_prog = prog; 105 106 return ext2fs_progress_display(prog); 107 } 108 109 void ext2fs_progress_close(ext2_sim_progmeter prog) 110 { 111 112 if (prog->label) 113 ext2fs_free_mem(&prog->label); 114 ext2fs_free_mem(&prog); 115 printf("\n"); 116 return; 117 } 118