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