fdisk: properly convert byte units K,M,G and T to cylinders/sectors

the shared command language assumed 512 byte sectors, which is
not the case for fdisk as it uses cylinders for the block unit.
so we introduce an extra argument in the Edit structure and
parseexpr() function so byte sizes are properly converted to
the block unit when the K,M,G and T postfixes are used.
This commit is contained in:
cinap_lenrek 2016-01-05 03:57:50 +01:00
parent 5aeddd6788
commit dd8908cff0
6 changed files with 14 additions and 9 deletions

View file

@ -79,7 +79,7 @@ mkOP(int ty, Exp *e1, Exp *e2)
static char *inp;
static jmp_buf jmp;
static vlong dot, size, dollar;
static vlong dot, size, dollar, unit;
static char** errp;
static int
@ -110,7 +110,8 @@ yylex(void)
n *= 1024;
/* fall through */
case 'k':
n *= 2;
n *= 1024;
n /= unit; /* convert to sectors */
break;
default:
--inp;
@ -164,7 +165,7 @@ eval(Exp *e)
int yyparse(void);
char*
parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong *result)
parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong xunit, vlong *result)
{
char *err;
@ -176,6 +177,7 @@ parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong *result)
dot = xdot;
size = xsize;
dollar = xdollar;
unit = xunit;
yyparse();
if(yyexp == nil)
return "nil yylval?";

View file

@ -294,6 +294,7 @@ main(int argc, char **argv)
edit.disk->secsize = secsize;
edit.disk->secs = edit.disk->size / secsize;
}
edit.unitsz = edit.disk->secsize;
edit.end = edit.disk->secs;
if(blank)

View file

@ -129,7 +129,7 @@ editdot(Edit *edit, int argc, char **argv)
if(argc > 2)
return "args";
if(err = parseexpr(argv[1], edit->dot, edit->end, edit->end, &ndot))
if(err = parseexpr(argv[1], edit->dot, edit->end, edit->end, edit->unitsz, &ndot))
return err;
edit->dot = ndot;
@ -157,7 +157,7 @@ editadd(Edit *edit, int argc, char **argv)
fprint(2, "start %s: ", edit->unit);
q = getline(edit);
}
if(err = parseexpr(q, edit->dot, edit->end, edit->end, &start))
if(err = parseexpr(q, edit->dot, edit->end, edit->end, edit->unitsz, &start))
return err;
if(start < 0 || start >= edit->end)
@ -181,7 +181,7 @@ editadd(Edit *edit, int argc, char **argv)
fprint(2, "end [%lld..%lld] ", start, maxend);
q = getline(edit);
}
if(err = parseexpr(q, edit->dot, maxend, edit->end, &end))
if(err = parseexpr(q, edit->dot, maxend, edit->end, edit->unitsz, &end))
return err;
if(start == end)

View file

@ -36,6 +36,7 @@ struct Edit {
void *aux;
vlong dot;
vlong end;
vlong unitsz;
/* do not use fields below this line */
int changed;
@ -48,7 +49,7 @@ void runcmd(Edit*, char*);
Part *findpart(Edit*, char*);
char *addpart(Edit*, Part*);
char *delpart(Edit*, Part*);
char *parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong *result);
char *parseexpr(char *s, vlong xdot, vlong xdollar, vlong xsize, vlong xunit, vlong *result);
int ctldiff(Edit *edit, int ctlfd);
void *emalloc(ulong);
char *estrdup(char*);

View file

@ -141,8 +141,8 @@ main(int argc, char **argv)
edit.disk->secsize = secsize;
edit.disk->secs = edit.disk->size / secsize;
}
sec2cyl = edit.disk->h * edit.disk->s;
edit.unitsz = sec2cyl * edit.disk->secsize;
edit.end = edit.disk->secs / sec2cyl;
findmbr(&edit);
@ -164,7 +164,7 @@ main(int argc, char **argv)
if(dowrite || printflag)
exits(0);
fprint(2, "cylinder = %lld bytes\n", sec2cyl*edit.disk->secsize);
fprint(2, "%s = %lld bytes\n", edit.unit, edit.unitsz);
runcmd(&edit, "p");
for(;;) {
fprint(2, ">>> ");

View file

@ -160,6 +160,7 @@ main(int argc, char **argv)
disk->secsize = secsize;
disk->secs = disk->size / secsize;
}
edit.unitsz = disk->secsize;
edit.end = disk->secs;
checkfat(disk);