Anonymous avatar Anonymous committed 40f1626

Implement displaying disk info and opening disk. Still buggy. (fdisk@sv.gnu.org/fdisk--main--0--patch-7)
fdisk@sv.gnu.org/fdisk--main--0--patch-7
Keywords:

Comments (0)

Files changed (2)

 #include "base.h"
 
 
+#define SMALLBUF 128
+
 static int MEGABYTE_SECTORS (PedDevice* dev)
 {
         return PED_MEGABYTE_SIZE / dev->sector_size;
 {
 	PedPartition*	part;
 	int		num; 
-	char		buf[100];
+	char		buf[SMALLBUF];
 	if (!disk) 
 		return 0;
 	if (!uiquery->getpart) {
-		snprintf(buf,sizeof(buf),_("%s number"),prompt);
+		snprintf(buf,SMALLBUF,_("%s number"),prompt);
 		num = (*value) ? (*value)->num : 0;
 		if (!uiquery->getint (buf, &num)) {
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 {
 	char*		disk_type_name;
 	PedDiskType*	disk_type;
+	int n;
 	if (!uiquery->getdisktype) {
 		if (*value) {
-			disk_type_name = (char *) calloc(sizeof((*value)->name),sizeof(char));
-			strcpy(disk_type_name,(*value)->name);
+			n = strlen((*value)->name);
+			disk_type_name = (char *) calloc(n,sizeof(char));
+			strncpy(disk_type_name,(*value)->name, n);
 		}
 		if (!uiquery->getstring (prompt, &disk_type_name, disk_type_list, NULL, -1))
 			return 0;
 
 	char*			fs_type_name;
 	PedFileSystemType*	fs_type;
+	int			n;
 	if (!uiquery->getfstype) {
 		if(*value) {
-			fs_type_name = (char *) calloc(sizeof((*value)->name),sizeof(char));
-			strcpy(fs_type_name,(*value)->name);
+			n = strlen((*value)->name);
+			fs_type_name = (char *) calloc(n,sizeof(char));
+			strncpy(fs_type_name,(*value)->name,n);
 		}
 		if (!uiquery->getstring(prompt,&fs_type_name,fs_type_list,NULL,-1))
 			return 0;
 {
 	char*	def_str;
 	char*	input;
-	int	valid;
+	int	valid,n;
 	/* FIXME: Check this more thoroughfully */
 
 	if (!custom && *value) {
 
 	def_str = ped_unit_format (dev, *value);
 	if (*value) {
-		input = (char *) calloc(sizeof(def_str),sizeof(char));
-		strcpy(input,def_str);
+		n = strlen(def_str);
+		input = (char *) calloc(n,sizeof(char));
+		strncpy(input,def_str,n);
 	}
 	if (custom || !input) {
 		uiquery->getstring(prompt,&input,NULL,NULL,1);
 do_name (PedDisk* disk, PedPartition* part, char *name, UICalls *uiquery)
 {
 	const char *temp;
+	int n;
         if (!disk)
                 goto error;
 	if (!part) 
                 	goto error;
 	if (!name) {
 		temp = ped_partition_get_name(part);
-		name = (char *) calloc(sizeof(temp),sizeof(char));
-		strcpy(name,temp);
+		n = strlen(temp);
+		name = (char *) calloc(n,sizeof(char));
+		strncpy(name,temp,n);
         	if (!uiquery->getstring(_("Partition name"), &name, NULL, NULL, 0))
 			goto error;
 	}
 error:
         return 0;
 }
+
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
 
+/* TODO: These go out of here */
+#define CFDISK_VERSION "-1"
 #define NCH 1
 
 #include "../config.h"
 
 #define K	1000
 
-#define MAX(X,Y) (X > Y ? X : Y)
-#define MIN(X,Y) (X < Y ? X : Y)
+#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
+#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
 
 /* Keys */
 #define BELL	'\007'	/* ^G */
 #define CTRL_P	'\020'	/* ^P */
 #define CTRL_N	'\016'	/* ^N */
 #define CTRL_R	'\022'	/* ^R */
+#define DEL	'\177'
+#define BKSP 	'\b'
+
 
 #define MENUSIZE 3
+#define INFOSIZE 5
 #define MENUDIV 1
 
 
 #define MENU_ANYKEY 4
 #define MENU_BUTTON 8
 
+#define SMALLBUF 128
 
 
 struct MenuItem {
 /*int curses_ready = 0;*/
 
 
+void
+do_quit(int status, const char *message) {
+	endwin();
+	if (message) {
+		printf("%s\n",message);
+	}
+	exit(status);
+}
 
 void
 clear_status() {
 			key = items[selected].key;
 		}
 		/* If the pressed key is in the list of accepted keys, print a warning */
-		/* FIXME: If the key of the selected item is not in the accepted keys, *
-		 * it is still valid                                                   */
 		if(!(type & MENU_ANYKEY) && !strchr(keys,tolower(key))) {
 			key = 0;
 			print_warning(_("Invalid key"));
 	return key;
 }
 
+static int
+read_char (const char* prompt, char **value) {
+	char buf[SMALLBUF];
+	int y = LINES - 2 - MENUSIZE, i, key = 0, done = 0,x;
+	while (y < LINES) {
+		move(y,0);
+		clrtoeol();
+		y++;
+	}
+	y = LINES - 1 - MENUSIZE;
+	int n = SMALLBUF;
+	if (*value) {
+		i = strlen(*value);
+		i = MIN(i,n);
+		strncpy(buf,*value,i);
+	}
+	else i = 0;
+	buf[i] = '\0';
+	mvaddstr(y,MENUDIV,prompt); addstr(": ");
+	addstr(buf);
+	getyx(stdscr,y,x);
+	n = MIN(n,COLS-x);
+	while(!done) {
+		key = getch();
+		if (key == ESC) {
+			if (getch() == ESC) 
+				return 0;
+			else getch();
+		}
+		else if (key == BKSP || key == DEL) {
+			if (i > 0) {
+				buf[--i] = '\0';
+				mvaddch(y,--x,' ');
+				move(y,x);
+			}
+			else
+				print_warning(_("No more characters to delete"));
+		}
+		else if (key == CR) {
+			done = 1;
+		}
+		else if (isprint(key)) {
+			if (i < n-1) {
+				buf[i++] = key;
+				buf[i] = '\0';
+				mvaddch(y,x++,key);
+			}
+			else
+				print_warning(_("String too long"));
+		}
+		refresh();
+	}
+	if (*value) free (*value);
+	n = strlen(buf);
+	*value = (char *)calloc(n,sizeof(char));
+	strncpy(*value,buf,n);
+	return 1;
+	
+}
 UICalls uiquery;
 
 
-int
+static int
 getbool(const char* prompt, int* value) {
 	int result;
 	struct MenuItem yes_no[] = {
 		{0, NULL, NULL }
 	};
 	menu_title(prompt);
-	result = do_menu(yes_no, 5, MENU_BUTTON | MENU_TITLE, "yn", 0);
+	result = do_menu(yes_no, 5, MENU_BUTTON | MENU_TITLE, "yn", !*value);
 	*value = (result == 'y');
 	return 1;
 }
 
+static int
+getstring (const char* prompt, char **value, const StrList* words, const StrList* locwords, int mword) {
+	/* TODO: Write this. For now ignores valid words. Make a list choice? */
+	return read_char(prompt,value);
+}
+
 static void
 init_calls() {
 	uiquery.getbool = getbool;
+	uiquery.getstring = getstring;
 }
 
+PedDevice *dev;
+PedDisk *disk;
+char *devname;
+char *info_size;
+char *info_sectorsize;
+char *info_model;
+char *info_heads;
+char *info_cylinders;
+char *info_sectors;
+char *info_cylsize;
+static void
+show_info() {
+	int x,y,n;
+	char buf[SMALLBUF];
+	for (y = 0; y < INFOSIZE+1; y++) {
+		move(y,0);
+		clrtoeol();
+	}
+	y = 0;
+	snprintf(buf,SMALLBUF,"cfdisk %s",CFDISK_VERSION);
+	n = strlen(buf);
+	n = MIN(n,COLS);
+	x = (COLS-n)/2;
+	mvaddstr(y++,x,buf);
+	
+	if (info_model) {
+		n = strlen(info_model);
+		n = MIN(n,COLS);
+		x = (COLS-n)/2;
+		mvaddstr(y++,x,info_model);
+	}
+	
+	if (info_size) {
+		snprintf(buf,SMALLBUF,"Disk: %s   Size: %s", devname, info_size);
+	}
+	else {
+		snprintf(buf,SMALLBUF,"Disk: %s", devname);
+	}
+	n = strlen(buf);
+	n = MIN(n,COLS);
+	x = (COLS-n)/2;
+	mvaddstr(y++,x,buf);
+	
+	if (info_heads && info_sectors && info_cylinders) {
+		snprintf(buf,SMALLBUF, "Heads: %s   Sectors per track: %s   Cylinders: %s",
+				info_heads, info_sectors, info_cylinders);
+		n = strlen(buf);
+		n = MIN(n,COLS);
+		x = (COLS - n) /2;
+		mvaddstr(y++,x,buf);
+	}
+	
+	
+}
 
+static void
+init_disk() {
+	char buf[SMALLBUF];
+	int n;
+	static struct MenuItem custom_part[] = {
+		{ 'c', N_("Choose"), N_("Prompt for device address") },
+		{ 'q', N_("Quit"), N_("Quit program") },
+		{ 0, NULL, NULL }
+	};
+	static struct MenuItem custom_label[] = {
+		{ 'c', N_("Create"), N_("Create new partition table") },
+		{ 'q', N_("Quit"), N_("Quit program") },
+		{ 0, NULL, NULL }
+	};
+	if (!dev) {
+		menu_title(_("Cannot find a device automatically"));
+		if ('c' == do_menu(custom_part, 8, MENU_BUTTON | MENU_TITLE, "cq", 0)) {
+			read_char(_("Enter path to the device"),&devname);
+			if (!devname) {
+				do_quit(0,NULL);
+			}
+			dev = ped_device_get(devname);
+			if (!dev || !ped_device_open(dev)) {
+				do_quit(1,_("Invalid device"));
+			}
+		}
+		else  do_quit(0,NULL);
+	}
+	disk = ped_disk_new(dev);
+	if (!disk) {
+		menu_title(_("Cannot find a partition table on the disk"));
+		if ('c' == do_menu(custom_label, 8, MENU_BUTTON | MENU_TITLE, "cq", 0)) {
+			if (!do_mklabel (dev, &disk, NULL, &uiquery))
+				do_quit(1,_("Creation of partition table failed"));
+		}
+	}
+	ped_unit_set_default(PED_UNIT_BYTE);
+
+	snprintf(buf,SMALLBUF,_("%s bytes, %s MB"),
+		ped_unit_format_custom_byte (dev, dev->length * dev->sector_size-1, PED_UNIT_BYTE),
+		ped_unit_format_custom_byte (dev, dev->length * dev->sector_size-1, PED_UNIT_MEGABYTE));
+	n = strlen(buf);
+	info_size = calloc(n,sizeof(char));
+	strncpy(info_size,buf,n);
+
+	n = strlen(dev->model);
+	info_model = calloc(n,sizeof(char));
+	strncpy(info_model,dev->model,n);
+	
+	snprintf(buf,SMALLBUF,"%lldB/%lldB\n",dev->sector_size, dev->phys_sector_size);
+	n = strnlen(buf);
+	info_sectorsize = calloc(n,sizeof(char));
+	strncpy(info_sectorsize,buf,n);
+
+	PedCHSGeometry* chs = &(dev->bios_geom);
+        info_cylsize = ped_unit_format_custom (dev, chs->heads * chs->sectors, PED_UNIT_KILOBYTE);
+
+	snprintf(buf, SMALLBUF, "%d", chs->cylinders);
+	n = strlen(buf);
+	info_cylinders = calloc(n,sizeof(char));
+	strncpy(info_cylinders,buf,n);
+
+	snprintf(buf, SMALLBUF, "%d", chs->heads);
+	n = strlen(buf);
+	info_heads = calloc(n,sizeof(char));
+	strncpy(info_heads,buf,n);
+	
+	snprintf(buf, SMALLBUF, "%d",  chs->sectors);
+	n = strlen(buf);
+	info_sectors = calloc(n,sizeof(char));
+	strncpy(info_sectors,buf,n);
+	/*
+                printf (_("BIOS cylinder,head,sector geometry: %d,%d,%d.  "
+                          "Each cylinder is %s.\n"),
+                        chs->cylinders, chs->heads, chs->sectors, cyl_size);
+                ped_free (cyl_size); */
+}
+
+static void
+print_info(void) {
+	
+}
 static struct MenuItem part_menu[] = {
 	{ 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
 	{ 'n', N_("New"), N_("Create new partition from free space") },
         { 0, NULL, NULL }
 };
 
+
+
 static void
 do_ui (void) {
 	/*curses_ready = 1;*/
 	noecho();
 	nonl();
 	init_calls();
-	int test;
-	uiquery.getbool(N_("Start interface?"),&test);
-	if (test)
-		do_menu(part_menu, 8, MENU_BUTTON, "bfcayrodtuwqph", 0);
+	init_disk();
+	int test = 0;
+	/*char *value;
+	read_char(_("What is your age"),&value);
+	uiquery.getbool(value,&test);
+	free(value);*/
+	show_info();
+	do_menu(part_menu, 8, MENU_BUTTON, "bfcayrodtuwqph", 0);
 	endwin();
 }
 int
-main (void) {
+main (int argc, char **argv) {
+	int opt;
+	while ((opt = getopt(argc,argv, "")) != -1);
+	if (argc-optind == 1) {
+		devname = argv[optind];
+		dev = ped_device_get(devname);
+		if (!dev) {
+			printf(_("Invalid device\n"));
+			exit(1);
+			
+		}
+	}
+	else {
+		ped_device_probe_all();
+		dev = ped_device_get_next(NULL);
+		if (dev) devname= dev->path;	
+	}
+	if (dev) if (!ped_device_open(dev)) {
+		printf(_("Invalid device\n"));
+		exit(1);
+	}
 	do_ui();
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.