Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | /usr/src/ext2ed/init.c |
| 4 | |
| 5 | A part of the extended file system 2 disk editor. |
| 6 | |
| 7 | -------------------------------- |
| 8 | Various initialization routines. |
| 9 | -------------------------------- |
| 10 | |
| 11 | First written on: April 9 1995 |
| 12 | |
| 13 | Copyright (C) 1995 Gadi Oxman |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <readline.h> |
| 21 | #include <signal.h> |
| 22 | #include <unistd.h> |
Theodore Ts'o | 0f31c73 | 2002-05-11 13:03:25 -0400 | [diff] [blame] | 23 | |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | |
| 28 | #include "ext2ed.h" |
| 29 | |
| 30 | char lines_s [80],cols_s [80]; |
| 31 | |
| 32 | void signal_handler (void); |
| 33 | |
| 34 | void prepare_to_close (void) |
| 35 | |
| 36 | { |
| 37 | close_windows (); |
| 38 | if (device_handle!=NULL) |
| 39 | fclose (device_handle); |
| 40 | free_user_commands (&general_commands); |
| 41 | free_user_commands (&ext2_commands); |
| 42 | free_struct_descriptors (); |
| 43 | } |
| 44 | |
| 45 | int init (void) |
| 46 | |
| 47 | { |
| 48 | printf ("Initializing ...\n"); |
| 49 | |
| 50 | if (!process_configuration_file ()) { |
| 51 | fprintf (stderr,"Error - Unable to complete configuration. Quitting.\n"); |
| 52 | return (0); |
| 53 | }; |
| 54 | |
| 55 | general_commands.last_command=-1; /* No commands whatsoever meanwhile */ |
| 56 | ext2_commands.last_command=-1; |
| 57 | add_general_commands (); /* Add the general commands, aviable always */ |
| 58 | device_handle=NULL; /* Notice that our device is still not set up */ |
| 59 | device_offset=-1; |
| 60 | current_type=NULL; /* No filesystem specific types yet */ |
| 61 | |
| 62 | remember_lifo.entries_count=0; /* Object memory is empty */ |
| 63 | |
| 64 | init_windows (); /* Initialize the NCURSES interface */ |
| 65 | init_readline (); /* Initialize the READLINE interface */ |
| 66 | init_signals (); /* Initialize the signal handlers */ |
| 67 | write_access=0; /* Write access disabled */ |
| 68 | |
| 69 | strcpy (last_command_line,"help"); /* Show the help screen to the user */ |
| 70 | dispatch ("help"); |
| 71 | return (1); /* Success */ |
| 72 | } |
| 73 | |
| 74 | void add_general_commands (void) |
| 75 | |
| 76 | { |
| 77 | add_user_command (&general_commands,"help","EXT2ED help system",help); |
| 78 | add_user_command (&general_commands,"set","Changes a variable in the current object",set); |
| 79 | add_user_command (&general_commands,"setdevice","Selects the filesystem block device (e.g. /dev/hda1)",set_device); |
| 80 | add_user_command (&general_commands,"setoffset","Moves asynchronicly in the filesystem",set_offset); |
| 81 | add_user_command (&general_commands,"settype","Tells EXT2ED how to interpert the current object",set_type); |
| 82 | add_user_command (&general_commands,"show","Displays the current object",show); |
| 83 | add_user_command (&general_commands,"pgup","Scrolls data one page up",pgup); |
| 84 | add_user_command (&general_commands,"pgdn","Scrolls data one page down",pgdn); |
| 85 | add_user_command (&general_commands,"redraw","Redisplay the screen",redraw); |
| 86 | add_user_command (&general_commands,"remember","Saves the current position and data information",remember); |
| 87 | add_user_command (&general_commands,"recall","Gets back to the saved object position",recall); |
| 88 | add_user_command (&general_commands,"enablewrite","Enters Read/Write mode - Allows changing the filesystem",enable_write); |
| 89 | add_user_command (&general_commands,"disablewrite","Enters read only mode",disable_write); |
| 90 | add_user_command (&general_commands,"writedata","Write data back to disk",write_data); |
| 91 | add_user_command (&general_commands,"next","Moves to the next byte in hex mode",next); |
| 92 | add_user_command (&general_commands,"prev","Moves to the previous byte in hex mode",prev); |
| 93 | } |
| 94 | |
| 95 | void add_ext2_general_commands (void) |
| 96 | |
| 97 | { |
| 98 | add_user_command (&ext2_commands,"super","Moves to the superblock of the filesystem",type_ext2___super); |
| 99 | add_user_command (&ext2_commands,"group","Moves to the first group descriptor",type_ext2___group); |
| 100 | add_user_command (&ext2_commands,"cd","Moves to the directory specified",type_ext2___cd); |
| 101 | } |
| 102 | |
| 103 | int set_struct_descriptors (char *file_name) |
| 104 | |
| 105 | { |
| 106 | FILE *fp; |
| 107 | char current_line [500],current_word [50],*ch; |
| 108 | char variable_name [50],variable_type [20]; |
| 109 | struct struct_descriptor *current_descriptor; |
| 110 | |
| 111 | if ( (fp=fopen (file_name,"rt"))==NULL) { |
| 112 | wprintw (command_win,"Error - Failed to open descriptors file %s\n",file_name); |
| 113 | refresh_command_win (); return (0); |
| 114 | }; |
| 115 | |
| 116 | while (!feof (fp)) { |
| 117 | fgets (current_line,500,fp); |
| 118 | if (feof (fp)) break; |
| 119 | ch=parse_word (current_line,current_word); |
| 120 | if (strcmp (current_word,"struct")==0) { |
| 121 | ch=parse_word (ch,current_word); |
| 122 | current_descriptor=add_new_descriptor (current_word); |
| 123 | |
| 124 | while (strchr (current_line,'{')==NULL) { |
| 125 | fgets (current_line,500,fp); |
| 126 | if (feof (fp)) break; |
| 127 | }; |
| 128 | if (feof (fp)) break; |
| 129 | |
| 130 | fgets (current_line,500,fp); |
| 131 | |
| 132 | while (strchr (current_line,'}')==NULL) { |
| 133 | while (strchr (current_line,';')==NULL) { |
| 134 | fgets (current_line,500,fp); |
| 135 | if (strchr (current_line,'}')!=NULL) break; |
| 136 | }; |
| 137 | if (strchr (current_line,'}') !=NULL) break; |
| 138 | ch=parse_word (current_line,variable_type); |
| 139 | ch=parse_word (ch,variable_name); |
| 140 | while (variable_name [strlen (variable_name)-1]!=';') { |
| 141 | strcpy (variable_type,variable_name); |
| 142 | ch=parse_word (ch,variable_name); |
| 143 | }; |
| 144 | variable_name [strlen (variable_name)-1]=0; |
| 145 | add_new_variable (current_descriptor,variable_type,variable_name); |
| 146 | fgets (current_line,500,fp); |
| 147 | }; |
| 148 | }; |
| 149 | }; |
| 150 | |
| 151 | fclose (fp); |
| 152 | return (1); |
| 153 | } |
| 154 | |
| 155 | void free_struct_descriptors (void) |
| 156 | |
| 157 | { |
| 158 | struct struct_descriptor *ptr,*next; |
| 159 | |
| 160 | ptr=first_type; |
| 161 | while (ptr!=NULL) { |
| 162 | next=ptr->next; |
| 163 | free_user_commands (&ptr->type_commands); |
| 164 | free (ptr); |
| 165 | ptr=next; |
| 166 | } |
| 167 | first_type=last_type=current_type=NULL; |
| 168 | } |
| 169 | |
| 170 | void free_user_commands (struct struct_commands *ptr) |
| 171 | |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i=0;i<=ptr->last_command;i++) { |
| 176 | free (ptr->names [i]); |
| 177 | free (ptr->descriptions [i]); |
| 178 | } |
| 179 | |
| 180 | ptr->last_command=-1; |
| 181 | } |
| 182 | |
| 183 | struct struct_descriptor *add_new_descriptor (char *name) |
| 184 | |
| 185 | { |
| 186 | struct struct_descriptor *ptr; |
| 187 | |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 188 | ptr = malloc (sizeof (struct struct_descriptor)); |
| 189 | if (ptr == NULL) { |
| 190 | printf ("Error - Can not allocate memory - Quitting\n"); |
| 191 | exit (1); |
| 192 | } |
| 193 | memset(ptr, 0, sizeof(struct struct_descriptor)); |
| 194 | ptr->prev = ptr->next = NULL; |
| 195 | strcpy (ptr->name,name); |
| 196 | ptr->length=0; |
| 197 | ptr->fields_num=0; |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 198 | if (first_type==NULL) { |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 199 | first_type = last_type = ptr; |
| 200 | } else { |
| 201 | ptr->prev = last_type; last_type->next = ptr; last_type=ptr; |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 202 | } |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 203 | ptr->type_commands.last_command=-1; |
| 204 | fill_type_commands (ptr); |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 205 | return (ptr); |
| 206 | } |
| 207 | |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 208 | struct type_table { |
| 209 | char *name; |
| 210 | int field_type; |
| 211 | int len; |
| 212 | }; |
| 213 | |
| 214 | struct type_table type_table[] = { |
| 215 | { "long", FIELD_TYPE_INT, 4 }, |
| 216 | { "short", FIELD_TYPE_INT, 2 }, |
| 217 | { "char", FIELD_TYPE_CHAR, 1 }, |
| 218 | { "__u32", FIELD_TYPE_UINT, 4 }, |
| 219 | { "__s32", FIELD_TYPE_INT, 4 }, |
| 220 | { "__u16", FIELD_TYPE_UINT, 2 }, |
| 221 | { "__s16", FIELD_TYPE_INT, 2 }, |
| 222 | { "__u8", FIELD_TYPE_UINT, 1 }, |
| 223 | { "__s8", FIELD_TYPE_INT, 1 }, |
| 224 | { 0, 0, 0 } |
| 225 | }; |
| 226 | |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 227 | void add_new_variable (struct struct_descriptor *ptr,char *v_type,char *v_name) |
| 228 | |
| 229 | { |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 230 | short len=1; |
| 231 | char field_type=FIELD_TYPE_INT; |
| 232 | struct type_table *p; |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 233 | |
| 234 | strcpy (ptr->field_names [ptr->fields_num],v_name); |
| 235 | ptr->field_positions [ptr->fields_num]=ptr->length; |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 236 | |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 237 | for (p = type_table; p->name; p++) { |
| 238 | if (strcmp(v_type, p->name) == 0) { |
| 239 | len = p->len; |
| 240 | field_type = p->field_type; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | if (p->name == 0) { |
| 245 | if (strncmp(v_type, "char[", 5) == 0) { |
| 246 | len = atoi(v_type+5); |
| 247 | field_type = FIELD_TYPE_CHAR; |
| 248 | } else { |
| 249 | printf("Unknown type %s for field %s\n", v_type, v_name); |
| 250 | exit(1); |
| 251 | } |
| 252 | } |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 253 | |
Theodore Ts'o | 7c36779 | 2002-05-12 00:13:36 -0400 | [diff] [blame^] | 254 | ptr->field_lengths [ptr->fields_num] = len; |
| 255 | ptr->field_types [ptr->fields_num] = field_type; |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 256 | |
| 257 | ptr->length+=len; |
| 258 | ptr->fields_num++; |
| 259 | } |
| 260 | |
| 261 | void fill_type_commands (struct struct_descriptor *ptr) |
| 262 | |
| 263 | /* |
| 264 | |
| 265 | Set specific type user commands. |
| 266 | |
| 267 | */ |
| 268 | |
| 269 | { |
| 270 | |
| 271 | if (strcmp ((ptr->name),"file")==0) { |
| 272 | add_user_command (&ptr->type_commands,"show","Shows file data",type_file___show); |
| 273 | add_user_command (&ptr->type_commands,"inode","Returns to the inode of the current file",type_file___inode); |
| 274 | add_user_command (&ptr->type_commands,"display","Specifies data format - text or hex",type_file___display); |
| 275 | add_user_command (&ptr->type_commands,"next","Pass to next byte",type_file___next); |
| 276 | add_user_command (&ptr->type_commands,"prev","Pass to the previous byte",type_file___prev); |
| 277 | add_user_command (&ptr->type_commands,"offset","Pass to a specified byte in the current block",type_file___offset); |
| 278 | add_user_command (&ptr->type_commands,"nextblock","Pass to next file block",type_file___nextblock); |
| 279 | add_user_command (&ptr->type_commands,"prevblock","Pass to the previous file block",type_file___prevblock); |
| 280 | add_user_command (&ptr->type_commands,"block","Specify which file block to edit",type_file___block); |
| 281 | add_user_command (&ptr->type_commands,"remember","Saves the file\'s inode position for later reference",type_file___remember); |
| 282 | add_user_command (&ptr->type_commands,"set","Sets the current byte",type_file___set); |
| 283 | add_user_command (&ptr->type_commands,"writedata","Writes the current block to the disk",type_file___writedata); |
| 284 | } |
| 285 | |
| 286 | if (strcmp ((ptr->name),"ext2_inode")==0) { |
| 287 | add_user_command (&ptr->type_commands,"show","Shows inode data",type_ext2_inode___show); |
| 288 | add_user_command (&ptr->type_commands,"next","Move to next inode in current block group",type_ext2_inode___next); |
| 289 | add_user_command (&ptr->type_commands,"prev","Move to next inode in current block group",type_ext2_inode___prev); |
| 290 | add_user_command (&ptr->type_commands,"group","Move to the group descriptors of the current inode table",type_ext2_inode___group); |
| 291 | add_user_command (&ptr->type_commands,"entry","Move to a specified entry in the current inode table",type_ext2_inode___entry); |
| 292 | add_user_command (&ptr->type_commands,"file","Display file data of the current inode",type_ext2_inode___file); |
| 293 | add_user_command (&ptr->type_commands,"dir","Display directory data of the current inode",type_ext2_inode___dir); |
| 294 | } |
| 295 | |
| 296 | if (strcmp ((ptr->name),"dir")==0) { |
| 297 | add_user_command (&ptr->type_commands,"show","Shows current directory data",type_dir___show); |
| 298 | add_user_command (&ptr->type_commands,"inode","Returns to the inode of the current directory",type_dir___inode); |
| 299 | add_user_command (&ptr->type_commands,"next","Pass to the next directory entry",type_dir___next); |
| 300 | add_user_command (&ptr->type_commands,"prev","Pass to the previous directory entry",type_dir___prev); |
| 301 | add_user_command (&ptr->type_commands,"followinode","Follows the inode specified in this directory entry",type_dir___followinode); |
| 302 | add_user_command (&ptr->type_commands,"remember","Remember the inode of the current directory entry",type_dir___remember); |
| 303 | add_user_command (&ptr->type_commands,"cd","Changes directory relative to the current directory",type_dir___cd); |
| 304 | add_user_command (&ptr->type_commands,"entry","Moves to a specified entry in the current directory",type_dir___entry); |
| 305 | add_user_command (&ptr->type_commands,"writedata","Writes the current entry to the disk",type_dir___writedata); |
| 306 | add_user_command (&ptr->type_commands,"set","Changes a variable in the current directory entry",type_dir___set); |
| 307 | } |
| 308 | |
| 309 | if (strcmp ((ptr->name),"ext2_super_block")==0) { |
| 310 | add_user_command (&ptr->type_commands,"show","Displays the super block data",type_ext2_super_block___show); |
| 311 | add_user_command (&ptr->type_commands,"gocopy","Move to another backup copy of the superblock",type_ext2_super_block___gocopy); |
| 312 | add_user_command (&ptr->type_commands,"setactivecopy","Copies the current superblock to the main superblock",type_ext2_super_block___setactivecopy); |
| 313 | } |
| 314 | |
| 315 | if (strcmp ((ptr->name),"ext2_group_desc")==0) { |
| 316 | add_user_command (&ptr->type_commands,"next","Pass to the next block group decriptor",type_ext2_group_desc___next); |
| 317 | add_user_command (&ptr->type_commands,"prev","Pass to the previous group descriptor",type_ext2_group_desc___prev); |
| 318 | add_user_command (&ptr->type_commands,"entry","Pass to a specific group descriptor",type_ext2_group_desc___entry); |
| 319 | add_user_command (&ptr->type_commands,"show","Shows the current group descriptor",type_ext2_group_desc___show); |
| 320 | add_user_command (&ptr->type_commands,"inode","Pass to the inode table of the current group block",type_ext2_group_desc___inode); |
| 321 | add_user_command (&ptr->type_commands,"gocopy","Move to another backup copy of the group descriptor",type_ext2_group_desc___gocopy); |
| 322 | add_user_command (&ptr->type_commands,"blockbitmap","Show the block allocation bitmap of the current group block",type_ext2_group_desc___blockbitmap); |
| 323 | add_user_command (&ptr->type_commands,"inodebitmap","Show the inode allocation bitmap of the current group block",type_ext2_group_desc___inodebitmap); |
| 324 | add_user_command (&ptr->type_commands,"setactivecopy","Copies the current group descriptor to the main table",type_ext2_super_block___setactivecopy); |
| 325 | } |
| 326 | |
| 327 | if (strcmp ((ptr->name),"block_bitmap")==0) { |
| 328 | add_user_command (&ptr->type_commands,"show","Displays the block allocation bitmap",type_ext2_block_bitmap___show); |
| 329 | add_user_command (&ptr->type_commands,"entry","Moves to a specific bit",type_ext2_block_bitmap___entry); |
| 330 | add_user_command (&ptr->type_commands,"next","Moves to the next bit",type_ext2_block_bitmap___next); |
| 331 | add_user_command (&ptr->type_commands,"prev","Moves to the previous bit",type_ext2_block_bitmap___prev); |
| 332 | add_user_command (&ptr->type_commands,"allocate","Allocates the current block",type_ext2_block_bitmap___allocate); |
| 333 | add_user_command (&ptr->type_commands,"deallocate","Deallocates the current block",type_ext2_block_bitmap___deallocate); |
| 334 | } |
| 335 | |
| 336 | if (strcmp ((ptr->name),"inode_bitmap")==0) { |
| 337 | add_user_command (&ptr->type_commands,"show","Displays the inode allocation bitmap",type_ext2_inode_bitmap___show); |
| 338 | add_user_command (&ptr->type_commands,"entry","Moves to a specific bit",type_ext2_inode_bitmap___entry); |
| 339 | add_user_command (&ptr->type_commands,"next","Moves to the next bit",type_ext2_inode_bitmap___next); |
| 340 | add_user_command (&ptr->type_commands,"prev","Moves to the previous bit",type_ext2_inode_bitmap___prev); |
| 341 | add_user_command (&ptr->type_commands,"allocate","Allocates the current inode",type_ext2_inode_bitmap___allocate); |
| 342 | add_user_command (&ptr->type_commands,"deallocate","Deallocates the current inode",type_ext2_inode_bitmap___deallocate); |
| 343 | } |
| 344 | |
| 345 | } |
| 346 | |
| 347 | void add_user_command (struct struct_commands *ptr,char *name,char *description,PF callback) |
| 348 | |
| 349 | { |
| 350 | int num; |
| 351 | |
| 352 | num=ptr->last_command; |
| 353 | if (num+1==MAX_COMMANDS_NUM) { |
| 354 | printf ("Internal Error - Can't add command %s\n",name); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | ptr->last_command=++num; |
| 359 | |
| 360 | ptr->names [num]=(char *) malloc (strlen (name)+1); |
| 361 | strcpy (ptr->names [num],name); |
| 362 | |
| 363 | if (*description!=0) { |
| 364 | ptr->descriptions [num]=(char *) malloc (strlen (description)+1); |
| 365 | strcpy (ptr->descriptions [num],description); |
| 366 | } |
| 367 | |
| 368 | ptr->callback [num]=callback; |
| 369 | } |
| 370 | |
| 371 | int set_file_system_info (void) |
| 372 | |
| 373 | { |
| 374 | int ext2_detected=0; |
| 375 | struct ext2_super_block *sb; |
| 376 | |
| 377 | file_system_info.super_block_offset=1024; |
| 378 | file_system_info.file_system_size=DefaultTotalBlocks*DefaultBlockSize; |
| 379 | |
| 380 | low_read ((char *) &file_system_info.super_block,sizeof (struct ext2_super_block),file_system_info.super_block_offset); |
| 381 | |
| 382 | sb=&file_system_info.super_block; |
| 383 | |
| 384 | if (sb->s_magic == EXT2_SUPER_MAGIC) |
| 385 | ext2_detected=1; |
| 386 | |
| 387 | if (ext2_detected) |
| 388 | wprintw (command_win,"Detected extended 2 file system on device %s\n",device_name); |
| 389 | else |
| 390 | wprintw (command_win,"Warning - Extended 2 filesystem not detected on device %s\n",device_name); |
| 391 | |
| 392 | if (!ext2_detected && !ForceExt2) |
| 393 | wprintw (command_win,"You may wish to use the configuration option ForceExt2 on\n"); |
| 394 | |
| 395 | if (ForceExt2 && !ext2_detected) |
| 396 | wprintw (command_win,"Forcing extended 2 filesystem\n"); |
| 397 | |
| 398 | if (ForceDefault || !ext2_detected) |
| 399 | wprintw (command_win,"Forcing default parameters\n"); |
| 400 | |
| 401 | refresh_command_win (); |
| 402 | |
| 403 | if (ext2_detected || ForceExt2) { |
| 404 | add_ext2_general_commands (); |
| 405 | if (!set_struct_descriptors (Ext2Descriptors)) |
| 406 | return (0); |
| 407 | } |
| 408 | |
| 409 | if (!ForceDefault && ext2_detected) { |
| 410 | |
| 411 | file_system_info.block_size=EXT2_MIN_BLOCK_SIZE << sb->s_log_block_size; |
| 412 | if (file_system_info.block_size == EXT2_MIN_BLOCK_SIZE) |
| 413 | file_system_info.first_group_desc_offset=2*EXT2_MIN_BLOCK_SIZE; |
| 414 | else |
| 415 | file_system_info.first_group_desc_offset=file_system_info.block_size; |
| 416 | file_system_info.groups_count=( sb->s_blocks_count-sb->s_first_data_block+sb->s_blocks_per_group-1) / |
| 417 | sb->s_blocks_per_group; |
| 418 | |
| 419 | file_system_info.inodes_per_block=file_system_info.block_size/sizeof (struct ext2_inode); |
| 420 | file_system_info.blocks_per_group=sb->s_inodes_per_group/file_system_info.inodes_per_block; |
| 421 | file_system_info.no_blocks_in_group=sb->s_blocks_per_group; |
| 422 | file_system_info.file_system_size=(sb->s_blocks_count-1)*file_system_info.block_size; |
| 423 | } |
| 424 | |
| 425 | else { |
| 426 | file_system_info.file_system_size=DefaultTotalBlocks*DefaultBlockSize; |
| 427 | file_system_info.block_size=DefaultBlockSize; |
| 428 | file_system_info.no_blocks_in_group=DefaultBlocksInGroup; |
| 429 | } |
| 430 | |
| 431 | if (file_system_info.file_system_size > 2147483647) { |
| 432 | wprintw (command_win,"Sorry, filesystems bigger than 2 GB are currently not supported\n"); |
| 433 | return (0); |
| 434 | } |
| 435 | return (1); |
| 436 | } |
| 437 | |
| 438 | void init_readline (void) |
| 439 | |
| 440 | { |
| 441 | rl_completion_entry_function=(Function *) complete_command; |
| 442 | } |
| 443 | |
| 444 | void init_signals (void) |
| 445 | |
| 446 | { |
Theodore Ts'o | 0f31c73 | 2002-05-11 13:03:25 -0400 | [diff] [blame] | 447 | signal (SIGWINCH, signal_SIGWINCH_handler); /* Catch SIGWINCH */ |
| 448 | signal (SIGTERM, signal_SIGTERM_handler); |
| 449 | signal (SIGSEGV, signal_SIGSEGV_handler); |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 450 | |
| 451 | } |
| 452 | |
| 453 | void signal_SIGWINCH_handler (int sig_num) |
| 454 | |
| 455 | { |
| 456 | redraw_request=1; /* We will handle it in main.c */ |
| 457 | } |
| 458 | |
| 459 | void signal_SIGTERM_handler (int sig_num) |
| 460 | |
| 461 | { |
| 462 | prepare_to_close (); |
| 463 | printf ("Terminated due to signal %d\n",sig_num); |
| 464 | exit (1); |
| 465 | } |
| 466 | |
| 467 | void signal_SIGSEGV_handler (int sig_num) |
| 468 | |
| 469 | { |
| 470 | prepare_to_close (); |
| 471 | printf ("Killed by signal %d !\n",sig_num); |
| 472 | exit (1); |
| 473 | } |
| 474 | |
| 475 | int process_configuration_file (void) |
| 476 | |
| 477 | { |
| 478 | char buffer [300]; |
| 479 | char option [80],value [80]; |
| 480 | FILE *fp; |
| 481 | |
Theodore Ts'o | b5ffead | 2002-05-11 19:17:00 -0400 | [diff] [blame] | 482 | strcpy (buffer, ETC_DIR); |
Theodore Ts'o | 583a1ce | 2002-05-11 13:00:22 -0400 | [diff] [blame] | 483 | strcat (buffer,"/ext2ed.conf"); |
| 484 | |
| 485 | if ((fp=fopen (buffer,"rt"))==NULL) { |
| 486 | fprintf (stderr,"Error - Unable to open configuration file %s\n",buffer); |
| 487 | return (0); |
| 488 | } |
| 489 | |
| 490 | while (get_next_option (fp,option,value)) { |
| 491 | if (strcasecmp (option,"Ext2Descriptors")==0) { |
| 492 | strcpy (Ext2Descriptors,value); |
| 493 | } |
| 494 | |
| 495 | else if (strcasecmp (option,"AlternateDescriptors")==0) { |
| 496 | strcpy (AlternateDescriptors,value); |
| 497 | } |
| 498 | |
| 499 | else if (strcasecmp (option,"LogFile")==0) { |
| 500 | strcpy (LogFile,value); |
| 501 | } |
| 502 | |
| 503 | else if (strcasecmp (option,"LogChanges")==0) { |
| 504 | if (strcasecmp (value,"on")==0) |
| 505 | LogChanges = 1; |
| 506 | else if (strcasecmp (value,"off")==0) |
| 507 | LogChanges = 0; |
| 508 | else { |
| 509 | fprintf (stderr,"Error - Illegal value: %s %s\n",option,value); |
| 510 | fclose (fp);return (0); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | else if (strcasecmp (option,"AllowChanges")==0) { |
| 515 | if (strcasecmp (value,"on")==0) |
| 516 | AllowChanges = 1; |
| 517 | else if (strcasecmp (value,"off")==0) |
| 518 | AllowChanges = 0; |
| 519 | else { |
| 520 | fprintf (stderr,"Error - Illegal value: %s %s\n",option,value); |
| 521 | fclose (fp);return (0); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | else if (strcasecmp (option,"AllowMountedRead")==0) { |
| 526 | if (strcasecmp (value,"on")==0) |
| 527 | AllowMountedRead = 1; |
| 528 | else if (strcasecmp (value,"off")==0) |
| 529 | AllowMountedRead = 0; |
| 530 | else { |
| 531 | fprintf (stderr,"Error - Illegal value: %s %s\n",option,value); |
| 532 | fclose (fp);return (0); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | else if (strcasecmp (option,"ForceExt2")==0) { |
| 537 | if (strcasecmp (value,"on")==0) |
| 538 | ForceExt2 = 1; |
| 539 | else if (strcasecmp (value,"off")==0) |
| 540 | ForceExt2 = 0; |
| 541 | else { |
| 542 | fprintf (stderr,"Error - Illegal value: %s %s\n",option,value); |
| 543 | fclose (fp);return (0); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | else if (strcasecmp (option,"DefaultBlockSize")==0) { |
| 548 | DefaultBlockSize = atoi (value); |
| 549 | } |
| 550 | |
| 551 | else if (strcasecmp (option,"DefaultTotalBlocks")==0) { |
| 552 | DefaultTotalBlocks = strtoul (value,NULL,10); |
| 553 | } |
| 554 | |
| 555 | else if (strcasecmp (option,"DefaultBlocksInGroup")==0) { |
| 556 | DefaultBlocksInGroup = strtoul (value,NULL,10); |
| 557 | } |
| 558 | |
| 559 | else if (strcasecmp (option,"ForceDefault")==0) { |
| 560 | if (strcasecmp (value,"on")==0) |
| 561 | ForceDefault = 1; |
| 562 | else if (strcasecmp (value,"off")==0) |
| 563 | ForceDefault = 0; |
| 564 | else { |
| 565 | fprintf (stderr,"Error - Illegal value: %s %s\n",option,value); |
| 566 | fclose (fp);return (0); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | else { |
| 571 | fprintf (stderr,"Error - Unknown option: %s\n",option); |
| 572 | fclose (fp);return (0); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | printf ("Configuration completed\n"); |
| 577 | fclose (fp); |
| 578 | return (1); |
| 579 | } |
| 580 | |
| 581 | int get_next_option (FILE *fp,char *option,char *value) |
| 582 | |
| 583 | { |
| 584 | char *ptr; |
| 585 | char buffer [600]; |
| 586 | |
| 587 | if (feof (fp)) return (0); |
| 588 | do{ |
| 589 | if (feof (fp)) return (0); |
| 590 | fgets (buffer,500,fp); |
| 591 | } while (buffer [0]=='#' || buffer [0]=='\n'); |
| 592 | |
| 593 | ptr=parse_word (buffer,option); |
| 594 | ptr=parse_word (ptr,value); |
| 595 | return (1); |
| 596 | } |
| 597 | |
| 598 | void check_mounted (char *name) |
| 599 | |
| 600 | { |
| 601 | FILE *fp; |
| 602 | char *ptr; |
| 603 | char current_line [500],current_word [200]; |
| 604 | |
| 605 | mounted=0; |
| 606 | |
| 607 | if ( (fp=fopen ("/etc/mtab","rt"))==NULL) { |
| 608 | wprintw (command_win,"Error - Failed to open /etc/mtab. Assuming filesystem is mounted.\n"); |
| 609 | refresh_command_win ();mounted=1;return; |
| 610 | }; |
| 611 | |
| 612 | while (!feof (fp)) { |
| 613 | fgets (current_line,500,fp); |
| 614 | if (feof (fp)) break; |
| 615 | ptr=parse_word (current_line,current_word); |
| 616 | if (strcasecmp (current_word,name)==0) { |
| 617 | mounted=1;fclose (fp);return; |
| 618 | } |
| 619 | }; |
| 620 | |
| 621 | fclose (fp); |
| 622 | |
| 623 | return; |
Theodore Ts'o | 0f31c73 | 2002-05-11 13:03:25 -0400 | [diff] [blame] | 624 | } |