Whamcloud - gitweb
e2scan: a tool for fast namespace/inode scanning
[tools/e2fsprogs.git] / ext2ed / general_com.c
index 21fd30b..c9b2ffc 100644 (file)
@@ -14,11 +14,13 @@ Copyright (C) 1995 Gadi Oxman
 
 */
 
+#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "ext2ed.h"
+#include "../version.h"
 
 void help (char *command_line)
 
@@ -32,16 +34,16 @@ void help (char *command_line)
 
        if (*ptr!=0) {
                 ptr=parse_word (ptr,argument);
-                if (*argument!=0) {             
+                if (*argument!=0) {
                         detailed_help (argument);
                         return;
                }
        }
 
        if (current_type!=NULL) {
-               
+
                wprintw (show_pad,"Type %s specific commands:\n",current_type->name);max_line++;
-               
+
                if (current_type->type_commands.last_command==-1) {
                        wprintw (show_pad,"\nnone\n");max_line+=2;
                }
@@ -52,11 +54,11 @@ void help (char *command_line)
                                }
                                wprintw (show_pad,"%-13s",current_type->type_commands.names [i]);
                                if (i%5!=4)
-                                       wprintw (show_pad,";  ");                               
+                                       wprintw (show_pad,";  ");
                        }
-               
+
                wprintw (show_pad,"\n\n");max_line+=2;
-       }               
+       }
 
        if (ext2_commands.last_command != -1) {
                wprintw (show_pad,"ext2 filesystem general commands: \n");max_line++;
@@ -66,48 +68,42 @@ void help (char *command_line)
                        }
                        wprintw (show_pad,"%-13s",ext2_commands.names [i]);
                        if (i%5!=4)
-                               wprintw (show_pad,";  ");                               
+                               wprintw (show_pad,";  ");
 
                }
                wprintw (show_pad,"\n\n");max_line+=2;
        }
 
        wprintw (show_pad,"General commands: \n");
-       
+
        for (i=0;i<=general_commands.last_command;i++) {
                if (i%5==0) {
                        wprintw (show_pad,"\n");max_line++;
                }
                wprintw (show_pad,"%-13s",general_commands.names [i]);
                if (i%5!=4)
-                       wprintw (show_pad,";  ");                               
+                       wprintw (show_pad,";  ");
        }
-       
+
        wprintw (show_pad,"\n\n");max_line+=2;
-       
-       wprintw (show_pad,"EXT2ED ver %d.%d (%s)\n",version_major,version_minor,revision_date);
+
+       wprintw (show_pad,"EXT2ED ver %s (%s)\n",E2FSPROGS_VERSION, E2FSPROGS_DATE);
        wprintw (show_pad,"Copyright (C) 1995 Gadi Oxman\n");
        wprintw (show_pad,"Reviewed 2001 Christian Bac\n");
+       wprintw (show_pad,"Modified and enchanced by Theodore Ts'o, 2002\n");
        wprintw (show_pad,"EXT2ED is hereby placed under the terms of the GNU General Public License.\n\n");
        wprintw (show_pad,"EXT2ED was programmed as a student project in the software laboratory\n");
        wprintw (show_pad,"of the faculty of electrical engineering in the\n");
        wprintw (show_pad,"Technion - Israel Institute of Technology\n");
        wprintw (show_pad,"with the guide of Avner Lottem and Dr. Ilana David.\n");
 
-       max_line+=6;
-       
-       wprintw (show_pad,"\n\n");max_line+=2;
-       
-       wprintw (show_pad,"Please feel free to mail me at (currently) %s\n",email_address);
-       wprintw (show_pad,"with any commet, suggestion, and of-course, bug report concerning EXT2ED.\n");
-       
-       max_line+=2;
-       
+       max_line+=10;
+
        show_pad_info.line=0;show_pad_info.max_line=max_line;
 
        werase (show_win);wmove (show_win,0,0);
        wprintw (show_win,"EXT2ED help");
-       
+
        refresh_show_win ();
        refresh_show_pad ();
 }
@@ -116,7 +112,7 @@ void detailed_help (char *text)
 
 {
        int i;
-       
+
        if (current_type != NULL)
                for (i=0;i<=current_type->type_commands.last_command;i++) {
                        if (strcmp (current_type->type_commands.names [i],text)==0) {
@@ -154,20 +150,20 @@ void set_device (char *command_line)
 
 {
        char *ptr,new_device [80];
-       
+
        ptr=parse_word (command_line,new_device);
        if (*ptr==0) {
                wprintw (command_win,"Error - Device name not specified\n");
                refresh_command_win ();return;
        }
-       parse_word (ptr,new_device);    
+       parse_word (ptr,new_device);
        check_mounted (new_device);
        if (mounted && !AllowMountedRead) {
                wprintw (command_win,"Error - Filesystem is mounted, aborting\n");
                wprintw (command_win,"You may wish to use the AllowMountedRead on configuration option\n");
                refresh_command_win ();return;
        }
-       
+
        if (mounted && AllowMountedRead) {
                wprintw (command_win,"Warning - Filesystem is mounted. Displayed data may be unreliable.\n");
                refresh_command_win ();
@@ -175,7 +171,7 @@ void set_device (char *command_line)
 
        if (device_handle!=NULL)
                fclose (device_handle);
-               
+
        if ( (device_handle=fopen (new_device,"rb"))==NULL) {
                wprintw (command_win,"Error - Can not open device %s\n",new_device);refresh_command_win ();
                return;
@@ -209,14 +205,14 @@ void set_offset (char *command_line)
        long mult=1;
        long new_offset;
        char *ptr,new_offset_buffer [80];
-       
+
        if (device_handle==NULL) {
                wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
                return;
        }
-       
+
        ptr=parse_word (command_line,new_offset_buffer);
-       
+
        if (*ptr==0) {
                wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();
                return;
@@ -251,7 +247,7 @@ void set_offset (char *command_line)
                }
                new_offset=device_offset+atol (new_offset_buffer+1)*mult;
        }
-       
+
        else if (new_offset_buffer [0]=='-') {
                if (device_offset==-1) {
                        wprintw (command_win,"Error - Select a fixed offset first\n");refresh_command_win ();
@@ -260,10 +256,10 @@ void set_offset (char *command_line)
                new_offset=device_offset-atol (new_offset_buffer+1)*mult;
                if (new_offset<0) new_offset=0;
        }
-       
-       else 
+
+       else
                new_offset=atol (new_offset_buffer)*mult;
-       
+
        if ( (fseek (device_handle,new_offset,SEEK_SET))==-1) {
                wprintw (command_win,"Error - Failed to seek to offset %ld in device %s\n",new_offset,device_name);
                refresh_command_win ();
@@ -275,15 +271,99 @@ void set_offset (char *command_line)
        type_data.offset_in_block=0;
 }
 
+void set_int(short len, void *ptr, char *name, char *value)
+{
+       char    *char_ptr;
+       short   *short_ptr;
+       long    *long_ptr;
+       long    v;
+       char    *tmp;
+
+       v = strtol(value, &tmp, 0);
+       if (*tmp) {
+               wprintw( command_win, "Bad value - %s\n", value);
+               return;
+       }
+       switch (len) {
+       case 1:
+               char_ptr = (char *) ptr;
+               *char_ptr = v;
+               break;
+       case 2:
+               short_ptr = (short *) ptr;
+               *short_ptr = v;
+               break;
+       case 4:
+               long_ptr = (long *) ptr;
+               *long_ptr = v;
+               break;
+       default:
+               wprintw (command_win,
+                        "set_int: unsupported length: %d\n", len);
+               return;
+       }
+       wprintw (command_win, "Variable %s set to %s\n",
+                name, value);
+}
+
+void set_uint(short len, void *ptr, char *name, char *value)
+{
+       unsigned char   *char_ptr;
+       unsigned short  *short_ptr;
+       unsigned long   *long_ptr;
+       unsigned long   v;
+       char            *tmp;
+
+       v = strtoul(value, &tmp, 0);
+       if (*tmp) {
+               wprintw( command_win, "Bad value - %s\n", value);
+               return;
+       }
+       switch (len) {
+       case 1:
+               char_ptr = (unsigned char *) ptr;
+               *char_ptr = v;
+               break;
+       case 2:
+               short_ptr = (unsigned short *) ptr;
+               *short_ptr = v;
+               break;
+       case 4:
+               long_ptr = (unsigned long *) ptr;
+               *long_ptr = v;
+               break;
+       default:
+               wprintw (command_win,
+                        "set_uint: unsupported length: %d\n", len);
+               return;
+       }
+       wprintw (command_win, "Variable %s set to %s\n",
+                name, value);
+}
+
+void set_char(short len, void *ptr, char *name, char *value)
+{
+       if (strlen(value)+1 > len) {
+               wprintw( command_win, "Value %s too big for field\n",
+                       name, len);
+               return;
+       }
+       memset(ptr, 0, len);
+       strcpy((char *) ptr, value);
+       wprintw (command_win, "Variable %s set to %s\n",
+                name, value);
+}
+
+
 void set (char *command_line)
 
 {
        unsigned short *int_ptr;
        unsigned char *char_ptr;
        unsigned long *long_ptr,offset=0;
-       int i,found=0;
+       int i,len, found=0;
        char *ptr,buffer [80],variable [80],value [80];
-       
+
        if (device_handle==NULL) {
                wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
                return;
@@ -310,29 +390,29 @@ void set (char *command_line)
        if (current_type==NULL) {
                wprintw (command_win,"Sorry, not yet supported\n");refresh_command_win ();return;
        }
-       
+
        for (i=0;i<current_type->fields_num && !found;i++) {
                if (strcmp (current_type->field_names [i],variable)==0) {
                        found=1;
                        ptr=type_data.u.buffer+offset;
-                       switch (current_type->field_lengths [i]) {
-                               case 1:
-                                       char_ptr=(unsigned char *) ptr;
-                                       *char_ptr=(char) atoi (value);
-                                       wprintw (command_win,"Variable %s set to %u\n",variable,*char_ptr);refresh_command_win ();
-                                       break;
-                               case 2:
-                                       int_ptr=(unsigned short *) ptr;
-                                       *int_ptr=atoi (value);
-                                       wprintw (command_win,"Variable %s set to %u\n",variable,*int_ptr);refresh_command_win ();
-                                       break;
-
-                               case 4:
-                                       long_ptr=(unsigned long *) ptr;
-                                       *long_ptr=atol (value);
-                                       wprintw (command_win,"Variable %s set to %lu\n",variable,*long_ptr);refresh_command_win ();
-                                       break;
+                       len = current_type->field_lengths [i];
+                       switch (current_type->field_types [i]) {
+                       case FIELD_TYPE_INT:
+                               set_int(len, ptr, variable, value);
+                               break;
+                       case FIELD_TYPE_UINT:
+                               set_uint(len, ptr, variable, value);
+                               break;
+                       case FIELD_TYPE_CHAR:
+                               set_char(len, ptr, variable, value);
+                               break;
+                       default:
+                               wprintw (command_win,
+                                        "set: unhandled type %d\n",
+                                        current_type->field_types [i]);
+                               break;
                        }
+                       refresh_command_win ();
                }
                offset+=current_type->field_lengths [i];
        }
@@ -350,7 +430,7 @@ void hex_set (char *command_line)
        unsigned char tmp;
        char *ptr,buffer [80],*ch_ptr;
        int mode=HEX;
-       
+
        ptr=parse_word (command_line,buffer);
        if (*ptr==0) {
                wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
@@ -403,7 +483,7 @@ void hex_set (char *command_line)
                        }
                }
        }
-       
+
        strcpy (buffer,"show");dispatch (buffer);
 }
 
@@ -421,14 +501,14 @@ void set_type (char *command_line)
 
        ptr=parse_word (command_line,buffer);
        parse_word (ptr,buffer);
-       
+
        if (strcmp (buffer,"none")==0 || strcmp (buffer,"hex")==0) {
                wprintw (command_win,"Data will be shown as hex dump\n");refresh_command_win ();
                current_type=NULL;
                sprintf (tmp_buffer,"show");dispatch (tmp_buffer);
                return;
        }
-       
+
        descriptor_ptr=first_type;
        while (descriptor_ptr!=NULL && !found) {
                if (strcmp (descriptor_ptr->name,buffer)==0)
@@ -444,14 +524,98 @@ void set_type (char *command_line)
        else {
                wprintw (command_win,"Error - %s is not a valid type\n",buffer);refresh_command_win ();
        }
-}    
+}
+
+void show_int(short len, void *ptr)
+{
+       long    temp;
+       char    *format;
+
+       switch (len) {
+       case 1:
+               temp = *((char *) ptr);
+               format = "%3d (0x%02x)\n";
+               break;
+       case 2:
+               temp = *((short *) ptr);
+               format = "%d (0x%x)\n";
+               break;
+       case 4:
+               temp = *((long *) ptr);
+               format = "%d\n";
+               break;
+       default:
+               wprintw (show_pad, "unimplemented\n");
+               return;
+       }
+       wprintw(show_pad, format, temp, temp);
+}
+
+void show_uint(short len, void *ptr)
+{
+       unsigned long   temp;
+       char            *format;
+
+       switch (len) {
+       case 1:
+               temp = *((unsigned char *) ptr);
+               temp = temp & 0xFF;
+               format = "%3u (0x%02x)\n";
+               break;
+       case 2:
+               temp = *((unsigned short *) ptr);
+               temp = temp & 0xFFFF;
+               format = "%u (0x%x)\n";
+               break;
+       case 4:
+               temp = (unsigned long) *((unsigned long *) ptr);
+               format = "%u\n";
+               break;
+       default:
+               wprintw (show_pad, "unimplemented\n");
+               return;
+       }
+       wprintw(show_pad, format, temp, temp);
+}
+
+void show_char(short len, void *ptr)
+{
+       unsigned char   *cp = (unsigned char *) ptr;
+       unsigned char   ch;
+       int             i,j;
+
+       wprintw(show_pad, "\"");
+
+       for (i=0; i < len; i++) {
+               ch = *cp++;
+               if (ch == 0) {
+                       for (j=i+1; j < len; j++)
+                               if (cp[j-i])
+                                       break;
+                       if (j == len)
+                               break;
+               }
+               if (ch > 128) {
+                       wprintw(show_pad, "M-");
+                       ch -= 128;
+               }
+               if ((ch < 32) || (ch == 0x7f)) {
+                       wprintw(show_pad, "^");
+                       ch ^= 0x40; /* ^@, ^A, ^B; ^? for DEL */
+               }
+               wprintw(show_pad, "%c", ch);
+       }
+
+       wprintw(show_pad, "\"\n");
+}
+
 
 
 void show (char *command_line)
 
 {
-       unsigned int i,l,temp_int;
-       unsigned long offset=0,temp_long;       
+       unsigned int i,l,len,temp_int;
+       unsigned long offset=0,temp_long;
        unsigned char temp_char,*ch_ptr;
        void *ptr;
 
@@ -459,7 +623,7 @@ void show (char *command_line)
                return;
 
        show_pad_info.line=0;
-       
+
        if (current_type==NULL) {
                wmove (show_pad,0,0);
                ch_ptr=type_data.u.buffer;
@@ -468,7 +632,7 @@ void show (char *command_line)
                        for (i=0;i<16;i++) {
                                if (type_data.offset_in_block==offset+i)
                                        wattrset (show_pad,A_REVERSE);
-                       
+
                                if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
                                        wprintw (show_pad,"%c",ch_ptr [i]);
                                else
@@ -480,7 +644,7 @@ void show (char *command_line)
                        for (i=0;i<16;i++) {
                                if (type_data.offset_in_block==offset+i)
                                        wattrset (show_pad,A_REVERSE);
-                       
+
                                wprintw (show_pad,"%02x",ch_ptr [i]);
 
                                if (type_data.offset_in_block==offset+i) {
@@ -502,29 +666,23 @@ void show (char *command_line)
                for (i=0;i<current_type->fields_num;i++) {
                        wprintw (show_pad,"%-20s = ",current_type->field_names [i]);
                        ptr=type_data.u.buffer+offset;
-                       switch (current_type->field_lengths [i]) {
-                               case 1:
-                                       temp_char=*((unsigned char *) ptr);
-                                       wprintw (show_pad,"%3u (0x%02x",temp_char,temp_char);
-                                       if (temp_char>=' ' && temp_char<='z')
-                                               wprintw (show_pad," , %c)\n",temp_char);
-                                       else
-                                               wprintw (show_pad,")\n");
-
-                                       offset ++;l++;
-                                       break;
-                               case 2:
-                                       temp_int=*((unsigned short *) ptr);
-                                       wprintw (show_pad,"%u (0x%x)\n",temp_int,temp_int);
-                                       offset +=2;l++;
-                                       break;
-                               case 4:
-                                       temp_long=*((unsigned long *) ptr);
-                                       wprintw (show_pad,"%lu\n",temp_long);
-                                       offset +=4;l++;
-                                       break;
+                       len = current_type->field_lengths[i];
+                       switch (current_type->field_types[i]) {
+                       case FIELD_TYPE_INT:
+                               show_int(len, ptr);
+                               break;
+                       case FIELD_TYPE_UINT:
+                               show_uint(len, ptr);
+                               break;
+                       case FIELD_TYPE_CHAR:
+                               show_char(len, ptr);
+                               break;
+                       default:
+                               wprintw (show_pad, "unimplemented\n");
+                               break;
                        }
-/*                     offset+=current_type->field_lengths [i]; */
+                       offset+=len;
+                       l++;
                }
                current_type->length=offset;
                show_pad_info.max_line=l-1;
@@ -539,12 +697,12 @@ void next (char *command_line)
        char *ptr,buffer [80];
 
        ptr=parse_word (command_line,buffer);
-       
+
        if (*ptr!=0) {
                ptr=parse_word (ptr,buffer);
                offset*=atol (buffer);
        }
-       
+
        if (current_type!=NULL) {
                sprintf (buffer,"setoffset type +%ld",offset);
                dispatch (buffer);
@@ -555,7 +713,7 @@ void next (char *command_line)
                type_data.offset_in_block+=offset;
                sprintf (buffer,"show");dispatch (buffer);
        }
-               
+
        else {
                wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
        }
@@ -568,12 +726,12 @@ void prev (char *command_line)
        char *ptr,buffer [80];
 
        ptr=parse_word (command_line,buffer);
-       
+
        if (*ptr!=0) {
                ptr=parse_word (ptr,buffer);
                offset*=atol (buffer);
        }
-       
+
        if (current_type!=NULL) {
                sprintf (buffer,"setoffset type -%ld",offset);
                dispatch (buffer);
@@ -584,13 +742,13 @@ void prev (char *command_line)
                type_data.offset_in_block-=offset;
                sprintf (buffer,"show");dispatch (buffer);
        }
-       
+
        else {
                wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
        }
 }
 
-void pgdn (char *commnad_line) 
+void pgdn (char *commnad_line)
 
 {
        show_pad_info.line+=show_pad_info.display_lines;
@@ -616,19 +774,19 @@ void remember (char *command_line)
 {
        long entry_num;
        char *ptr,buffer [80];
-       
+
        if (device_handle==NULL) {
                wprintw (command_win,"Error - No device opened\n");refresh_command_win ();
                return;
        }
 
        ptr=parse_word (command_line,buffer);
-       
+
        if (*ptr==0) {
                wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
-               return;         
+               return;
        }
-       
+
        ptr=parse_word (ptr,buffer);
 
        entry_num=remember_lifo.entries_count++;
@@ -636,16 +794,16 @@ void remember (char *command_line)
                entry_num=0;
                remember_lifo.entries_count--;
        }
-       
+
        remember_lifo.offset [entry_num]=device_offset;
        remember_lifo.type [entry_num]=current_type;
        strcpy (remember_lifo.name [entry_num],buffer);
-       
+
        if (current_type!=NULL)
                wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",current_type->name,device_offset,buffer);
        else
                wprintw (command_win,"Offset %ld remembered as %s\n",device_offset,buffer);
-                       
+
        refresh_command_win ();
 }
 
@@ -664,17 +822,17 @@ void recall (char *command_line)
 
        if (*ptr==0) {
                wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
-               return;         
+               return;
        }
 
        ptr=parse_word (ptr,buffer);
 
-       
+
        for (entry_num=remember_lifo.entries_count-1;entry_num>=0;entry_num--) {
                if (strcmp (remember_lifo.name [entry_num],buffer)==0)
-                       break;  
+                       break;
        }
-       
+
        if (entry_num==-1) {
                wprintw (command_win,"Error - Can not recall %s\n",buffer);refresh_command_win ();
                return;
@@ -682,13 +840,13 @@ void recall (char *command_line)
 
        sprintf (buffer,"setoffset %ld",remember_lifo.offset [entry_num]);dispatch (buffer);
        if (remember_lifo.type [entry_num] != NULL) {
-               sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);   
+               sprintf (buffer,"settype %s",remember_lifo.type [entry_num]->name);dispatch (buffer);
        }
 
        else {
-               sprintf (buffer,"settype none");dispatch (buffer);      
+               sprintf (buffer,"settype none");dispatch (buffer);
        }
-                       
+
        wprintw (command_win,"Object %s in Offset %ld recalled\n",current_type->name,device_offset);
        refresh_command_win ();
 }
@@ -707,12 +865,12 @@ void enable_write (char *command_line)
                wprintw (command_win,"Sorry, write access is not allowed\n");
                return;
        }
-       
+
        if (mounted) {
                wprintw (command_win,"Error - Filesystem is mounted\n");
-               return;                 
+               return;
        }
-       
+
        if ( (fp=fopen (device_name,"r+b"))==NULL) {
                wprintw (command_win,"Error - Can not open device %s for reading and writing\n",device_name);refresh_command_win ();
                return;
@@ -736,7 +894,7 @@ void disable_write (char *command_line)
                wprintw (command_win,"Error - Can not open device %s\n",device_name);refresh_command_win ();
                return;
        }
-       
+
        fclose (device_handle);
        device_handle=fp;write_access=0;
        wprintw (command_win,"Write access disabled\n");refresh_command_win ();