]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/message.c
libext2fs: Potential null ptr deref in undo_err_handler_init
[thirdparty/e2fsprogs.git] / e2fsck / message.c
CommitLineData
21c84b71
TT
1/*
2 * message.c --- print e2fsck messages (with compression)
3 *
4 * Copyright 1996, 1997 by Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 *
11 * print_e2fsck_message() prints a message to the user, using
12 * compression techniques and expansions of abbreviations.
13 *
14 * The following % expansions are supported:
15 *
16 * %b <blk> block number
695706ca 17 * %B <blkcount> interpret blkcount as blkcount
1b6bf175 18 * %c <blk2> block number
fcdebce7
AD
19 * %Di <dirent>->ino inode number
20 * %Dn <dirent>->name string
21 * %Dr <dirent>->rec_len
22 * %Dl <dirent>->name_len
23 * %Dt <dirent>->filetype
24 * %d <dir> inode number
21c84b71
TT
25 * %g <group> integer
26 * %i <ino> inode number
27 * %Is <inode> -> i_size
cebe48a1 28 * %IS <inode> -> i_extra_isize
21c84b71
TT
29 * %Ib <inode> -> i_blocks
30 * %Il <inode> -> i_links_count
31 * %Im <inode> -> i_mode
32 * %IM <inode> -> i_mtime
33 * %IF <inode> -> i_faddr
34 * %If <inode> -> i_file_acl
35 * %Id <inode> -> i_dir_acl
ecf1b776
TT
36 * %Iu <inode> -> i_uid
37 * %Ig <inode> -> i_gid
ccfbb266 38 * %It <inode type>
21c84b71 39 * %j <ino2> inode number
1b6bf175 40 * %m <com_err error message>
21c84b71
TT
41 * %N <num>
42 * %p ext2fs_get_pathname of directory <ino>
43 * %P ext2fs_get_pathname of <dirent>->ino with <ino2> as
44 * the containing directory. (If dirent is NULL
45 * then return the pathname of directory <ino2>)
46 * %q ext2fs_get_pathname of directory <dir>
47 * %Q ext2fs_get_pathname of directory <ino> with <dir> as
48 * the containing directory.
695706ca 49 * %r <blkcount> interpret blkcount as refcount
1b6bf175
TT
50 * %s <str> miscellaneous string
51 * %S backup superblock
3b5386dc 52 * %X <num> hexadecimal format
21c84b71
TT
53 *
54 * The following '@' expansions are supported:
55 *
342d847d 56 * @a extended attribute
f8188fff 57 * @A error allocating
21c84b71
TT
58 * @b block
59 * @B bitmap
1917875f 60 * @c compress
21c84b71 61 * @C conflicts with some other fs block
21c84b71
TT
62 * @D deleted
63 * @d directory
64 * @e entry
65 * @E Entry '%Dn' in %p (%i)
1b6bf175 66 * @f filesystem
21c84b71
TT
67 * @F for @i %i (%Q) is
68 * @g group
8fdc9985 69 * @h HTREE directory inode
d74edf4e
TT
70 * @i inode
71 * @I illegal
72 * @j journal
21c84b71
TT
73 * @l lost+found
74 * @L is a link
7ae1983a
TT
75 * @m multiply-claimed
76 * @n invalid
80bfaa3e 77 * @o orphaned
8fdc9985 78 * @p problem in
624e4a64 79 * @q quota
21c84b71 80 * @r root inode
efc6f628 81 * @s should be
1b6bf175 82 * @S superblock
80bfaa3e 83 * @u unattached
d74edf4e 84 * @v device
15d482ba 85 * @x extent
21c84b71
TT
86 * @z zero-length
87 */
88
89#include <stdlib.h>
90#include <unistd.h>
91#include <string.h>
92#include <ctype.h>
93#include <termios.h>
94
95#include "e2fsck.h"
96
97#include "problem.h"
98
99#ifdef __GNUC__
100#define _INLINE_ __inline__
101#else
102#define _INLINE_
103#endif
104
105/*
106 * This structure defines the abbreviations used by the text strings
107 * below. The first character in the string is the index letter. An
108 * abbreviation of the form '@<i>' is expanded by looking up the index
109 * letter <i> in the table below.
110 */
111static const char *abbrevs[] = {
342d847d 112 N_("aextended attribute"),
0c4a0726
TT
113 N_("Aerror allocating"),
114 N_("bblock"),
115 N_("Bbitmap"),
1917875f 116 N_("ccompress"),
0c4a0726
TT
117 N_("Cconflicts with some other fs @b"),
118 N_("iinode"),
119 N_("Iillegal"),
1ce64539 120 N_("jjournal"),
0c4a0726
TT
121 N_("Ddeleted"),
122 N_("ddirectory"),
123 N_("eentry"),
124 N_("E@e '%Dn' in %p (%i)"),
125 N_("ffilesystem"),
126 N_("Ffor @i %i (%Q) is"),
127 N_("ggroup"),
8fdc9985 128 N_("hHTREE @d @i"),
0c4a0726
TT
129 N_("llost+found"),
130 N_("Lis a link"),
7ae1983a
TT
131 N_("mmultiply-claimed"),
132 N_("ninvalid"),
80bfaa3e 133 N_("oorphaned"),
8fdc9985 134 N_("pproblem in"),
624e4a64 135 N_("qquota"),
0c4a0726
TT
136 N_("rroot @i"),
137 N_("sshould be"),
138 N_("Ssuper@b"),
80bfaa3e 139 N_("uunattached"),
d74edf4e 140 N_("vdevice"),
15d482ba 141 N_("xextent"),
0c4a0726 142 N_("zzero-length"),
21c84b71
TT
143 "@@",
144 0
145 };
146
147/*
148 * Give more user friendly names to the "special" inodes.
149 */
721edd0e 150#define num_special_inodes 11
21c84b71
TT
151static const char *special_inode_name[] =
152{
0c4a0726
TT
153 N_("<The NULL inode>"), /* 0 */
154 N_("<The bad blocks inode>"), /* 1 */
21c84b71 155 "/", /* 2 */
0edcc270
AK
156 N_("<The user quota inode>"), /* 3 */
157 N_("<The group quota inode>"), /* 4 */
0c4a0726 158 N_("<The boot loader inode>"), /* 5 */
721edd0e
TT
159 N_("<The undelete directory inode>"), /* 6 */
160 N_("<The group descriptor inode>"), /* 7 */
161 N_("<The journal inode>"), /* 8 */
162 N_("<Reserved inode 9>"), /* 9 */
163 N_("<Reserved inode 10>"), /* 10 */
21c84b71
TT
164};
165
9e51eca7
TT
166/*
167 * This function does "safe" printing. It will convert non-printable
168 * ASCII characters using '^' and M- notation.
169 */
f68aa414 170static void safe_print(const char *cp, int len)
9e51eca7
TT
171{
172 unsigned char ch;
173
174 if (len < 0)
175 len = strlen(cp);
176
177 while (len--) {
178 ch = *cp++;
179 if (ch > 128) {
180 fputs("M-", stdout);
181 ch -= 128;
182 }
ec8d2c3f 183 if ((ch < 32) || (ch == 0x7f)) {
9e51eca7 184 fputc('^', stdout);
ec8d2c3f 185 ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
9e51eca7
TT
186 }
187 fputc(ch, stdout);
188 }
189}
190
191
21c84b71
TT
192/*
193 * This function prints a pathname, using the ext2fs_get_pathname
194 * function
195 */
86c627ec 196static void print_pathname(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t ino)
21c84b71
TT
197{
198 errcode_t retval;
199 char *path;
200
201 if (!dir && (ino < num_special_inodes)) {
0c4a0726 202 fputs(_(special_inode_name[ino]), stdout);
21c84b71
TT
203 return;
204 }
efc6f628 205
21c84b71
TT
206 retval = ext2fs_get_pathname(fs, dir, ino, &path);
207 if (retval)
208 fputs("???", stdout);
209 else {
9e51eca7 210 safe_print(path, -1);
c4e3d3f3 211 ext2fs_free_mem(&path);
21c84b71
TT
212 }
213}
214
fe26a55a
TT
215static void print_time(time_t t)
216{
217 const char * time_str;
218 static int do_gmt = -1;
219
220#ifdef __dietlibc__
221 /* The diet libc doesn't respect the TZ environemnt variable */
222 if (do_gmt == -1) {
223 time_str = getenv("TZ");
224 if (!time_str)
225 time_str = "";
226 do_gmt = !strcmp(time_str, "GMT0");
227 }
228#endif
229 time_str = asctime((do_gmt > 0) ? gmtime(&t) : localtime(&t));
230 printf("%.24s", time_str);
231}
232
21c84b71
TT
233/*
234 * This function handles the '@' expansion. We allow recursive
235 * expansion; an @ expression can contain further '@' and '%'
efc6f628 236 * expressions.
21c84b71 237 */
1b6bf175
TT
238static _INLINE_ void expand_at_expression(e2fsck_t ctx, char ch,
239 struct problem_context *pctx,
1a191d66 240 int *first, int recurse)
21c84b71
TT
241{
242 const char **cpp, *str;
efc6f628 243
21c84b71
TT
244 /* Search for the abbreviation */
245 for (cpp = abbrevs; *cpp; cpp++) {
246 if (ch == *cpp[0])
247 break;
248 }
1a191d66 249 if (*cpp && recurse < 10) {
6177c41f 250 str = _(*cpp) + 1;
21c84b71
TT
251 if (*first && islower(*str)) {
252 *first = 0;
253 fputc(toupper(*str++), stdout);
254 }
1a191d66 255 print_e2fsck_message(ctx, str, pctx, *first, recurse+1);
21c84b71
TT
256 } else
257 printf("@%c", ch);
258}
259
260/*
ecf1b776 261 * This function expands '%IX' expressions
21c84b71 262 */
8a8f3654 263static _INLINE_ void expand_inode_expression(ext2_filsys fs, char ch,
8ff1a860 264 struct problem_context *ctx)
21c84b71
TT
265{
266 struct ext2_inode *inode;
cebe48a1 267 struct ext2_inode_large *large_inode;
21c84b71
TT
268
269 if (!ctx || !ctx->inode)
270 goto no_inode;
efc6f628 271
21c84b71 272 inode = ctx->inode;
cebe48a1
TT
273 large_inode = (struct ext2_inode_large *) inode;
274
21c84b71
TT
275 switch (ch) {
276 case 's':
246501c6
TT
277 if (LINUX_S_ISDIR(inode->i_mode))
278 printf("%u", inode->i_size);
279 else {
280#ifdef EXT2_NO_64_TYPE
281 if (inode->i_size_high)
282 printf("0x%x%08x", inode->i_size_high,
283 inode->i_size);
284 else
285 printf("%u", inode->i_size);
286#else
0bd0e593 287 printf("%llu", EXT2_I_SIZE(inode));
246501c6
TT
288#endif
289 }
21c84b71 290 break;
cebe48a1
TT
291 case 'S':
292 printf("%u", large_inode->i_extra_isize);
293 break;
21c84b71 294 case 'b':
8a8f3654
TT
295 if (fs->super->s_feature_ro_compat &
296 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
1ca1059f
TT
297 printf("%llu", inode->i_blocks +
298 (((long long) inode->osd2.linux2.l_i_blocks_hi)
299 << 32));
300 else
301 printf("%u", inode->i_blocks);
21c84b71
TT
302 break;
303 case 'l':
304 printf("%d", inode->i_links_count);
305 break;
306 case 'm':
307 printf("0%o", inode->i_mode);
308 break;
309 case 'M':
fe26a55a 310 print_time(inode->i_mtime);
21c84b71
TT
311 break;
312 case 'F':
313 printf("%u", inode->i_faddr);
314 break;
315 case 'f':
a63745e8 316 printf("%llu", ext2fs_file_acl_block(inode));
21c84b71
TT
317 break;
318 case 'd':
246501c6
TT
319 printf("%u", (LINUX_S_ISDIR(inode->i_mode) ?
320 inode->i_dir_acl : 0));
21c84b71 321 break;
ecf1b776 322 case 'u':
5113a6e3 323 printf("%d", inode_uid(*inode));
ecf1b776
TT
324 break;
325 case 'g':
5113a6e3 326 printf("%d", inode_gid(*inode));
ecf1b776 327 break;
ccfbb266 328 case 't':
efc6f628 329 if (LINUX_S_ISREG(inode->i_mode))
ccfbb266 330 printf(_("regular file"));
efc6f628 331 else if (LINUX_S_ISDIR(inode->i_mode))
ccfbb266 332 printf(_("directory"));
efc6f628 333 else if (LINUX_S_ISCHR(inode->i_mode))
ccfbb266 334 printf(_("character device"));
efc6f628 335 else if (LINUX_S_ISBLK(inode->i_mode))
ccfbb266 336 printf(_("block device"));
efc6f628 337 else if (LINUX_S_ISFIFO(inode->i_mode))
ccfbb266 338 printf(_("named pipe"));
efc6f628 339 else if (LINUX_S_ISLNK(inode->i_mode))
ccfbb266
TT
340 printf(_("symbolic link"));
341 else if (LINUX_S_ISSOCK(inode->i_mode))
342 printf(_("socket"));
343 else
344 printf(_("unknown file type with mode 0%o"),
345 inode->i_mode);
346 break;
21c84b71
TT
347 default:
348 no_inode:
349 printf("%%I%c", ch);
350 break;
351 }
352}
353
354/*
355 * This function expands '%dX' expressions
356 */
8a480350 357static _INLINE_ void expand_dirent_expression(ext2_filsys fs, char ch,
21c84b71
TT
358 struct problem_context *ctx)
359{
360 struct ext2_dir_entry *dirent;
8a480350 361 unsigned int rec_len;
21c84b71 362 int len;
efc6f628 363
21c84b71
TT
364 if (!ctx || !ctx->dirent)
365 goto no_dirent;
efc6f628 366
21c84b71 367 dirent = ctx->dirent;
efc6f628 368
21c84b71
TT
369 switch (ch) {
370 case 'i':
371 printf("%u", dirent->inode);
372 break;
373 case 'n':
b6f79831 374 len = dirent->name_len & 0xFF;
8a480350
TT
375 if ((ext2fs_get_rec_len(fs, dirent, &rec_len) == 0) &&
376 (len > rec_len))
377 len = rec_len;
9e51eca7 378 safe_print(dirent->name, len);
21c84b71 379 break;
1b6bf175 380 case 'r':
8a480350
TT
381 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
382 printf("%u", rec_len);
1b6bf175
TT
383 break;
384 case 'l':
b6f79831 385 printf("%u", dirent->name_len & 0xFF);
1b6bf175 386 break;
aa4115a4
TT
387 case 't':
388 printf("%u", dirent->name_len >> 8);
389 break;
21c84b71
TT
390 default:
391 no_dirent:
392 printf("%%D%c", ch);
393 break;
394 }
395}
396
397static _INLINE_ void expand_percent_expression(ext2_filsys fs, char ch,
695706ca 398 int *first,
21c84b71
TT
399 struct problem_context *ctx)
400{
fe26a55a 401 e2fsck_t e2fsck_ctx = fs ? (e2fsck_t) fs->priv_data : NULL;
695706ca 402 const char *m;
fe26a55a 403
21c84b71
TT
404 if (!ctx)
405 goto no_context;
efc6f628 406
21c84b71
TT
407 switch (ch) {
408 case '%':
409 fputc('%', stdout);
410 break;
411 case 'b':
15d482ba
TT
412#ifdef EXT2_NO_64_TYPE
413 printf("%u", (unsigned long) ctx->blk);
414#else
415 printf("%llu", (unsigned long long) ctx->blk);
416#endif
21c84b71
TT
417 break;
418 case 'B':
695706ca
TT
419 if (ctx->blkcount == BLOCK_COUNT_IND)
420 m = _("indirect block");
421 else if (ctx->blkcount == BLOCK_COUNT_DIND)
422 m = _("double indirect block");
423 else if (ctx->blkcount == BLOCK_COUNT_TIND)
424 m = _("triple indirect block");
425 else if (ctx->blkcount == BLOCK_COUNT_TRANSLATOR)
426 m = _("translator block");
427 else
428 m = _("block #");
429 if (*first && islower(m[0]))
430 fputc(toupper(*m++), stdout);
431 fputs(m, stdout);
432 if (ctx->blkcount >= 0) {
246501c6 433#ifdef EXT2_NO_64_TYPE
695706ca 434 printf("%d", ctx->blkcount);
246501c6 435#else
695706ca 436 printf("%lld", (long long) ctx->blkcount);
246501c6 437#endif
695706ca 438 }
21c84b71 439 break;
1b6bf175 440 case 'c':
15d482ba
TT
441#ifdef EXT2_NO_64_TYPE
442 printf("%u", (unsigned long) ctx->blk2);
443#else
444 printf("%llu", (unsigned long long) ctx->blk2);
445#endif
1b6bf175 446 break;
21c84b71 447 case 'd':
86c627ec 448 printf("%u", ctx->dir);
21c84b71
TT
449 break;
450 case 'g':
451 printf("%d", ctx->group);
452 break;
453 case 'i':
86c627ec 454 printf("%u", ctx->ino);
21c84b71
TT
455 break;
456 case 'j':
86c627ec 457 printf("%u", ctx->ino2);
21c84b71 458 break;
1b6bf175
TT
459 case 'm':
460 printf("%s", error_message(ctx->errcode));
461 break;
21c84b71 462 case 'N':
246501c6 463#ifdef EXT2_NO_64_TYPE
21c84b71 464 printf("%u", ctx->num);
246501c6 465#else
de8f3a76 466 printf("%llu", (long long)ctx->num);
246501c6 467#endif
21c84b71
TT
468 break;
469 case 'p':
470 print_pathname(fs, ctx->ino, 0);
471 break;
472 case 'P':
473 print_pathname(fs, ctx->ino2,
474 ctx->dirent ? ctx->dirent->inode : 0);
475 break;
476 case 'q':
477 print_pathname(fs, ctx->dir, 0);
478 break;
479 case 'Q':
480 print_pathname(fs, ctx->dir, ctx->ino);
481 break;
695706ca
TT
482 case 'r':
483#ifdef EXT2_NO_64_TYPE
484 printf("%d", ctx->blkcount);
485#else
486 printf("%lld", (long long) ctx->blkcount);
487#endif
488 break;
1b6bf175 489 case 'S':
8deb80a5 490 printf("%u", get_backup_sb(NULL, fs, NULL, NULL));
1b6bf175
TT
491 break;
492 case 's':
133a56dc 493 printf("%s", ctx->str ? ctx->str : "NULL");
1b6bf175 494 break;
fe26a55a
TT
495 case 't':
496 print_time((time_t) ctx->num);
497 break;
498 case 'T':
499 print_time(e2fsck_ctx ? e2fsck_ctx->now : time(0));
500 break;
3b5386dc
TT
501 case 'X':
502#ifdef EXT2_NO_64_TYPE
503 printf("0x%x", ctx->num);
504#else
de8f3a76 505 printf("0x%llx", (long long)ctx->num);
3b5386dc
TT
506#endif
507 break;
21c84b71
TT
508 default:
509 no_context:
510 printf("%%%c", ch);
511 break;
512 }
efc6f628 513}
21c84b71 514
1b6bf175 515void print_e2fsck_message(e2fsck_t ctx, const char *msg,
1a191d66
TT
516 struct problem_context *pctx, int first,
517 int recurse)
21c84b71 518{
1b6bf175 519 ext2_filsys fs = ctx->fs;
21c84b71
TT
520 const char * cp;
521 int i;
5596defa
TT
522
523 e2fsck_clear_progbar(ctx);
21c84b71
TT
524 for (cp = msg; *cp; cp++) {
525 if (cp[0] == '@') {
526 cp++;
1a191d66 527 expand_at_expression(ctx, *cp, pctx, &first, recurse);
21c84b71
TT
528 } else if (cp[0] == '%' && cp[1] == 'I') {
529 cp += 2;
8a8f3654 530 expand_inode_expression(fs, *cp, pctx);
21c84b71
TT
531 } else if (cp[0] == '%' && cp[1] == 'D') {
532 cp += 2;
8a480350 533 expand_dirent_expression(fs, *cp, pctx);
21c84b71
TT
534 } else if ((cp[0] == '%')) {
535 cp++;
695706ca 536 expand_percent_expression(fs, *cp, &first, pctx);
21c84b71
TT
537 } else {
538 for (i=0; cp[i]; i++)
539 if ((cp[i] == '@') || cp[i] == '%')
540 break;
541 printf("%.*s", i, cp);
542 cp += i-1;
543 }
544 first = 0;
545 }
546}