2 * util.c --- miscellaneous utilities
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
21 #include <sys/resource.h>
23 const char * fix_msg[2] = { "IGNORED", "FIXED" };
25 void fatal_error (const char *msg)
28 fprintf (stderr, "%s: %s\n", program_name, msg);
32 void *allocate_memory(int size, const char *description)
37 #ifdef DEBUG_ALLOCATE_MEMORY
38 printf("Allocating %d bytes for %s...\n", size, description);
42 sprintf(buf, "Can't allocate %s\n", description);
50 int ask_yn(const char * string, int def)
53 struct termios termios, tmp;
56 tcgetattr (0, &termios);
58 tmp.c_lflag &= ~(ICANON | ECHO);
61 tcsetattr (0, TCSANOW, &tmp);
69 printf("%s%s? ", string, defstr);
72 if ((c = getchar()) == EOF)
83 else if ((c == ' ' || c == '\n') && (def != -1))
90 tcsetattr (0, TCSANOW, &termios);
94 int ask (const char * string, int def)
97 printf ("%s? no\n\n", string);
101 printf ("%s? yes\n\n", string);
105 printf ("%s? %s\n\n", string, def ? "yes" : "no");
108 return ask_yn(string, def);
111 void read_bitmaps(ext2_filsys fs)
115 if (invalid_bitmaps) {
116 com_err(program_name, 0,
117 "read_bitmaps: illegal bitmap block(s) for %s",
122 ehandler_operation("reading inode and block bitmaps");
123 retval = ext2fs_read_bitmaps(fs);
124 ehandler_operation(0);
126 com_err(program_name, retval,
127 "while retrying to read bitmaps for %s",
133 void write_bitmaps(ext2_filsys fs)
137 if (ext2fs_test_bb_dirty(fs)) {
138 ehandler_operation("writing block bitmaps");
139 retval = ext2fs_write_block_bitmap(fs);
140 ehandler_operation(0);
142 com_err(program_name, retval,
143 "while retrying to write block bitmaps for %s",
149 if (ext2fs_test_ib_dirty(fs)) {
150 ehandler_operation("writing inode bitmaps");
151 retval = ext2fs_write_inode_bitmap(fs);
152 ehandler_operation(0);
154 com_err(program_name, retval,
155 "while retrying to write inode bitmaps for %s",
162 void preenhalt(ext2_filsys fs)
166 fprintf(stderr, "\n\n%s: UNEXPECTED INCONSISTENCY; "
167 "RUN fsck MANUALLY.\n\t(i.e., without -a or -p options)\n",
170 fs->super->s_state |= EXT2_ERROR_FS;
171 ext2fs_mark_super_dirty(fs);
174 exit(FSCK_UNCORRECTED);
177 void init_resource_track(struct resource_track *track)
179 #ifdef HAVE_GETRUSAGE
183 track->brk_start = sbrk(0);
184 gettimeofday(&track->time_start, 0);
185 #ifdef HAVE_GETRUSAGE
186 getrusage(RUSAGE_SELF, &r);
187 track->user_start = r.ru_utime;
188 track->system_start = r.ru_stime;
190 track->user_start.tv_sec = track->user_start.tv_usec = 0;
191 track->system_start.tv_sec = track->system_start.tv_usec = 0;
196 #define _INLINE_ __inline__
201 static _INLINE_ float timeval_subtract(struct timeval *tv1,
204 return ((tv1->tv_sec - tv2->tv_sec) +
205 ((float) (tv1->tv_usec - tv2->tv_usec)) / 1000000);
208 void print_resource_track(struct resource_track *track)
210 #ifdef HAVE_GETRUSAGE
213 struct timeval time_end;
215 gettimeofday(&time_end, 0);
216 #ifdef HAVE_GETRUSAGE
217 getrusage(RUSAGE_SELF, &r);
219 printf("Memory used: %d, elapsed time: %6.3f/%6.3f/%6.3f\n",
220 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
221 timeval_subtract(&time_end, &track->time_start),
222 timeval_subtract(&r.ru_utime, &track->user_start),
223 timeval_subtract(&r.ru_stime, &track->system_start));
225 printf("Memory used: %d, elapsed time: %6.3f\n",
226 (int) (((char *) sbrk(0)) - ((char *) track->brk_start)),
227 timeval_subtract(&time_end, &track->time_start));
231 void e2fsck_read_inode(ext2_filsys fs, unsigned long ino,
232 struct ext2_inode * inode, const char *proc)
236 retval = ext2fs_read_inode(fs, ino, inode);
238 com_err("ext2fs_read_inode", retval,
239 "while reading inode %ld in %s", ino, proc);
244 extern void e2fsck_write_inode(ext2_filsys fs, unsigned long ino,
245 struct ext2_inode * inode, const char *proc)
249 retval = ext2fs_write_inode(fs, ino, inode);
251 com_err("ext2fs_write_inode", retval,
252 "while writing inode %ld in %s", ino, proc);
258 void mtrace_print(char *mesg)
260 FILE *malloc_get_mallstream();
261 FILE *f = malloc_get_mallstream();
264 fprintf(f, "============= %s\n", mesg);