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