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