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

📄 cfdisk.c

📁 一個相當mini的磁碟分割軟體,適合放在嵌入式系統
💻 C
📖 第 1 页 / 共 5 页
字号:
            { 0, NULL, NULL }        };	move(COMMAND_LINE_Y, COMMAND_LINE_X);	clrtoeol();	refresh();	clear_warning();	switch (toupper( menuSimple(menuGeometry, 3) )) {	case 'C':	    sprintf(def, "%d", cylinders);	    mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		     "Enter the number of cylinders: ");	    if (get_string(response, LINE_LENGTH, def) > 0) {		tmp_val = atoi(response);		if (tmp_val > 0 && tmp_val <= MAX_CYLINDERS) {		    cylinders = tmp_val;		    ret_val = TRUE;		} else		    print_warning(BAD_CYLINDERS);	    }	    break;	case 'H':	    sprintf(def, "%d", heads);	    mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		     "Enter the number of heads: ");	    if (get_string(response, LINE_LENGTH, def) > 0) {		tmp_val = atoi(response);		if (tmp_val > 0 && tmp_val <= MAX_HEADS) {		    heads = tmp_val;		    ret_val = TRUE;		} else		    print_warning(BAD_HEADS);	    }	    break;	case 'S':	    sprintf(def, "%d", sectors);	    mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X,		     "Enter the number of sectors per track: ");	    if (get_string(response, LINE_LENGTH, def) > 0) {		tmp_val = atoi(response);		if (tmp_val > 0 && tmp_val <= MAX_SECTORS) {		    sectors = tmp_val;		    ret_val = TRUE;		} else		    print_warning(BAD_SECTORS);	    }	    break;	case ESC:	case 'D':	    done = TRUE;	    break;	default:	    putchar(BELL);	    break;	}    }    if (ret_val) {	int disk_end = heads*sectors*cylinders-1;	if (p_info[num_parts-1].last_sector > disk_end) {	    while (p_info[num_parts-1].first_sector > disk_end) {		if (p_info[num_parts-1].id == FREE_SPACE ||		    p_info[num_parts-1].id == UNUSABLE)		    remove_part(num_parts-1);		else		    del_part(num_parts-1);	    }	    p_info[num_parts-1].last_sector = disk_end;	    if (ext_info.last_sector > disk_end)		ext_info.last_sector = disk_end;	} else if (p_info[num_parts-1].last_sector < disk_end) {	    if (p_info[num_parts-1].id == FREE_SPACE ||		p_info[num_parts-1].id == UNUSABLE) {		p_info[num_parts-1].last_sector = disk_end;	    } else {		insert_empty_part(num_parts,				  p_info[num_parts-1].last_sector+1,				  disk_end);	    }	}	/* Make sure the partitions are correct */	check_part_info();    }    return ret_val;}void change_id(int i){    char id[LINE_LENGTH], def[LINE_LENGTH];    int num_types = 0;    int num_across, num_down;    int len, new_id = LINUX;    int y_start, y_end;    int j, pos;    for (num_types = 0, j = 1; j < NUM_PART_TYPES; j++)	if (partition_type[j])	    num_types++;    num_across = COLS/COL_ID_WIDTH;    num_down = (((float)num_types)/num_across + 1);    y_start = COMMAND_LINE_Y - 1 - num_down;    if (y_start > DISK_TABLE_START+cur_part+4)	y_start = DISK_TABLE_START+cur_part+4;    y_end = y_start + num_down - 1;    for (j = y_start - 1; j <= y_end + 1; j++) {	move(j, 0);	clrtoeol();    }    for (pos = 0, j = 1; j < NUM_PART_TYPES; j++)	if (partition_type[j]) {	    move(y_start + pos % num_down, (pos/num_down)*COL_ID_WIDTH + 1);	    printw("%02X %-16.16s", j, partition_type[j]);	    pos++;	}    sprintf(def, "%02X", new_id);    mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X, "Enter filesystem type: ");    if ((len = get_string(id, 2, def)) <= 0 && len != GS_DEFAULT)	return;    if (len != GS_DEFAULT) {	if (!isxdigit(id[0]))	    return;	new_id = (isdigit(id[0]) ? id[0] - '0' : tolower(id[0]) - 'a' + 10);	if (len == 2)	    if (isxdigit(id[1]))		new_id = new_id*16 +		    (isdigit(id[1]) ? id[1] - '0' : tolower(id[1]) - 'a' + 10);	    else		return;    }    if (new_id == 0)	print_warning(ID_EMPTY);    else if (is_extended(new_id))	print_warning(ID_EXT);    else	p_info[i].id = new_id;}void draw_partition(int i){    int size, j;    int y = i + DISK_TABLE_START + 2 - (cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN;    char *t;    if (!arrow_cursor) {	move(y, 0);	for (j = 0; j < COLS; j++)	    addch(' ');    }    if (p_info[i].id > 0) {	mvprintw(y, NAME_START,		 "%s%d", disk_device, p_info[i].num+1);	if (p_info[i].flags) {	    if (p_info[i].flags == ACTIVE_FLAG)		mvaddstr(y, FLAGS_START, "Boot");	    else		mvprintw(y, FLAGS_START, "Unk(%02X)", p_info[i].flags);	    if (p_info[i].first_sector == 0 || IS_LOGICAL(p_info[i].num)) {		if (p_info[i].offset != sectors)		    addstr(", NC");	    } else {		if (p_info[i].offset != 0)		    addstr(", NC");	    }	} else {	    if (p_info[i].first_sector == 0 || IS_LOGICAL(p_info[i].num)) {		if (p_info[i].offset != sectors)		    mvaddstr(y, FLAGS_START, "NC");	    } else {		if (p_info[i].offset != 0)		    mvaddstr(y, FLAGS_START, "NC");	    }	}    }    mvaddstr(y, PTYPE_START,	     (p_info[i].id == UNUSABLE ? "" :	      (IS_LOGICAL(p_info[i].num) ? "Logical" :	       (p_info[i].num >= 0 ? "Primary" :		(p_info[i].num == PRI_OR_LOG ? "Pri/Log" :		 (p_info[i].num == PRIMARY ? "Primary" : "Logical"))))));    t = partition_type_text(i);    if (t)	 mvaddstr(y, FSTYPE_START, t);    else	 mvprintw(y, FSTYPE_START, "Unknown (%02X)", p_info[i].id);    if (p_info[i].volume_label[0]) {	int l = strlen(p_info[i].volume_label);	int s = SIZE_START-5-l;	mvprintw(y, (s > LABEL_START) ? LABEL_START : s,		 " [%s]  ", p_info[i].volume_label);    }    size = p_info[i].last_sector - p_info[i].first_sector + 1;    if (display_units == SECTORS)	mvprintw(y, SIZE_START, "%9d", size);    else if (display_units == CYLINDERS)	mvprintw(y, SIZE_START, "%9d", size/(sectors*heads));    else	mvprintw(y, SIZE_START, "%9.2f", ceiling(size/20.48)/100);    if (((size/(sectors*heads)) != ceiling(size/(sectors*(float)heads))) ||	((p_info[i].first_sector/(sectors*heads)) !=	 ceiling(p_info[i].first_sector/(sectors*heads))))	mvprintw(y, COLUMNS-1, "*");}void init_const(void){    if (!defined) {	NAME_START = (((float)NAME_START)/COLUMNS)*COLS;	FLAGS_START = (((float)FLAGS_START)/COLUMNS)*COLS;	PTYPE_START = (((float)PTYPE_START)/COLUMNS)*COLS;	FSTYPE_START = (((float)FSTYPE_START)/COLUMNS)*COLS;	LABEL_START = (((float)LABEL_START)/COLUMNS)*COLS;	SIZE_START = (((float)SIZE_START)/COLUMNS)*COLS;	COMMAND_LINE_X = (((float)COMMAND_LINE_X)/COLUMNS)*COLS;	COMMAND_LINE_Y = LINES - 4;	WARNING_START = LINES - 2;	if ((NUM_ON_SCREEN = COMMAND_LINE_Y - DISK_TABLE_START - 3) <= 0)	    NUM_ON_SCREEN = 1;	COLUMNS = COLS;	defined = TRUE;    }}void draw_screen(void){    int i;    char *line;    line = (char *)malloc((COLS+1)*sizeof(char));    if (warning_last_time) {	for (i = 0; i < COLS; i++) {	    move(WARNING_START, i);	    line[i] = inch();	}	line[COLS] = 0;    }    erase();    if (warning_last_time)	mvaddstr(WARNING_START, 0, line);    sprintf(line, "cfdisk %s", VERSION);    mvaddstr(HEADER_START, (COLS-strlen(line))/2, line);    sprintf(line, "Disk Drive: %s", disk_device);    mvaddstr(HEADER_START+2, (COLS-strlen(line))/2, line);    sprintf(line, "Heads: %d   Sectors per Track: %d   Cylinders: %d",	    heads, sectors, cylinders);    mvaddstr(HEADER_START+3, (COLS-strlen(line))/2, line);    mvaddstr(DISK_TABLE_START, NAME_START, "Name");    mvaddstr(DISK_TABLE_START, FLAGS_START, "Flags");    mvaddstr(DISK_TABLE_START, PTYPE_START-1, "Part Type");    mvaddstr(DISK_TABLE_START, FSTYPE_START, "FS Type");    mvaddstr(DISK_TABLE_START, LABEL_START+1, "[Label]");    if (display_units == SECTORS)	mvaddstr(DISK_TABLE_START, SIZE_START, "  Sectors");    else if (display_units == CYLINDERS)	mvaddstr(DISK_TABLE_START, SIZE_START, "Cylinders");    else	mvaddstr(DISK_TABLE_START, SIZE_START, "Size (MB)");    move(DISK_TABLE_START+1, 1);    for (i = 1; i < COLS-1; i++)	addch('-');    if (NUM_ON_SCREEN >= num_parts)	for (i = 0; i < num_parts; i++)	    draw_partition(i);    else	for (i = (cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN;	     i < NUM_ON_SCREEN + (cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN &&	     i < num_parts;	     i++)	    draw_partition(i);    free(line);}int draw_cursor(int move){    if (move != 0 && (cur_part + move < 0 || cur_part + move >= num_parts))	return -1;    if (arrow_cursor)	mvaddstr(DISK_TABLE_START + cur_part + 2		 - (cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN, 0, "   ");    else	draw_partition(cur_part);    cur_part += move;    if (((cur_part - move)/NUM_ON_SCREEN)*NUM_ON_SCREEN !=	(cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN)	draw_screen();    if (arrow_cursor)	mvaddstr(DISK_TABLE_START + cur_part + 2		 - (cur_part/NUM_ON_SCREEN)*NUM_ON_SCREEN, 0, "-->");    else {	standout();	draw_partition(cur_part);	standend();    }    return 0;}void do_curses_fdisk(void){    int done = FALSE;    char command;    static struct MenuItem menuMain[]=    {        { 'b', "Bootable", "Toggle bootable flag of the current partition" },        { 'd', "Delete", "Delete the current partition" },        { 'g', "Geometry", "Change disk geometry (experts only)" },        { 'h', "Help", "Print help screen" },        { 'm', "Maximize", "Maximize disk usage of the current partition (experts only)" },        { 'n', "New", "Create new partition from free space" },        { 'p', "Print", "Print partition table to the screen or to a file" },        { 'q', "Quit", "Quit program without writing partition table" },        { 't', "Type", "Change the filesystem type (DOS, Linux, OS/2 and so on)" },        { 'u', "Units", "Change units of the partition size display (MB, sect, cyl)" },        { 'W', "Write", "Write partition table to disk (this might destroy data)" },        { 0, NULL, NULL }    };    curses_started = 1;    initscr();    old_SIGINT = signal(SIGINT, die);    old_SIGTERM = signal(SIGTERM, die);#ifdef DEBUG    signal(SIGINT, old_SIGINT);    signal(SIGTERM, old_SIGTERM);#endif    cbreak();    noecho();    nonl();    init_const();    fill_p_info();    draw_screen();    while (!done) {	char *s;	(void)draw_cursor(0);	if (p_info[cur_part].id == FREE_SPACE) {	    s = ((opentype == O_RDWR) ? "hnpquW" : "hnpqu");	    command = menuSelect(COMMAND_LINE_Y, COMMAND_LINE_X, menuMain, 8,	        s, MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, 0);	} else if (p_info[cur_part].id > 0) {	    s = ((opentype == O_RDWR) ? "bdhmpqtuW" : "bdhmpqtu");	    command = menuSelect(COMMAND_LINE_Y, COMMAND_LINE_X, menuMain, 8,	        s, MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, 0);	} else {	    s = ((opentype == O_RDWR) ? "hpquW" : "hpqu");	    command = menuSelect(COMMAND_LINE_Y, COMMAND_LINE_X, menuMain, 8,	        s, MENU_HORIZ | MENU_BUTTON | MENU_ACCEPT_OTHERS, 0);	}	switch ( command ) {	case 'B':	case 'b':	    if (p_info[cur_part].id > 0)		p_info[cur_part].flags ^= 0x80;	    else		print_warning(NO_FLAGS);	    break;	case 'D':	case 'd':	    if (p_info[cur_part].id > 0) {		del_part(cur_part);		if (cur_part >= num_parts)		    cur_part = num_parts - 1;		draw_screen();	    } else		print_warning(DEL_EMPTY);	    break;	case 'G':	case 'g':	    if (change_geometry())		draw_screen();	    break;	case 'M':	case 'm':	    if (p_info[cur_part].id > 0) {		if (p_info[cur_part].first_sector == 0 ||		    IS_LOGICAL(p_info[cur_part].num)) {		    if (p_info[cur_part].offset == sectors)			p_info[cur_part].offset = 1;		    else			p_info[cur_part].offset = sectors;		    draw_screen();		} else if (p_info[cur_part].offset != 0)		    p_info[cur_part].offset = 0;		else		    print_warning(MAX_UNMAXABLE);	    } else		print_warning(MAX_UNMAXABLE);	    break;	case 'N':	case 'n':	    if (p_info[cur_part].id == FREE_SPACE) {		new_part(cur_part);		draw_screen();	    } else if (p_info[cur_part].id == UNUSABLE)		print_warning(ADD_UNUSABLE);	    else		print_warning(ADD_EXISTS);	    break;	case 'P':	case 'p':	    print_tables();	    draw_screen();	    break;	case 'Q':	case 'q':	    done = TRUE;	    break;	case 'T':	case 't':	    if (p_info[cur_part].id > 0) {		change_id(cur_part);		draw_screen();	    } else		print_warning(TYPE_EMPTY);	    break;	case 'U':	case 'u':	    if (display_units == MEGABYTES)		display_units = SECTORS;	    else if (display_units == SECTORS)		display_units = CYLINDERS;	    else if (display_units == CYLINDERS)		display_units = MEGABYTES;	    draw_screen();	    break;	case 'W':	    write_part_table();	    break;	case 'H':	case 'h':	case '?':	    display_help();	    draw_screen();	    break;	case MENU_UP : /* Up arrow */	    if (!draw_cursor(-1))		command = 0;	    else		print_warning(NO_MORE_PARTS);	    break;	case MENU_DOWN : /* Down arrow */	    if (!draw_cursor(1))		command = 0;	    else		pr

⌨️ 快捷键说明

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