📄 cfdisk.c
字号:
p_info[i].id = id; p_info[i].num = num; p_info[i].volume_label[0] = 0; p_info[i].fstype[0] = 0; p_info[i].ostype[0] = 0; if (want_label) { if (may_have_dos_label(id)) get_dos_label(i); else if (id == LINUX) get_linux_label(i); } check_part_info(); return 0;}static intfind_primary(void) { int num = 0, cur = 0; while (cur < num_parts && IS_PRIMARY(num)) if ((p_info[cur].id > 0 && p_info[cur].num == num) || (is_extended(ext_info.id) && ext_info.num == num)) { num++; cur = 0; } else cur++; if (!IS_PRIMARY(num)) return -1; else return num;}static intfind_logical(int i) { int num = -1; int j; for (j = i; j < num_parts && num == -1; j++) if (p_info[j].id > 0 && IS_LOGICAL(p_info[j].num)) num = p_info[j].num; if (num == -1) { num = 4; for (j = 0; j < num_parts; j++) if (p_info[j].id > 0 && p_info[j].num == num) num++; } return num;}static voidinc_logical(int i) { int j; for (j = i; j < num_parts; j++) if (p_info[j].id > 0 && IS_LOGICAL(p_info[j].num)) p_info[j].num++;}/* Command menu support by Janne Kukonlehto <jtklehto@phoenix.oulu.fi> September 1994 *//* Constants for menuType parameter of menuSelect function */#define MENU_HORIZ 1#define MENU_VERT 2#define MENU_ACCEPT_OTHERS 4#define MENU_BUTTON 8/* Miscellenous constants */#define MENU_SPACING 2#define MENU_MAX_ITEMS 256 /* for simpleMenu function */#define MENU_UP 1#define MENU_DOWN 2#define MENU_RIGHT 3#define MENU_LEFT 4struct MenuItem{ int key; /* Keyboard shortcut; if zero, then there is no more items in the menu item table */ char *name; /* Item name, should be eight characters with current implementation */ char *desc; /* Item description to be printed when item is selected */};/* * Actual function which prints the button bar and highlights the active button * Should not be called directly. Call function menuSelect instead. */static intmenuUpdate( int y, int x, struct MenuItem *menuItems, int itemLength, char *available, int menuType, int current ) { int i, lmargin = x, ymargin = y; char *mcd; /* Print available buttons */ move( y, x ); clrtoeol(); for( i = 0; menuItems[i].key; i++ ) { char buff[20]; int lenName; const char *mi; /* Search next available button */ while( menuItems[i].key && !strchr(available, menuItems[i].key) ) i++; if( !menuItems[i].key ) break; /* No more menu items */ /* If selected item is not available and we have bypassed it, make current item selected */ if( current < i && menuItems[current].key < 0 ) current = i; /* If current item is selected, highlight it */ if( current == i ) /*attron( A_REVERSE )*/ standout (); /* Print item */ /* Because of a bug in gettext() we must not translate empty strings */ if (menuItems[i].name[0]) mi = _(menuItems[i].name); else mi = ""; lenName = strlen( mi );#if 0 if(lenName > itemLength || lenName >= sizeof(buff)) print_warning(_("Menu item too long. Menu may look odd."));#endif if (lenName >= sizeof(buff)) { /* truncate ridiculously long string */ xstrncpy(buff, mi, sizeof(buff)); } else if (lenName >= itemLength) { snprintf(buff, sizeof(buff), (menuType & MENU_BUTTON) ? "[%s]" : "%s", mi); } else { snprintf(buff, sizeof(buff), (menuType & MENU_BUTTON) ? "[%*s%-*s]" : "%*s%-*s", (itemLength - lenName) / 2, "", (itemLength - lenName + 1) / 2 + lenName, mi); } mvaddstr( y, x, buff ); /* Lowlight after selected item */ if( current == i ) /*attroff( A_REVERSE )*/ standend (); /* Calculate position for the next item */ if( menuType & MENU_VERT ) { y += 1; if( y >= WARNING_START ) { y = ymargin; x += itemLength + MENU_SPACING; if( menuType & MENU_BUTTON ) x += 2; } } else { x += itemLength + MENU_SPACING; if( menuType & MENU_BUTTON ) x += 2; if( x > COLUMNS - lmargin - 12 ) { x = lmargin; y ++ ; } } } /* Print the description of selected item */ mcd = _(menuItems[current].desc); mvaddstr( WARNING_START + 1, (COLUMNS - strlen( mcd )) / 2, mcd ); return y;}/* This function takes a list of menu items, lets the user choose one * * and returns the keyboard shortcut value of the selected menu item */static intmenuSelect( int y, int x, struct MenuItem *menuItems, int itemLength, char *available, int menuType, int menuDefault ) { int i, ylast = y, key = 0, current = menuDefault; if( !( menuType & ( MENU_HORIZ | MENU_VERT ) ) ) { print_warning(_("Menu without direction. Defaulting horizontal.")); menuType |= MENU_HORIZ; } /* Make sure that the current is one of the available items */ while( !strchr(available, menuItems[current].key) ) { current ++ ; if( !menuItems[current].key ) current = 0; } /* Repeat until allowable choice has been made */ while( !key ) { /* Display the menu and read a command */ ylast = menuUpdate( y, x, menuItems, itemLength, available, menuType, current ); refresh(); key = getch(); /* Clear out all prompts and such */ clear_warning(); for (i = y; i < ylast; i++) { move(i, x); clrtoeol(); } move( WARNING_START + 1, 0 ); clrtoeol(); /* Cursor keys - possibly split by slow connection */ if( key == ESC ) { /* Check whether this is a real ESC or one of extended keys */ /*nodelay(stdscr, TRUE);*/ key = getch(); /*nodelay(stdscr, FALSE);*/ if( key == /*ERR*/ ESC ) { /* This is a real ESC */ key = ESC; } if(key == '[' || key == 'O') { /* This is one extended keys */ key = getch(); switch(key) { case 'A': /* Up arrow */ key = MENU_UP; break; case 'B': /* Down arrow */ key = MENU_DOWN; break; case 'C': /* Right arrow */ key = MENU_RIGHT; break; case 'D': /* Left arrow */ key = MENU_LEFT; break; default: key = 0; } } } /* Enter equals the keyboard shortcut of current menu item */ if (key == CR) key = menuItems[current].key; /* Give alternatives for arrow keys in case the window manager swallows these */ if (key == TAB) key = MENU_RIGHT; if (key == UPKEY) /* ^P */ key = MENU_UP; if (key == DOWNKEY) /* ^N */ key = MENU_DOWN; if (key == MENU_UP) { if( menuType & MENU_VERT ) { do { current -- ; if( current < 0 ) while( menuItems[current+1].key ) current ++ ; } while( !strchr( available, menuItems[current].key )); key = 0; } } if (key == MENU_DOWN) { if( menuType & MENU_VERT ) { do { current ++ ; if( !menuItems[current].key ) current = 0 ; } while( !strchr( available, menuItems[current].key )); key = 0; } } if (key == MENU_RIGHT) { if( menuType & MENU_HORIZ ) { do { current ++ ; if( !menuItems[current].key ) current = 0 ; } while( !strchr( available, menuItems[current].key )); key = 0; } } if (key == MENU_LEFT) { if( menuType & MENU_HORIZ ) { do { current -- ; if( current < 0 ) { while( menuItems[current + 1].key ) current ++ ; } } while( !strchr( available, menuItems[current].key )); key = 0; } } /* Should all keys to be accepted? */ if( key && (menuType & MENU_ACCEPT_OTHERS) ) break; /* Is pressed key among acceptable ones? */ if( key && (strchr(available, tolower(key)) || strchr(available, key))) break; /* The key has not been accepted so far -> let's reject it */ if (key) { key = 0; putchar( BELL ); print_warning(_("Illegal key")); } } /* Clear out prompts and such */ clear_warning(); for( i = y; i <= ylast; i ++ ) { move( i, x ); clrtoeol(); } move( WARNING_START + 1, 0 ); clrtoeol(); return key;}/* A function which displays "Press a key to continue" * * and waits for a keypress. * * Perhaps calling function menuSelect is a bit overkill but who cares? */static voidmenuContinue(void) { static struct MenuItem menuContinueBtn[]= { { 'c', "", N_("Press a key to continue") }, { 0, NULL, NULL } }; menuSelect(COMMAND_LINE_Y, COMMAND_LINE_X, menuContinueBtn, 0, "c", MENU_HORIZ | MENU_ACCEPT_OTHERS, 0 );}/* Function menuSelect takes way too many parameters * * Luckily, most of time we can do with this function */static intmenuSimple(struct MenuItem *menuItems, int menuDefault) { int i, j, itemLength = 0; char available[MENU_MAX_ITEMS]; for(i = 0; menuItems[i].key; i++) { j = strlen( _(menuItems[i].name) ); if( j > itemLength ) itemLength = j; available[i] = menuItems[i].key; } available[i] = 0; return menuSelect(COMMAND_LINE_Y, COMMAND_LINE_X, menuItems, itemLength, available, MENU_HORIZ | MENU_BUTTON, menuDefault);}/* End of command menu support code */static voidnew_part(int i) { char response[LINE_LENGTH], def[LINE_LENGTH]; char c; long long first = p_info[i].first_sector; long long last = p_info[i].last_sector; long long offset = 0; int flags = 0; int id = LINUX; int num = -1; long long num_sects = last - first + 1; int len, ext, j; char *errmsg; double sectors_per_MB = K*K / 512.0; if (p_info[i].num == PRI_OR_LOG) { static struct MenuItem menuPartType[]= { { 'p', N_("Primary"), N_("Create a new primary partition") }, { 'l', N_("Logical"), N_("Create a new logical partition") }, { ESC, N_("Cancel"), N_("Don't create a partition") }, { 0, NULL, NULL } }; c = menuSimple( menuPartType, 0 ); if (toupper(c) == 'P') num = find_primary(); else if (toupper(c) == 'L') num = find_logical(i); else return; } else if (p_info[i].num == PRIMARY) num = find_primary(); else if (p_info[i].num == LOGICAL) num = find_logical(i); else print_warning(_("!!! Internal error !!!")); snprintf(def, sizeof(def), "%.2f", num_sects/sectors_per_MB); mvaddstr(COMMAND_LINE_Y, COMMAND_LINE_X, _("Size (in MB): ")); if ((len = get_string(response, LINE_LENGTH, def)) <= 0 && len != GS_DEFAULT) return; else if (len > 0) {#define num_cyls(bytes) (round_int(bytes/SECTOR_SIZE/cylinder_size)) for (j = 0; j < len-1 && (isdigit(response[j]) || response[j] == '.'); j++); if (toupper(response[j]) == 'K') { num_sects = num_cyls(atof(response)*K)*cylinder_size; } else if (toupper(response[j]) == 'M') { num_sects = num_cyls(atof(response)*K*K)*cylinder_size; } else if (toupper(response[j]) == 'G') { num_sects = num_cyls(atof(response)*K*K*K)*cylinder_size; } else if (toupper(response[j]) == 'C') { num_sects = round_int(atof(response))*cylinder_size; } else if (toupper(response[j]) == 'S') { num_sects = round_int(atof(response)); } else { num_sects = num_cyls(atof(response)*K*K)*cylinder_size; } } if (num_sects <= 0 || num_sects > p_info[i].last_sector - p_info[i].first_sector + 1) return; move( COMMAND_LINE_Y, COMMAND_LINE_X ); clrtoeol(); if (num_sects < p_info[i].last_sector - p_info[i].first_sector + 1) { /* Determine where inside free space to put partition. */ static struct MenuItem menuPlace[]= { { 'b', N_("Beginning"), N_("Add partition at beginning of free space") }, { 'e', N_("End"), N_("Add partition at end of free space") }, { ESC, N_("Cancel"), N_("Don't create a partition") }, { 0, NULL, NULL } }; c = menuSimple( menuPlace, 0 ); if (toupper(c) == 'B') last = first + num_sects - 1; else if (toupper(c) == 'E') first = last - num_sects + 1; else return; } if (IS_LOGICAL(num) && !is_extended(ext_info.id)) { /* We want to add a logical partition, but need to create an * extended partition first. */ if ((ext = find_primary()) < 0) { print_warning(_("No room to create the extended partition")); return; } errmsg = 0; if (add_part(ext, DOS_EXTENDED, 0, first, last, (first == 0 ? sectors : 0), 0, &errmsg) && errmsg) print_warning(errmsg); first = ext_info.first_sector + ext_info.offset; } if (IS_LOGICAL(num)) inc_logical(i); /* Now we have a complete partition to ourselves */ if (first == 0 || IS_LOGICAL(num)) offset = sectors; errmsg = 0; if (add_part(num, id, flags, first, last, offset, 0, &errmsg) && errmsg) print_warning(errmsg);}static voidget_kernel_geometry(void) {#ifdef HDIO_GETGEO struct hd_geometry geometry; if (!ioctl(fd, HDIO_GETGEO, &geometry)) { kern_heads = geometry.heads; kern_sectors = geometry.sectors; }#endif}static intsaid_yes(char answer) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -