⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfdisk.c

📁 一個相當mini的磁碟分割軟體,適合放在嵌入式系統
💻 C
📖 第 1 页 / 共 5 页
字号:
	      mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		       "Are you sure you want write the partition table "		       "to disk? (yes or no): ");	      len = get_string(response, LINE_LENGTH, NULL);	      clear_warning();	      if (len == GS_ESCAPE)		   return;	      else if (len == 2 &&		       toupper(response[0]) == 'N' &&		       toupper(response[1]) == 'O') {		   print_warning(NO_WRITE);		   return;	      } else if (len == 3 &&			 toupper(response[0]) == 'Y' &&			 toupper(response[1]) == 'E' &&			 toupper(response[2]) == 'S')		   done = TRUE;	      else		   print_warning(YES_NO);	 }	 clear_warning();	 print_warning(WRITING_PART);	 refresh();    }    read_sector(buffer.c.b, 0);    fill_primary_table(&buffer);    write_sector(buffer.c.b, 0);    for (i = 0; i < num_parts; i++)	if (IS_LOGICAL(p_info[i].num)) {	    read_sector(buffer.c.b, p_info[i].first_sector);	    fill_logical_table(&buffer, &(p_info[i]));	    write_sector(buffer.c.b, p_info[i].first_sector);	}    if (is_bdev) {	 sync();	 sleep(2);	 if (!ioctl(fd,BLKRRPART))	      changed = TRUE;	 sync();	 sleep(4);	 clear_warning();	 if (changed)	      print_warning(YES_WRITE);	 else	      print_warning(RRPART_FAILED);    } else	 print_warning(YES_WRITE);}void fp_printf(FILE *fp, char *format, ...){    va_list args;    char buf[1024];    int y, x;    va_start(args, format);    vsprintf(buf, format, args);    va_end(args);    if (fp == NULL) {	/* The following works best if the string to be printed has at           most only one newline. */	printw("%s", buf);	getyx(stdscr, y, x);	if (y >= COMMAND_LINE_Y-2) {	    menuContinue();	    erase();	    move(0, 0);	}    } else	fprintf(fp, "%s", buf);}#define MAX_PER_LINE 16void print_file_buffer(FILE *fp, char *buffer){    int i,l;    for (i = 0, l = 0; i < SECTOR_SIZE; i++, l++) {	if (l == 0)	    fp_printf(fp, "0x%03X:", i);	fp_printf(fp, " %02X", (unsigned char) buffer[i]);	if (l == MAX_PER_LINE - 1) {	    fp_printf(fp, "\n");	    l = -1;	}    }    if (l > 0)	fp_printf(fp, "\n");    fp_printf(fp, "\n");}void print_raw_table(void){    int i, to_file;    partition_table buffer;    char fname[LINE_LENGTH];    FILE *fp;    if (print_only) {	fp = stdout;	to_file = TRUE;    } else {	mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		 "Enter filename or press RETURN to display on screen: ");	if ((to_file = get_string(fname, LINE_LENGTH, NULL)) < 0)	    return;	if (to_file) {	    if ((fp = fopen(fname, "w")) == NULL) {		char errstr[LINE_LENGTH];		sprintf(errstr, PRINT_OPEN_ERR, fname);		print_warning(errstr);		return;	    }	} else {	    fp = NULL;	    erase();	    move(0, 0);	}    }    fp_printf(fp, "Disk Drive: %s\n", disk_device);    fp_printf(fp, "Sector 0:\n");    read_sector(buffer.c.b, 0);    fill_primary_table(&buffer);    print_file_buffer(fp, buffer.c.b);    for (i = 0; i < num_parts; i++)	if (IS_LOGICAL(p_info[i].num)) {	    fp_printf(fp, "Sector %d:\n", p_info[i].first_sector);	    read_sector(buffer.c.b, p_info[i].first_sector);	    fill_logical_table(&buffer, &(p_info[i]));	    print_file_buffer(fp, buffer.c.b);	}    if (to_file) {	if (!print_only)	    fclose(fp);    } else {        menuContinue();    }}void print_p_info_entry(FILE *fp, partition_info *p){    int size;    char part_str[40];    if (p->id == UNUSABLE)	fp_printf(fp, "   None   ");    else if (p->id == FREE_SPACE && p->num == PRI_OR_LOG)	fp_printf(fp, "   Pri/Log");    else if (p->id == FREE_SPACE && p->num == PRIMARY)	fp_printf(fp, "   Primary");    else if (p->id == FREE_SPACE && p->num == LOGICAL)	fp_printf(fp, "   Logical");    else	fp_printf(fp, "%2d %-7.7s", p->num+1,		  IS_LOGICAL(p->num) ? "Logical" : "Primary");    fp_printf(fp, " ");    fp_printf(fp, "%8d%c", p->first_sector,	      ((p->first_sector/(sectors*heads)) !=	       ((float)p->first_sector/(sectors*heads)) ?	       '*' : ' '));    fp_printf(fp, "%8d%c", p->last_sector,	      (((p->last_sector+1)/(sectors*heads)) !=	       ((float)(p->last_sector+1)/(sectors*heads)) ?	       '*' : ' '));    fp_printf(fp, "%7d%c", p->offset,	      ((((p->first_sector == 0 || IS_LOGICAL(p->num)) &&		 (p->offset != sectors)) ||		(p->first_sector != 0 && IS_PRIMARY(p->num) &&		 p->offset != 0)) ?	       '#' : ' '));    size = p->last_sector - p->first_sector + 1;    fp_printf(fp, "%8d%c", size,	      ((size/(sectors*heads)) != ((float)size/(sectors*heads)) ?	       '*' : ' '));    fp_printf(fp, " ");    if (p->id == UNUSABLE)	sprintf(part_str, "%.17s", "Unusable");    else if (p->id == FREE_SPACE)	sprintf(part_str, "%.17s", "Free Space");    else if (partition_type[p->id])	sprintf(part_str, "%.17s (%02X)", partition_type[p->id], p->id);    else	sprintf(part_str, "%.17s (%02X)", "Unknown", p->id);    fp_printf(fp, "%-22.22s", part_str);    fp_printf(fp, " ");    if (p->flags == ACTIVE_FLAG)	fp_printf(fp, "Boot (%02X)", p->flags);    else if (p->flags != 0)	fp_printf(fp, "Unknown (%02X)", p->flags);    else	fp_printf(fp, "None (%02X)", p->flags);    fp_printf(fp, "\n");}void print_p_info(void){    char fname[LINE_LENGTH];    FILE *fp;    int i, to_file, pext = is_extended(ext_info.id);    if (print_only) {	fp = stdout;	to_file = TRUE;    } else {	mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		 "Enter filename or press RETURN to display on screen: ");	if ((to_file = get_string(fname, LINE_LENGTH, NULL)) < 0)	    return;	if (to_file) {	    if ((fp = fopen(fname, "w")) == NULL) {		char errstr[LINE_LENGTH];		sprintf(errstr, PRINT_OPEN_ERR, fname);		print_warning(errstr);		return;	    }	} else {	    fp = NULL;	    erase();	    move(0, 0);	}    }    fp_printf(fp, "Partition Table for %s\n", disk_device);    fp_printf(fp, "\n");    fp_printf(fp, "            First    Last\n");    fp_printf(fp, " # Type     Sector   Sector   Offset  Length   Filesystem Type (ID)   Flags\n");    fp_printf(fp, "-- ------- -------- --------- ------ --------- ---------------------- ---------\n");    for (i = 0; i < num_parts; i++) {	if (pext && (p_info[i].first_sector >= ext_info.first_sector)) {	    print_p_info_entry(fp,&ext_info);	    pext = FALSE;	}	print_p_info_entry(fp, &(p_info[i]));    }    if (to_file) {	if (!print_only)	    fclose(fp);    } else {        menuContinue();    }}void print_part_entry(FILE *fp, int num, partition_info *pi){    int first = 0, start = 0, end = 0, size = 0;    int ss = 0, sh = 0, sc = 0;    int es = 0, eh = 0, ec = 0;    int flags = 0, id = 0;    if (pi != NULL) {	flags = pi->flags;	id = pi->id;	if (IS_LOGICAL(num))	    first = pi->offset;	else	    first = pi->first_sector + pi->offset;	start = pi->first_sector + pi->offset;	end = pi->last_sector;	size = end - start + 1;	if ((start/(sectors*heads)) > 1023)	    start = heads*sectors*1024 - 1;	if ((end/(sectors*heads)) > 1023)	    end = heads*sectors*1024 - 1;	ss = start % sectors + 1;	start /= sectors;	sh = start % heads;	sc = start / heads;	es = end % sectors + 1;	end /= sectors;	eh = end % heads;	ec = end / heads;    }    fp_printf(fp, "%2d  0x%02X %4d %4d %4d 0x%02X %4d %4d %4d %8d %9d\n",	      num+1, flags, sh, ss, sc, id, eh, es, ec, first, size);}void print_part_table(void){    int i, j, to_file;    char fname[LINE_LENGTH];    FILE *fp;    if (print_only) {	fp = stdout;	to_file = TRUE;    } else {	mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		 "Enter filename or press RETURN to display on screen: ");	if ((to_file = get_string(fname, LINE_LENGTH, NULL)) < 0)	    return;	if (to_file) {	    if ((fp = fopen(fname, "w")) == NULL) {		char errstr[LINE_LENGTH];		sprintf(errstr, PRINT_OPEN_ERR, fname);		print_warning(errstr);		return;	    }	} else {	    fp = NULL;	    erase();	    move(0, 0);	}    }    fp_printf(fp, "Partition Table for %s\n", disk_device);    fp_printf(fp, "\n");    fp_printf(fp, "         ---Starting---      ----Ending----    Start Number of\n");    fp_printf(fp, " # Flags Head Sect Cyl   ID  Head Sect Cyl    Sector  Sectors\n");    fp_printf(fp, "-- ----- ---- ---- ---- ---- ---- ---- ---- -------- ---------\n");    for (i = 0; i < 4; i++) {	for (j = 0;	     j < num_parts && (p_info[j].id <= 0 || p_info[j].num != i);	     j++);	if (j < num_parts) {	    print_part_entry(fp, i, &(p_info[j]));	} else if (is_extended(ext_info.id) && ext_info.num == i) {	    print_part_entry(fp, i, &ext_info);	} else {	    print_part_entry(fp, i, NULL);	}    }    for (i = 0; i < num_parts; i++)	if (IS_LOGICAL(p_info[i].num))	    print_part_entry(fp, p_info[i].num, &(p_info[i]));    if (to_file) {	if (!print_only)	    fclose(fp);    } else {        menuContinue();    }}void print_tables(void){    int done = FALSE;    static struct MenuItem menuFormat[]=    {        { 'r', "Raw", "Print the table using raw data format" },        { 's', "Sectors", "Print the table ordered by sectors" },        { 't', "Table", "Just print the partition table" },        { ESC, "Cancel", "Don't print the table" },        { 0, NULL, NULL }    };        while (!done)	switch ( toupper(menuSimple( menuFormat, 2)) ) {	case 'R':	    print_raw_table();	    done = TRUE;	    break;	case 'S':	    print_p_info();	    done = TRUE;	    break;	case 'T':	    print_part_table();	    done = TRUE;	    break;	case ESC:	    done = TRUE;	    break;	}}#define END_OF_HELP "EOHS!"#define NEW_HELP_SCREEN "SNHS!"void display_help(){    char *help_text[] = {	"Help Screen for cfdisk " VERSION,	"",	"This is cfdisk, a curses based disk partitioning programs, which",	"allows you to create, delete and modify partitions on your hard",	"disk drive.",	"",	"Copyright (C) 1994-1997 Kevin E. Martin & aeb",	"",	"Command      Meaning",	"-------      -------",	"  b          Toggle bootable flag of the current partition",	"  d          Delete the current partition",	"  g          Change cylinders, heads, sectors-per-track parameters",	"             WARNING: This option should only be used by people who",	"             know what they are doing.",	"  h          Print this screen",	"  m          Maximize disk usage of the current partition",	"             Note: This may make the partition incompatible with",	"             DOS, OS/2, ...",	"  n          Create new partition from free space",	"  p          Print partition table to the screen or to a file",	"             There are several different formats for the partition",	"             that you can choose from:",	"                r - Raw data (exactly what would be written to disk)",	"                s - Table ordered by sectors",	"                t - Table in raw format",	"  q          Quit program without writing partition table",	"  t          Change the filesystem type",	"  u          Change units of the partition size display",	"             Rotates through Mb, sectors and cylinders",	"  W          Write partition table to disk (must enter upper case W)",        "             Since this might destroy data on the disk, you must",	"             either confirm or deny the write by entering `yes' or",	"             `no'",	"Up Arrow     Move cursor to the previous partition",	"Down Arrow   Move cursor to the next partition",	"CTRL-L       Redraws the screen",	"  ?          Print this screen",	"",	"Note: All of the commands can be entered with either upper or lower",	"case letters (except for Writes).",	END_OF_HELP    };    int cur_line = 0;    FILE *fp = NULL;    erase();    move(0, 0);    while (strcmp(help_text[cur_line], END_OF_HELP))	if (!strcmp(help_text[cur_line], NEW_HELP_SCREEN)) {	    menuContinue();	    erase();	    move(0, 0);	    cur_line++;	} else	    fp_printf(fp, "%s\n", help_text[cur_line++]);    menuContinue();}int change_geometry(void){    int ret_val = FALSE;    int done = FALSE;    char def[LINE_LENGTH];    char response[LINE_LENGTH];    int tmp_val;    while (!done) {        static struct MenuItem menuGeometry[]=        {            { 'c', "Cylinders", "Change cylinder geometry" },            { 'h', "Heads", "Change head geometry" },            { 's', "Sectors", "Change sector geometry" },            { 'd', "Done", "Done with changing geometry" },

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -