+ i = int_log10(max);
+ sprintf(progress->format, "%%%dd/%%%dld", i, i);
+ memset(progress->backup, '\b', sizeof(progress->backup)-1);
+ progress->backup[sizeof(progress->backup)-1] = 0;
+ if ((2*i)+1 < sizeof(progress->backup))
+ progress->backup[(2*i)+1] = 0;
+ progress->max = max;
+
+ fputs(label, stdout);
+ fflush(stdout);
+}
+
+static void progress_update(struct progress_struct *progress, __u32 val)
+{
+ if (progress->format[0] == 0)
+ return;
+ printf(progress->format, val, progress->max);
+ fputs(progress->backup, stdout);
+}
+
+static void progress_close(struct progress_struct *progress)
+{
+ if (progress->format[0] == 0)
+ return;
+ fputs(_("done \n"), stdout);
+}
+
+
+/*
+ * Helper function which zeros out _num_ blocks starting at _blk_. In
+ * case of an error, the details of the error is returned via _ret_blk_
+ * and _ret_count_ if they are non-NULL pointers. Returns 0 on
+ * success, and an error code on an error.
+ *
+ * As a special case, if the first argument is NULL, then it will
+ * attempt to free the static zeroizing buffer. (This is to keep
+ * programs that check for memory leaks happy.)
+ */
+static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
+ struct progress_struct *progress,
+ blk_t *ret_blk, int *ret_count)
+{
+ int j, count, next_update, next_update_incr;
+ static char *buf;
+ errcode_t retval;
+
+ /* If fs is null, clean up the static buffer and return */
+ if (!fs) {
+ if (buf) {
+ free(buf);
+ buf = 0;
+ }
+ return 0;
+ }
+ /* Allocate the zeroizing buffer if necessary */
+ if (!buf) {
+ buf = malloc(fs->blocksize * STRIDE_LENGTH);
+ if (!buf) {
+ com_err("malloc", ENOMEM,
+ _("while allocating zeroizing buffer"));
+ exit(1);
+ }
+ memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
+ }
+ /* OK, do the write loop */
+ next_update = 0;
+ next_update_incr = num / 100;
+ if (next_update_incr < 1)
+ next_update_incr = 1;
+ for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
+ count = num - j;
+ if (count > STRIDE_LENGTH)
+ count = STRIDE_LENGTH;
+ retval = io_channel_write_blk(fs->io, blk, count, buf);
+ if (retval) {
+ if (ret_count)
+ *ret_count = count;
+ if (ret_blk)
+ *ret_blk = blk;
+ return retval;
+ }
+ if (progress && j > next_update) {
+ next_update += num / 100;
+ progress_update(progress, blk);
+ }
+ }
+ return 0;
+}
+
+static void write_inode_tables(ext2_filsys fs)
+{
+ errcode_t retval;
+ blk_t blk;
+ int i, num;
+ struct progress_struct progress;
+
+ if (quiet)
+ memset(&progress, 0, sizeof(progress));
+ else
+ progress_init(&progress, _("Writing inode tables: "),
+ fs->group_desc_count);