PFDISK.C
资源名称:bootmenu.zip [点击查看]
上传用户:yeshiping1
上传日期:2007-01-06
资源大小:29k
文件大小:16k
源码类别:
磁盘编程
开发平台:
Others
- /*
- * pfdisk - Partition a Fixed DISK
- * by Gordon W. Ross, Jan. 1990
- *
- * See the file "pfdisk.doc" for user instructions.
- *
- * This program uses a simple, line-oriented interpreter,
- * designed for both interactive and non-interactive use.
- * To facilitate non-interactive use, the output from the
- * 'L' (list partitions) command is carefully arranged so it
- * can be used directly as command input. Neat trick, eh?
- */
- char *versionString =
- "# pfdisk version 1.2 by Gordon W. Ross Aug. 1990n";
- /* These don't really matter. The user is asked to set them. */
- #define DEFAULT_CYLS 306
- #define DEFAULT_HEADS 4
- #define DEFAULT_SECTORS 17
- #define PROMPT_STRING "pfdisk> "
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include "sysdep.h"
- #include "syscodes.h"
- typedef unsigned char uchar;
- typedef unsigned int uint;
- typedef unsigned long ulong;
- struct part { /* An entry in the partition table */
- uchar active; /* active flag (0x80 or 0) */
- uchar b_head; /* begin head */
- uchar b_sec; /* sector */
- uchar b_cyl; /* cylinder */
- uchar sysid; /* system id (see sysid.c) */
- uchar e_head; /* end head */
- uchar e_sec; /* end sector */
- uchar e_cyl; /* end cylinder */
- /* These two are just longs, but this way is machine independent. */
- uchar lsBeg[4]; /* logical sectors, beginning */
- uchar lsLen[4]; /* logical sectors, length */
- };
- #define LOC_PT 0x1BE
- #define LOC_NT 0x180
- #define LOC_GWR 0x1A0
- #define MAGIC_LOC 0x1FE
- #define MAGIC_0 0x55
- #define MAGIC_1 0xAA
- #define MAX_LINE 80
- char buffer[SECSIZE]; /* The boot block buffer */
- int bufmod=0; /* buffer modified... */
- /* (zero means buffer is unmodified) */
- int useNTable; /* boot sector uses name table */
- /* device parameters (force someone to set them!) */
- unsigned cyls = DEFAULT_CYLS;
- unsigned heads = DEFAULT_HEADS;
- unsigned sectors = DEFAULT_SECTORS;
- char *devname; /* device name */
- char cmdline[MAX_LINE];
- char filename[80]; /* used by r/w commands */
- char *prompt; /* null if no tty input */
- /* Some of these strings are used in more than one place.
- * For consistency, I put a newline on all of them.
- */
- char h_h[] = "? <enter> : Help summaryn";
- char h_l[] = "L : List partition tablen";
- char h_1[] = "1 id first last name : set partition 1n";
- char h_2[] = "2,3,4 ... (like 1) : set respective partitionn";
- char h_a[] = "A n : Activate partition nn";
- char h_g[] = "G cyls heads sectors : set disk Geometryn";
- char h_i[] = "I : list known ID numbersn";
- char h_r[] = "R [optional-file] : Read device (or specified file)n";
- char h_w[] = "W [optional-file] : Write device (or specified file)n";
- char h_q[] = "Q[!] : Quit (! means force)n";
- char * helpTable[] = {
- h_h, h_l, h_1, h_2, h_a, h_g, h_i, h_r, h_w, h_q,
- "# (All command letters have lower-case equivalents.)n",
- (char *) 0 }; /* This MUST have a zero as the last element */
- char *BadArg="Error: bad argument: %sn";
- char *WarnNotSaved =
- "Warning, modified partition table not saved.n";
- help()
- {
- char ** p;
- for (p = helpTable; *p; p++)
- printf(*p);
- }
- /* forward declarations */
- void checkValidity();
- char * setPartition();
- char * makeActive();
- char * setGeometry();
- ulong chs2long();
- char * nameID();
- int printIDs();
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char *cmdp; /* points to command word */
- char *argp; /* points to command args */
- /* check command line args (device name) */
- if (argc != 2) {
- usage(argv[0]); /* See s-sysname.c */
- exit(1);
- }
- devname = argv[1];
- /* Should we prompt? */
- prompt = (isatty(fileno(stdin))) ? PROMPT_STRING : (char *) 0;
- /* Print version name. */
- fputs(versionString, stderr);
- /* get disk parameters */
- getGeometry(devname,&cyls,&heads,§ors);
- /* Get the boot block. */
- if (getBBlk(devname, buffer) < 0)
- fprintf(stderr,"%s: read failedn", devname);
- checkValidity();
- if (prompt) fprintf(stderr,"For help, enter: '?'n");
- /* Read and process commands a line at a time. */
- while (1) {
- if (prompt) fputs(prompt,stdout);
- if (! fgets(cmdline, MAX_LINE, stdin)) break;
- /* Find beginning of command word */
- cmdp = cmdline;
- while (isspace(*cmdp)) cmdp++;
- /* find beginning of args */
- argp = cmdp;
- while (*argp && !isspace(*argp)) argp++;
- while (isspace(*argp) || *argp=='=') argp++;
- switch (*cmdp) {
- case ' ': /* blank line */
- case '#': /* line comment */
- break;
- case '?': case 'h': case 'H':
- help();
- break;
- case '1': /* set partition entry */
- case '2': case '3': case '4':
- argp = setPartition(cmdp, argp);
- if (argp) { /* arg list error */
- fprintf(stderr,BadArg,argp);
- fprintf(stderr,h_1);
- fprintf(stderr,h_2);
- break;
- }
- bufmod = 1;
- break;
- case 'a': case 'A': /* activate partition */
- argp = makeActive(argp);
- if (argp) {
- fprintf(stderr,BadArg,argp);
- fprintf(stderr,h_a);
- break;
- }
- bufmod = 1;
- break;
- case 'g': case 'G': /* set disk parameters (Geometry) */
- argp = setGeometry(argp);
- if (argp) { /* arg list error */
- fprintf(stderr,BadArg,argp);
- fprintf(stderr,h_g);
- }
- break;
- case 'i': case 'I': /* List known ID numbers */
- printIDs();
- break;
- case 'l': case 'L': /* List the partition table */
- listPTable();
- break;
- case 'q': case 'Q': /* Quit */
- if (bufmod && (cmdp[1] != '!')) {
- fprintf(stderr,"