File: fdisk.c

package info (click to toggle)
amiga-fdisk 0.04-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ansic: 1,316; makefile: 197; sh: 35
file content (399 lines) | stat: -rw-r--r-- 8,645 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*                  AFDISK  -  Amiga RDB Fdisk for Linux
 *              fdisk.c part  -  generic input output stuff
 *                   written in 1996 by Stefan Reinauer
 *
 *    Copyright (C) 1996-1998 Stefan Reinauer
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2, or (at your option)
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <getopt.h>
#include <ctype.h>

#ifdef DONT_USE_READLINE
char *readline (const char *prompt)
{
       char buffer[1024];
       char *s;
       int size;
       printf ("%s",prompt);
       fflush (stdout);
       fgets (buffer, sizeof(buffer), stdin);
       size = strlen (buffer);
       s = malloc ((size+1)*sizeof(char));
       s = strcpy (s, buffer);
       fflush (stdin);
       return s;
}
#else
#include <readline/readline.h>
#include <readline/history.h>     
#endif

#if 0
#include <asm/byteorder.h>
#else
#include <netinet/in.h>
#endif

#include <amiga/types.h>
#include <amigastuff.h>
#include <implemented.h>

#define VERSION "0.04"
#define RELDATE "98/11/03"

char *testdevs[] = { "/dev/hda", "/dev/hdb", "/dev/hdc", "/dev/hdd",
                     "/dev/sda", "/dev/sdb", "/dev/sdc", "/dev/sdd",
                     "/dev/sde", "/dev/sdf", "/dev/sdg", "/dev/sdh",
                     NULL };

char disk_device[256]="/dev/sda";
int  list_only=0, get_dev=0;

int atonum(char *s)
{
#ifdef FIX_NO_READLINE
#warning FIX_NO_READLINE enabled in atonum
        return atoi(s);
#else
	char *d=s;
	unsigned int n;

	while (*s == ' ') /* Eat spaces */
		s++;

	/* 0x is hex, 0 is octal, everything else is decimal. */
	if (strncmp(s, "0x", 2) == 0 || strncmp(s, "0X", 2) == 0)
		sscanf(s + 2, "%x", &n);
	else if (s[0] == '0' && s[1])
		sscanf(s + 1, "%o", &n);
	else {
		d=s;
		while (d[0]!=0) {
			if (!isdigit( ((int)(d[0])) ))
				return 0;
			d++;
		}
		sscanf(s, "%u", &n);
	}
	return n;
#endif
}

int partition(void)
{
	char *partnum;
	int i;

	partition_show();

	partnum=readline("Partition number: ");
	if (partnum==NULL) exit(0);
	i=atonum(partnum);
	free(partnum);

	return i;
}

int override(void)
{
	char *ovr;
	int i=0;
	if (!list_only) {
		ovr=readline("Do you want to override this error? [y/N] ");
		if (ovr==NULL) exit(0);
		if (ovr[0]=='y' || ovr[0]=='Y') i=1;
		free(ovr);
	} else {
		fprintf (stderr,"Overriding last error.\n");
		i=1;
	}

	return i;
}

int listknown(void)
{
	printf("Known Partition Types\n---------------------\n\n\
 1.  DOS\\0	Old Filesystem\n\
 2.  DOS\\1	Original FastFileSystem (FFS)\n\
 3.  DOS\\2	Old International Filesystem\n\
 4.  DOS\\3	International FastFileSystem\n\
 5.  DOS\\4	Old Filesystem with directory cache\n\
 6.  DOS\\6	FastFileSystem with directory cache\n\
 7.  UNI\\0	classic AT&T System-V filesystem\n\
 8.  UNI\\1	UNIX boot \"filesystem\" (dummy entry for Amiga OS's boot menu)\n\
 9.  UNI\\2	Berkeley filesystem for System V\n\
10.  RESV	Reserved (e.g. swap space)\n\
11.  LNX\\0	Linux native\n\
12.  SWP\\0	Linux swap\n\n");

	return 0;
}

int menu(void)
{
	printf ("Command action\n\
   a   toggle bootable flag\n\
   b   change number of bootblocks\n\
   c   change boot priority\n\
   d   delete a partition\n\
   e   toggle nomount flag\n\
   l   list known partition types\n\
   m   print this menu\n\
   n   add a new partition\n\
   p   print the partition table\n\
   q   quit without saving changes\n\
   t   change a partition's DosType\n\
   r   reorganize/move RDB\n\
   w   write table to disk and exit\n\n");
	return 0;
}

int version(void)
{
	printf ("amiga-fdisk %s (%s) by Stefan Reinauer, <stepan@linux.de>\n\n",VERSION,RELDATE);
	return 0;
}

ULONG gettype(void)
{
	char *dtype;
	ULONG hexval,ascii;

	listknown();

	printf ("\n  Choose a number of the upper list or\n  enter a DosType of the format 0x444f5302\n\n");
#ifdef FIX_NO_READLINE
#warning FIX_NO_READLINE enabled in gettype
	printf(
	"(NOTE: amiga-fdisk from the boot-floppies package accepts only numbers\n"\
	" from the list above. Replace amiga-fdisk-bf by amiga-fdisk as soon as\n"\
	" possible to get the full functionality of amiga-fdisk.)\n\n");
#endif
	dtype=readline("Enter DosType: ");
	if (dtype==NULL) exit (0);
	hexval=atonum(dtype);
        ascii=ntohl(*(ULONG *)dtype);
	free(dtype);

	switch (hexval) {
		case 0:  return ascii;
		case 1:  return 0x444f5300;
		case 2:  return 0x444f5301;
		case 3:  return 0x444f5302;
		case 4:  return 0x444f5303;
		case 5:  return 0x444f5304;
		case 6:  return 0x444f5305;
		case 7:  return 0x554e4900;
		case 8:  return 0x554e4901;
		case 9:  return 0x554e4902;
		case 10: return 0x52455356;
		case 11: return 0x4C4E5800;
		case 12: return 0x53575000;
		default: return (hexval);
	}
	return 0;
}

 
int main(int argc, char **argv)
{
	int  c, count;
	char *entry, *optinput;

	/* Parsing all command line parameters */

	while(((c = getopt(argc, argv, "-dvls:TViN:A:u:xC:H:S:fqLnRO:I:?")) != EOF))
	switch(c) {
	case 1:
		get_dev=1;
		strcpy(disk_device, optarg);
		break;
	case 'v':
		version();
		return 0;
	case '?':
		printf ("\nUsage: %s [-l] [-v] [-d] [-s /dev/hdxn] [/dev/hdx]\n",argv[0]);
		return 0;
	case 'l':
		list_only = 1;
		break;
	case 'T':
		version();
		listknown();
		return 0;
	default:
		count=0;
		while (functions[count].key!='!') {
			if (functions[count].key==c) {
				printf ("Option \"%c\" - \"%s\" - not yet implemented.\n",c, functions[count].desc);
				break;
			}
			count++;
		}
		return 0;
	}


	/* switch to selected mode and look for valid device */

	if (list_only) {
		if (get_dev) {
			if (get_rdb()!=-1) {
				rigiddisk_show();
				partition_show();
				printf ("\n");
			}
		} else {
			count=0;
			while (testdevs[count] != NULL) {
				strcpy(disk_device, testdevs[count++]);

				if (get_rdb()!=-1) {
					rigiddisk_show();
					partition_show();
					printf ("\n");
				}
			}
		}
		return 0;

	} else {
		if (!get_dev)
			printf ("Using %s as default device!\n",disk_device);
		if (get_rdb()==-1)
			 return -1;
	}

	printf ("\n");  

	/* Entering interactive mode */

	for (;;) {

#ifdef DEBUG
		pt();
#endif

		entry=readline(  "Command (m for help): ");
		if (entry==NULL) {
			quitall();
			return 0;
		}
		switch ((*entry)) {
		case 'q':
		case 'Q':
			quitall();
			free(entry);
			return 0;
		case 'l':
		case 'L':
			listknown();
			break;
		case 'p':
		case 'P':
			printf ("\n");
			rigiddisk_show();
			partition_show();
			printf ("\n");
			break;
		case 'a':
		case 'A':
			partition_togglebootable(partition());
			break;
		case 'b':
		case 'B':
			count=partition();
			optinput=readline("Number of bootblocks: ");
			if (optinput==NULL) return 0;
			partition_bootblk(count,atonum(optinput));
			free(optinput);
			break;
		case 'c':
		case 'C':
			count=partition();
			optinput=readline("New Boot Priority: ");
			if (optinput==NULL) return 0;
			partition_bootpri(count,atonum(optinput));
			free(optinput);
			break;
		case 'd':
		case 'D':
			partition_delete(partition());
			break;
		case 'e':
		case 'E':
			partition_togglenomount(partition());
			break;
		case 'n':
		case 'N':
			if (room_for_partition()!=-1) {
				count=partition();
				if (partition_new(count)!=-1) {
					optinput=readline("Low Cylinder: ");
					if (optinput==NULL) return 0;
					partition_locyl(count,atonum(optinput));
					free(optinput);

					optinput=readline("High Cylinder: ");
					if (optinput==NULL) return 0;
					partition_hicyl(count,atonum(optinput));
					free(optinput);
				}
			}
			break;
		case 't':
		case 'T':
			count=partition();
			partition_dostype(count,gettype());
			break;

		case 'w':
		case 'W':
			free(entry);
			rigiddisk_reorg(firstblock);
			rigiddisk_save();
			return 0;
		case 'r':
		case 'R':
			printf ("\n");
			rigiddisk_show();
			printf ("\n");
			optinput=readline(" RDB start at: ");
			if (optinput==NULL) return 0;
			rigiddisk_reorg(atonum(optinput));
			free(optinput);
			break;
		case '\0':
			break;
		case 'm':
		case 'M':
		default:
			menu();
			break;
		}

		free(entry);

	}
}