fdisk: remove old code to ask user
Old code has been broken by design: * use global variables * fixed buffer size * useless within libfdisk Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
950edd1a71
commit
afb7c83520
218
fdisks/fdisk.c
218
fdisks/fdisk.c
|
@ -51,9 +51,6 @@ sector_t get_nr_sects(struct partition *p) {
|
|||
return read4_little_endian(p->size4);
|
||||
}
|
||||
|
||||
char *line_ptr, /* interactive input */
|
||||
line_buffer[LINE_LENGTH];
|
||||
|
||||
int nowarn = 0; /* no warnings for fdisk -l/-s */
|
||||
|
||||
void toggle_units(struct fdisk_context *cxt)
|
||||
|
@ -212,221 +209,6 @@ void warn_limits(struct fdisk_context *cxt)
|
|||
}
|
||||
}
|
||||
|
||||
static void maybe_exit(struct fdisk_context *cxt, int rc, int *asked)
|
||||
{
|
||||
char line[LINE_LENGTH];
|
||||
|
||||
assert(cxt);
|
||||
assert(cxt->label);
|
||||
|
||||
putchar('\n');
|
||||
if (asked)
|
||||
*asked = 0;
|
||||
|
||||
if (fdisk_label_is_changed(cxt->label)) {
|
||||
fprintf(stderr, _("Do you really want to quit? "));
|
||||
|
||||
if (!fgets(line, LINE_LENGTH, stdin) || rpmatch(line) == 1)
|
||||
goto leave;
|
||||
if (asked)
|
||||
*asked = 1;
|
||||
return;
|
||||
}
|
||||
leave:
|
||||
fdisk_free_context(cxt);
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
/* read line; return 0 or first char */
|
||||
int read_line(struct fdisk_context *cxt, int *asked)
|
||||
{
|
||||
line_ptr = line_buffer;
|
||||
if (!fgets(line_buffer, LINE_LENGTH, stdin)) {
|
||||
maybe_exit(cxt, 1, asked);
|
||||
return 0;
|
||||
}
|
||||
if (asked)
|
||||
*asked = 0;
|
||||
while (*line_ptr && !isgraph(*line_ptr))
|
||||
line_ptr++;
|
||||
return *line_ptr;
|
||||
}
|
||||
|
||||
char read_char(struct fdisk_context *cxt, char *mesg)
|
||||
{
|
||||
do {
|
||||
fputs(mesg, stdout);
|
||||
fflush (stdout); /* requested by niles@scyld.com */
|
||||
|
||||
} while (!read_line(cxt, NULL));
|
||||
|
||||
return *line_ptr;
|
||||
}
|
||||
|
||||
char read_chars(struct fdisk_context *cxt, char *mesg)
|
||||
{
|
||||
int rc, asked = 0;
|
||||
|
||||
do {
|
||||
fputs(mesg, stdout);
|
||||
fflush (stdout); /* niles@scyld.com */
|
||||
rc = read_line(cxt, &asked);
|
||||
} while (asked);
|
||||
|
||||
if (!rc) {
|
||||
*line_ptr = '\n';
|
||||
line_ptr[1] = 0;
|
||||
}
|
||||
return *line_ptr;
|
||||
}
|
||||
|
||||
/* deprecated in favour of fdisk_ask_number() */
|
||||
unsigned int
|
||||
read_int_with_suffix(struct fdisk_context *cxt,
|
||||
unsigned int low, unsigned int dflt, unsigned int high,
|
||||
unsigned int base, char *mesg, int *is_suffix_used)
|
||||
{
|
||||
unsigned int res;
|
||||
int default_ok = 1;
|
||||
int absolute = 0;
|
||||
static char *ms = NULL;
|
||||
static size_t mslen = 0;
|
||||
|
||||
if (!ms || strlen(mesg)+100 > mslen) {
|
||||
mslen = strlen(mesg)+200;
|
||||
ms = xrealloc(ms,mslen);
|
||||
}
|
||||
|
||||
if (dflt < low || dflt > high)
|
||||
default_ok = 0;
|
||||
|
||||
if (default_ok)
|
||||
snprintf(ms, mslen, _("%s (%u-%u, default %u): "),
|
||||
mesg, low, high, dflt);
|
||||
else
|
||||
snprintf(ms, mslen, "%s (%u-%u): ",
|
||||
mesg, low, high);
|
||||
|
||||
while (1) {
|
||||
int use_default = default_ok;
|
||||
|
||||
/* ask question and read answer */
|
||||
while (read_chars(cxt, ms) != '\n' && !isdigit(*line_ptr)
|
||||
&& *line_ptr != '-' && *line_ptr != '+')
|
||||
continue;
|
||||
|
||||
if (*line_ptr == '+' || *line_ptr == '-') {
|
||||
int minus = (*line_ptr == '-');
|
||||
int suflen;
|
||||
|
||||
absolute = 0;
|
||||
res = atoi(line_ptr + 1);
|
||||
|
||||
while (isdigit(*++line_ptr))
|
||||
use_default = 0;
|
||||
|
||||
while (isspace(*line_ptr))
|
||||
line_ptr++;
|
||||
|
||||
suflen = strlen(line_ptr) - 1;
|
||||
|
||||
while(isspace(*(line_ptr + suflen)))
|
||||
*(line_ptr + suflen--) = '\0';
|
||||
|
||||
if ((*line_ptr == 'C' || *line_ptr == 'c') &&
|
||||
*(line_ptr + 1) == '\0') {
|
||||
/*
|
||||
* Cylinders
|
||||
*/
|
||||
if (fdisk_context_use_cylinders(cxt))
|
||||
res *= cxt->geom.heads * cxt->geom.sectors;
|
||||
} else if (*line_ptr &&
|
||||
*(line_ptr + 1) == 'B' &&
|
||||
*(line_ptr + 2) == '\0') {
|
||||
/*
|
||||
* 10^N
|
||||
*/
|
||||
if (*line_ptr == 'K')
|
||||
absolute = 1000;
|
||||
else if (*line_ptr == 'M')
|
||||
absolute = 1000000;
|
||||
else if (*line_ptr == 'G')
|
||||
absolute = 1000000000;
|
||||
else
|
||||
absolute = -1;
|
||||
} else if (*line_ptr &&
|
||||
*(line_ptr + 1) == '\0') {
|
||||
/*
|
||||
* 2^N
|
||||
*/
|
||||
if (*line_ptr == 'K')
|
||||
absolute = 1 << 10;
|
||||
else if (*line_ptr == 'M')
|
||||
absolute = 1 << 20;
|
||||
else if (*line_ptr == 'G')
|
||||
absolute = 1 << 30;
|
||||
else
|
||||
absolute = -1;
|
||||
} else if (*line_ptr != '\0')
|
||||
absolute = -1;
|
||||
|
||||
if (absolute == -1) {
|
||||
printf(_("Unsupported suffix: '%s'.\n"), line_ptr);
|
||||
printf(_("Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)\n"
|
||||
" 2^N: K (KibiByte), M (MebiByte), G (GibiByte)\n"));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (absolute && res) {
|
||||
unsigned long long bytes;
|
||||
unsigned long unit;
|
||||
|
||||
bytes = (unsigned long long) res * absolute;
|
||||
unit = cxt->sector_size * fdisk_context_get_units_per_sector(cxt);
|
||||
bytes += unit/2; /* round */
|
||||
bytes /= unit;
|
||||
res = bytes;
|
||||
}
|
||||
if (minus)
|
||||
res = -res;
|
||||
res += base;
|
||||
} else {
|
||||
res = atoi(line_ptr);
|
||||
while (isdigit(*line_ptr)) {
|
||||
line_ptr++;
|
||||
use_default = 0;
|
||||
}
|
||||
}
|
||||
if (use_default) {
|
||||
printf(_("Using default value %u\n"), dflt);
|
||||
return dflt;
|
||||
}
|
||||
if (res >= low && res <= high)
|
||||
break;
|
||||
else
|
||||
printf(_("Value out of range.\n"));
|
||||
}
|
||||
if (is_suffix_used)
|
||||
*is_suffix_used = absolute > 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Print the message MESG, then read an integer in LOW..HIGH.
|
||||
* If the user hits Enter, DFLT is returned, provided that is in LOW..HIGH.
|
||||
* Answers like +10 are interpreted as offsets from BASE.
|
||||
*
|
||||
* There is no default if DFLT is not between LOW and HIGH.
|
||||
*/
|
||||
unsigned int
|
||||
read_int(struct fdisk_context *cxt,
|
||||
unsigned int low, unsigned int dflt, unsigned int high,
|
||||
unsigned int base, char *mesg)
|
||||
{
|
||||
return read_int_with_suffix(cxt, low, dflt, high, base, mesg, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void toggle_dos_compatibility_flag(struct fdisk_context *cxt)
|
||||
{
|
||||
struct fdisk_label *lb = fdisk_context_get_label(cxt, "dos");
|
||||
|
|
|
@ -69,12 +69,7 @@ extern int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
|
|||
void *data __attribute__((__unused__)));
|
||||
|
||||
/* prototypes for fdisk.c */
|
||||
extern char *line_ptr;
|
||||
|
||||
extern void list_partition_types(struct fdisk_context *cxt);
|
||||
extern int read_line(struct fdisk_context *cxt, int *asked);
|
||||
extern char read_char(struct fdisk_context *cxt, char *mesg);
|
||||
|
||||
extern struct fdisk_parttype *ask_partition_type(struct fdisk_context *cxt);
|
||||
extern void reread_partition_table(struct fdisk_context *cxt, int leave);
|
||||
extern struct partition *get_part_table(int);
|
||||
|
|
Loading…
Reference in New Issue