/* * misc/bximage.c * $Id: bximage.c,v 1.5 2001-06-01 05:45:55 bdenney Exp $ * * Create empty hard disk or floppy disk images for bochs. * */ #include #include #include #include #include #include "config.h" char *EOF_ERR = "ERROR: End of input"; char *rcsid = "$Id: bximage.c,v 1.5 2001-06-01 05:45:55 bdenney Exp $"; char *divider = "========================================================================"; /* menu data for choosing floppy/hard disk */ char *fdhd_menu = "\nDo you want to create a floppy disk image or a hard disk image?\nPlease type hd or fd. [hd] "; char *fdhd_choices[] = { "fd", "hd" }; int fdhd_n_choices = 2; /* menu data for choosing floppy size */ char *fdsize_menu = "\nChoose the size of floppy disk image to create, in megabytes.\nPlease type 0.72, 1.2, 1.44, or 2.88. [1.44] "; char *fdsize_choices[] = { "0.72","1.2","1.44","2.88" }; int fdsize_n_choices = 4; /* stolen from main.cc */ void bx_center_print (FILE *file, char *line, int maxwidth) { int imax; int i; imax = (maxwidth - strlen(line)) >> 1; for (i=0; imax) { printf ("Your choice (%s) was not an integer between %d and %d.\n\n", clean, min, max); } else { // choice is okay *out = n; return 0; } } } int ask_menu (char *prompt, int n_choices, char *choice[], int the_default, int *out) { char buffer[1024]; char *clean; int i; *out = -1; while (1) { printf ("%s", prompt); if (!fgets (buffer, sizeof(buffer), stdin)) return -1; clean = clean_string (buffer); if (strlen(clean) < 1) { // empty line, use the default *out = the_default; return 0; } for (i=0; i0) printf (", "); printf ("%s", choice[i]); } printf ("\n"); } } int ask_yn (char *prompt, int the_default, int *out) { char buffer[16]; char *clean; *out = -1; while (1) { printf ("%s", prompt); if (!fgets (buffer, sizeof(buffer), stdin)) return -1; clean = clean_string (buffer); if (strlen(clean) < 1) { // empty line, use the default *out = the_default; return 0; } switch (tolower(clean[0])) { case 'y': *out=1; return 0; case 'n': *out=0; return 0; } printf ("Please type either yes or no.\n"); } } int ask_string (char *prompt, char *the_default, char *out) { char buffer[1024]; char *clean; out[0] = 0; printf ("%s", prompt); if (!fgets (buffer, sizeof(buffer), stdin)) return -1; clean = clean_string (buffer); if (strlen(clean) < 1) { // empty line, use the default strcpy (out, the_default); return 0; } strcpy (out, clean); return 0; } /* produce the image file */ int make_image (int sec, char *filename) { FILE *fp; char buffer[1024]; int i; unsigned int n; // check if it exists before trashing someone's disk image fp = fopen (filename, "r"); if (fp) { int confirm; sprintf (buffer, "\nThe disk image '%s' already exists. Are you sure you want to replace it?\nPlease type yes or no. [no] ", filename); if (ask_yn (buffer, 0, &confirm) < 0) fatal (EOF_ERR); if (!confirm) fatal ("ERROR: Aborted"); fclose (fp); } // okay, now open it for writing fp = fopen (filename, "w"); if (fp == NULL) { // attempt to print an error #ifdef HAVE_PERROR sprintf (buffer, "while opening '%s' for writing", filename); perror (buffer); #endif fatal ("ERROR: Could not write disk image"); } // clear the buffer for (i=0; i<512; i++) buffer[i] = 0; // write it however many times printf ("\nWriting: ["); for (i=0; i