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

📄 cfdisk.c

📁 一個相當mini的磁碟分割軟體,適合放在嵌入式系統
💻 C
📖 第 1 页 / 共 5 页
字号:
void get_dos_label(int i){    char sector[128];#define DOS_OSTYPE_OFFSET 3#define DOS_LABEL_OFFSET 43#define DOS_FSTYPE_OFFSET 54#define DOS_OSTYPE_SZ 8#define DOS_LABEL_SZ 11#define DOS_FSTYPE_SZ 8    ext2_loff_t offset;    int j;    offset = ((ext2_loff_t) p_info[i].first_sector + p_info[i].offset)	 * SECTOR_SIZE;    if (ext2_llseek(fd, offset, SEEK_SET) == offset	&& read(fd, &sector, sizeof(sector)) == sizeof(sector)) {	 dos_copy_to_info(p_info[i].ostype, OSTYPESZ,			  sector+DOS_OSTYPE_OFFSET, DOS_OSTYPE_SZ);	 dos_copy_to_info(p_info[i].volume_label, LABELSZ,			  sector+DOS_LABEL_OFFSET, DOS_LABEL_SZ);	 dos_copy_to_info(p_info[i].fstype, FSTYPESZ,			  sector+DOS_FSTYPE_OFFSET, DOS_FSTYPE_SZ);    }}void get_ext2_label(int i){#define EXT2_SUPER_MAGIC 0xEF53#define EXT2LABELSZ 16    struct ext2_super_block {	char  s_dummy0[56];	unsigned char  s_magic[2];	char  s_dummy1[62];	char  s_volume_name[EXT2LABELSZ];	char  s_last_mounted[64];	char  s_dummy2[824];    } sb;    char *label = sb.s_volume_name;    ext2_loff_t offset;    int j;    offset = ((ext2_loff_t) p_info[i].first_sector + p_info[i].offset)	 * SECTOR_SIZE + 1024;    if (ext2_llseek(fd, offset, SEEK_SET) == offset	&& read(fd, &sb, sizeof(sb)) == sizeof(sb)	&& sb.s_magic[0] + 256*sb.s_magic[1] == EXT2_SUPER_MAGIC) {	 for(j=0; j<EXT2LABELSZ; j++)	      if(!isprint(label[j]))		   label[j] = 0;	 label[EXT2LABELSZ] = 0;	 strncpy(p_info[i].volume_label, label, LABELSZ);	 strncpy(p_info[i].fstype, "ext2", FSTYPESZ);    }}void check_part_info(void){    int i, pri = 0, log = 0;    for (i = 0; i < num_parts; i++)	if (p_info[i].id > 0 && IS_PRIMARY(p_info[i].num))	    pri++;	else if (p_info[i].id > 0 && IS_LOGICAL(p_info[i].num))	    log++;    if (is_extended(ext_info.id))	if (log > 0)	    pri++;	else {	    ext_info.first_sector = 0;	    ext_info.last_sector = 0;	    ext_info.offset = 0;	    ext_info.flags = 0;	    ext_info.id = FREE_SPACE;	    ext_info.num = PRIMARY;	}    if (pri >= 4) {	for (i = 0; i < num_parts; i++)	    if (p_info[i].id == FREE_SPACE || p_info[i].id == UNUSABLE)		if (is_extended(ext_info.id))		    if (p_info[i].first_sector >= ext_info.first_sector &&			p_info[i].last_sector <= ext_info.last_sector) {			p_info[i].id = FREE_SPACE;			p_info[i].num = LOGICAL;		    } else if (i > 0 &&			       p_info[i-1].first_sector >=			       ext_info.first_sector &&			       p_info[i-1].last_sector <=			       ext_info.last_sector) {			p_info[i].id = FREE_SPACE;			p_info[i].num = LOGICAL;		    } else if (i < num_parts-1 &&			       p_info[i+1].first_sector >=			       ext_info.first_sector &&			       p_info[i+1].last_sector <=			       ext_info.last_sector) {			p_info[i].id = FREE_SPACE;			p_info[i].num = LOGICAL;		    } else			p_info[i].id = UNUSABLE;		else /* if (!is_extended(ext_info.id)) */		    p_info[i].id = UNUSABLE;	    else /* if (p_info[i].id > 0) */		while (0); /* Leave these alone */    } else { /* if (pri < 4) */	for (i = 0; i < num_parts; i++) {	    if (p_info[i].id == UNUSABLE)		p_info[i].id = FREE_SPACE;	    if (p_info[i].id == FREE_SPACE)		if (is_extended(ext_info.id))		    if (p_info[i].first_sector >= ext_info.first_sector &&			p_info[i].last_sector <= ext_info.last_sector)			p_info[i].num = LOGICAL;		    else if (i > 0 &&			     p_info[i-1].first_sector >=			     ext_info.first_sector &&			     p_info[i-1].last_sector <=			     ext_info.last_sector)			p_info[i].num = PRI_OR_LOG;		    else if (i < num_parts-1 &&			     p_info[i+1].first_sector >=			     ext_info.first_sector &&			     p_info[i+1].last_sector <=			     ext_info.last_sector)			p_info[i].num = PRI_OR_LOG;		    else			p_info[i].num = PRIMARY;		else /* if (!is_extended(ext_info.id)) */		    p_info[i].num = PRI_OR_LOG;	    else /* if (p_info[i].id > 0) */		while (0); /* Leave these alone */	}    }}void remove_part(int i){    int p;    for (p = i; p < num_parts; p++)	p_info[p] = p_info[p+1];    num_parts--;}void insert_empty_part(int i, int first, int last){    int p;    for (p = num_parts; p > i; p--)	 p_info[p] = p_info[p-1];    p_info[i].first_sector = first;    p_info[i].last_sector = last;    p_info[i].offset = 0;    p_info[i].flags = 0;    p_info[i].id = FREE_SPACE;    p_info[i].num = PRI_OR_LOG;    p_info[i].volume_label[0] = 0;    p_info[i].fstype[0] = 0;    p_info[i].ostype[0] = 0;    num_parts++;}void del_part(int i){    int num = p_info[i].num;    if (i > 0 && (p_info[i-1].id == FREE_SPACE ||		  p_info[i-1].id == UNUSABLE)) {	/* Merge with previous partition */	p_info[i-1].last_sector = p_info[i].last_sector;	remove_part(i--);    }    if (i < num_parts - 1 && (p_info[i+1].id == FREE_SPACE ||			      p_info[i+1].id == UNUSABLE)) {	/* Merge with next partition */	p_info[i+1].first_sector = p_info[i].first_sector;	remove_part(i);    }    if (i > 0)	p_info[i].first_sector = p_info[i-1].last_sector + 1;    else	p_info[i].first_sector = 0;    if (i < num_parts - 1)	p_info[i].last_sector = p_info[i+1].first_sector - 1;    else	p_info[i].last_sector = sectors*heads*cylinders - 1;    p_info[i].offset = 0;    p_info[i].flags = 0;    p_info[i].id = FREE_SPACE;    p_info[i].num = PRI_OR_LOG;    if (IS_LOGICAL(num)) {	/* We have a logical partition --> shrink the extended partition	 * if (1) this is the first logical drive, or (2) this is the	 * last logical drive; and if there are any other logical drives	 * then renumber the ones after "num".	 */	if (i == 0 || (i > 0 && IS_PRIMARY(p_info[i-1].num))) {	    ext_info.first_sector = p_info[i].last_sector + 1;	    ext_info.offset = 0;	}	if (i == num_parts-1 ||	    (i < num_parts-1 && IS_PRIMARY(p_info[i+1].num)))	    ext_info.last_sector = p_info[i].first_sector - 1;	for (i = 0; i < num_parts; i++)	    if (p_info[i].num > num)		p_info[i].num--;    }    /* Clean up the rest of the partitions */    check_part_info();}int add_part(int num, int id, int flags, int first, int last, int offset,	     int want_label){    int i, pri = 0, log = 0;    if (num_parts == MAXIMUM_PARTS ||	first < 0 ||	first >= cylinders*heads*sectors ||	last < 0 ||	last >= cylinders*heads*sectors) {	return -1;		/* bad start or end */    }    for (i = 0; i < num_parts; i++)	if (p_info[i].id > 0 && IS_PRIMARY(p_info[i].num))	    pri++;	else if (p_info[i].id > 0 && IS_LOGICAL(p_info[i].num))	    log++;    if (is_extended(ext_info.id) && log > 0)	pri++;    if (IS_PRIMARY(num))	if (pri >= 4) {	    return -1;		/* no room for more */	} else	    pri++;    for (i = 0; i < num_parts && p_info[i].last_sector < first; i++);    if (i == num_parts || p_info[i].id != FREE_SPACE	|| last > p_info[i].last_sector) {	return -1;    }    if (is_extended(id)) {	if (ext_info.id != FREE_SPACE) {	    return -1;		/* second extended */	}	else if (IS_PRIMARY(num)) {	    ext_info.first_sector = first;	    ext_info.last_sector = last;	    ext_info.offset = offset;	    ext_info.flags = flags;	    ext_info.id = id;	    ext_info.num = num;	    ext_info.volume_label[0] = 0;	    ext_info.fstype[0] = 0;	    ext_info.ostype[0] = 0;	    return 0;	} else {	    return -1;		/* explicit extended logical */	}    }    if (IS_LOGICAL(num)) {	if (!is_extended(ext_info.id)) {	    print_warning("!!!! Internal error creating logical "			  "drive with no extended partition !!!!");	} else {	    /* We might have a logical partition outside of the extended	     * partition's range --> we have to extend the extended	     * partition's range to encompass this new partition, but we	     * must make sure that there are no primary partitions between	     * it and the closest logical drive in extended partition.	     */	    if (first < ext_info.first_sector) {		if (i < num_parts-1 && IS_PRIMARY(p_info[i+1].num)) {		    print_warning(TWO_EXTENDEDS);		    return -1;		} else {		    if (first == 0) {			ext_info.first_sector = 0;			ext_info.offset = first = offset;		    } else {			ext_info.first_sector = first;		    }		}	    } else if (last > ext_info.last_sector) {		if (i > 0 && IS_PRIMARY(p_info[i-1].num)) {		    print_warning(TWO_EXTENDEDS);		    return -1;		} else {		    ext_info.last_sector = last;		}	    }	}    }    if (first != p_info[i].first_sector &&	!(IS_LOGICAL(num) && first == offset)) {	insert_empty_part(i, p_info[i].first_sector, first-1);	i++;    }    if (last != p_info[i].last_sector)	insert_empty_part(i+1, last+1, p_info[i].last_sector);    p_info[i].first_sector = first;    p_info[i].last_sector = last;    p_info[i].offset = offset;    p_info[i].flags = flags;    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_ext2_label(i);    }    check_part_info();    return 0;}int find_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;}int find_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;}void inc_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{    char 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.             */int menuUpdate( int y, int x, struct MenuItem *menuItems, int itemLength, char *available, int menuType, int current ){    int i, lmargin = x, ymargin = y;    /* Print available buttons */    move( y, x ); clrtoeol();    for( i = 0; menuItems[i].key; i++ )    {        char buff[20];        int lenName;        /* 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 */        lenName = strlen( menuItems[i].name );        if(lenName > itemLength)            print_warning("Menu item too long. Menu may look odd.");        if( menuType & MENU_BUTTON )            sprintf( buff, "[%*s%-*s]", (itemLength - lenName) / 2, "",                (itemLength - lenName + 1) / 2 + lenName, menuItems[i].name );        else            sprintf( buff, "%*s%-*s", (itemLength - lenName) / 2, "",                (itemLength - lenName + 1) / 2 + lenName, menuItems[i].name );        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 */    mvaddstr( WARNING_START + 1, (COLUMNS - strlen( menuItems[current].desc )) / 2, menuItems[current].desc );    return y;}/* This function takes a list of menu items, lets the user choose one of them * * and returns the value keyboard shortcut of the selected menu item          */int menuSelect( 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) )    {

⌨️ 快捷键说明

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