8000 minix/minix/commands/part/part.c at master · aliel34/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2065 lines (1806 loc) · 49.4 KB

part.c

File metadata and controls

2065 lines (1806 loc) · 49.4 KB
/* part 1.57 - Partition table editor Author: Kees J. Bot
* 13 Mar 1992
* Needs about 22k heap+stack.
*/
#define nil 0
#include <sys/types.h>
#include <stdio.h>
#include <termcap.h>
#include <errno.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/partition.h>
#include <machine/partition.h>
#include <termios.h>
/* True if a partition is an extended partition. */
#define ext_part(s) ((s) == 0x05 || (s) == 0x0F)
/* Minix master bootstrap code. */
static char MASTERBOOT[] = "/usr/mdec/mbr";
/* Template:
----first---- --geom/last-- ------sectors-----
Device Cyl Head Sec Cyl Head Sec Base Size Kb
/dev/c0d0 977 5 17
/dev/c0d0:2 0 0 2 976 4 16 2 83043 41521
Num Sort Type
0* p0 81 MINIX 0 0 3 33 4 9 3 2880 1440
1 p1 81 MINIX 33 4 10 178 2 2 2883 12284 6142
2 p2 81 MINIX 178 2 3 976 4 16 15167 67878 33939
3 p3 00 None 0 0 0 0 0 -1 0 0 0
*/
#define MAXSIZE 999999999L /* Will 1T be enough this year? */
#define SECTOR_SIZE 512
#define DEV_FD0 0x200 /* Device number of /dev/fd0 */
#define DEV_C0D0 0x300 /* Device number of /dev/c0d0 */
#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
#define arraylimit(a) ((a) + arraysize(a))
void report(const char *label)
{
fprintf(stderr, "part: %s: %s\n", label, strerror(errno));
}
void fatal(const char *label)
{
report(label);
exit(1);
}
struct termios termios;
void save_ttyflags(void)
/* Save tty attributes for later restoration. */
{
if (tcgetattr(0, &termios) < 0) fatal("");
}
void restore_ttyflags(void)
/* Reset the tty flags to how we got 'em. */
{
if (tcsetattr(0, TCSANOW, &termios) < 0) fatal("");
}
void tty_raw(void)
/* Set the terminal to raw mode, no signals, no echoing. */
{
struct termios rawterm;
rawterm= termios;
rawterm.c_lflag &= ~(ICANON|ISIG|ECHO);
rawterm.c_iflag &= ~(ICRNL);
if (tcsetattr(0, TCSANOW, &rawterm) < 0) fatal("");
}
#define ctrl(c) ((c) == '?' ? '\177' : ((c) & '\37'))
char t_cd[16], t_cm[32], t_so[16], t_se[16], t_md[16], t_me[16];
int t_li, t_co;
#define STATUSROW 10
void init_tty(void)
/* Get terminal capabilities and set the tty to "editor" mode. */
{
char *term;
static char termbuf[1024];
char *tp;
if ((term= getenv("TERM")) == nil || tgetent(termbuf, term) != 1) {
fprintf(stderr, "part: Can't get terminal capabilities\n");
exit(1);
}
if (tgetstr("cd", (tp= t_cd, &tp)) == nil
|| tgetstr("cm", (tp= t_cm, &tp)) == nil) {
fprintf(stderr, "part: This terminal is too dumb\n");
exit(1);
}
t_li= tgetnum("li");
t_co= tgetnum("co");
(void) tgetstr("so", (tp= t_so, &tp));
(void) tgetstr("se", (tp= t_se, &tp));
(void) tgetstr("md", (tp= t_md, &tp));
(void) tgetstr("me", (tp= t_me, &tp));
save_ttyflags();
tty_raw();
}
int putchr(int c)
{
return putchar(c);
}
void putstr(char *s)
{
int c;
while ((c= *s++) != 0) putchr(c);
}
void set_cursor(int row, int col)
{
tputs(tgoto(t_cm, col, row), 1, putchr);
}
int statusrow= STATUSROW;
int stat_ktl= 1;
int need_help= 1;
void stat_start(int serious)
/* Prepare for printing on a fresh status line, possibly highlighted. */
{
set_cursor(statusrow++, 0);
tputs(t_cd, 1, putchr);
if (serious) tputs(t_so, 1, putchr);
}
void stat_end(int ktl)
/* Closing bracket for stat_start. Sets "keystrokes to live" of message. */
{
tputs(t_se, 1, putchr);
stat_ktl= ktl;
need_help= 1;
}
void stat_reset(void)
/* Reset the statusline pointer and clear old messages if expired. */
{
if (stat_ktl > 0 && --stat_ktl == 0) {
statusrow= STATUSROW;
need_help= 1;
}
if (need_help && statusrow < (24-2)) {
if (statusrow > STATUSROW) stat_start(0);
stat_start(0);
putstr(
"Type '+' or '-' to change, 'r' to read, '?' for more help, 'q' to exit");
}
statusrow= STATUSROW;
need_help= 0;
}
void clear_screen(void)
{
set_cursor(0, 0);
tputs(t_cd, 1, putchr);
stat_ktl= 1;
stat_reset();
}
void reset_tty(void)
/* Reset the tty to cooked mode. */
{
restore_ttyflags();
set_cursor(statusrow, 0);
tputs(t_cd, 1, putchr);
}
void *alloc(size_t n)
{
void *m;
if ((m= malloc(n)) == nil) { reset_tty(); fatal(""); }
return m;
}
typedef enum parttype { DUNNO, SUBPART, PRIMARY, FLOPPY } parttype_t;
typedef struct device {
struct device *next, *prev; /* Circular dequeue. */
dev_t rdev; /* Device number (sorting only). */
char *name; /* E.g. /dev/c0d0 */
char *subname; /* E.g. /dev/c0d0:2 */
parttype_t parttype;
} device_t;
device_t *firstdev= nil, *curdev;
void newdevice(char *name, int scanning)
/* Add a device to the device list. If scanning is set then we are reading
* /dev, so insert the device in device number order and make /dev/c0d0 current.
*/
{
device_t *new, *nextdev, *prevdev;
struct stat st;
st.st_rdev= 0;
if (scanning) {
if (stat(name, &st) < 0 || !S_ISBLK(st.st_mode)) return;
switch (major(st.st_rdev)) {
case 2:
/* Floppy */
if (minor(st.st_rdev) >= 4) return;
break;
case 3:
case 8:
case 10:
case 12:
case 16:
/* Disk controller */
if (minor(st.st_rdev) >= 0x80
|| minor(st.st_rdev) % 5 != 0) return;
break;
default:
return;
}
/* Interesting device found. */
} else {
(void) stat(name, &st);
}
new= alloc(sizeof(*new));
new->rdev= st.st_rdev;
new->name= alloc((strlen(name) + 1) * sizeof(new->name[0]));
strcpy(new->name, name);
new->subname= new->name;
new->parttype= DUNNO;
if (major(st.st_rdev) == major(DEV_FD0) && minor(st.st_rdev) < 112) {
new->parttype= FLOPPY;
} else
if (st.st_rdev >= DEV_C0D0 && minor(st.st_rdev) < 128
&& minor(st.st_rdev) % 5 == 0) {
new->parttype= PRIMARY;
}
if (firstdev == nil) {
firstdev= new;
new->next= new->prev= new;
curdev= firstdev;
return;
}
nextdev= firstdev;
while (new->rdev >= nextdev->rdev
&& (nextdev= nextdev->next) != firstdev) {}
prevdev= nextdev->prev;
new->next= nextdev;
nextdev->prev= new;
new->prev= prevdev;
prevdev->next= new;
if (new->rdev < firstdev->rdev) firstdev= new;
if (new->rdev == DEV_C0D0) curdev= new;
if (curdev->rdev != DEV_C0D0) curdev= firstdev;
}
void getdevices(void)
/* Get all block devices from /dev that look interesting. */
{
DIR *d;
struct dirent *e;
char name[5 + NAME_MAX + 1];
if ((d= opendir("/dev")) == nil) fatal("/dev");
while ((e= readdir(d)) != nil) {
strcpy(name, "/dev/");
strcpy(name + 5, e->d_name);
newdevice(name, 1);
}
(void) closedir(d);
}
int dirty= 0;
unsigned char bootblock[SECTOR_SIZE];
struct part_entry table[1 + NR_PARTITIONS];
int existing[1 + NR_PARTITIONS];
unsigned long offset= 0, extbase= 0, extsize;
int submerged= 0;
char sort_index[1 + NR_PARTITIONS];
unsigned cylinders= 1, heads= 1, sectors= 1, secpcyl= 1;
unsigned alt_cyls= 1, alt_heads= 1, alt_secs= 1;
int precise= 0;
int device= -1;
unsigned long sortbase(struct part_entry *pe)
{
return pe->sysind == NO_PART ? -1 : pe->lowsec;
}
void sort(void)
/* Let the sort_index array show the order partitions are sorted in. */
{
int i, j;
int idx[1 + NR_PARTITIONS];
for (i= 1; i <= NR_PARTITIONS; i++) idx[i]= i;
for (i= 1; i <= NR_PARTITIONS; i++) {
for (j= 1; j <= NR_PARTITIONS-1; j++) {
int sj= idx[j], sj1= idx[j+1];
if (sortbase(&table[sj]) > sortbase(&table[sj1])) {
idx[j]= sj1;
idx[j+1]= sj;
}
}
}
for (i= 1; i <= NR_PARTITIONS; i++) sort_index[idx[i]]= i;
}
void dos2chs(unsigned char *dos, unsigned *chs)
/* Extract cylinder, head and sector from the three bytes DOS uses to address
* a sector. Note that bits 8 & 9 of the cylinder number come from bit 6 & 7
* of the sector byte. The sector number is rebased to count from 0.
*/
{
chs[0]= ((dos[1] & 0xC0) << 2) | dos[2];
chs[1]= dos[0];
chs[2]= (dos[1] & 0x3F) - 1;
}
void abs2dos(unsigned char *dos, unsigned long pos)
/* Translate a sector offset to three DOS bytes. */
{
unsigned h, c, s;
c= pos / secpcyl;
h= (pos % secpcyl) / sectors;
s= pos % sectors + 1;
dos[0]= h;
dos[1]= s | ((c >> 2) & 0xC0);
dos[2]= c & 0xFF;
}
void recompute0(void)
/* Recompute the partition size for the device after a geometry change. */
{
if (device < 0) {
cylinders= heads= sectors= 1;
memset(table, 0, sizeof(table));
} else
if (!precise && offset == 0) {
table[0].lowsec= 0;
table[0].size= (unsigned long) cylinders * heads * sectors;
}
table[0].sysind= device < 0 ? NO_PART : MINIX_PART;
secpcyl= heads * sectors;
}
void guess_geometry(void)
/* With a bit of work one can deduce the disk geometry from the partition
* table. This may be necessary if the driver gets it wrong. (If partition
* tables didn't have C/H/S numbers we would not care at all...)
*/
{
int i, n;
struct part_entry *pe;
unsigned chs[3];
unsigned long sec;
unsigned h, s;
unsigned char HS[256][8]; /* Bit map off all possible H/S */
alt_cyls= alt_heads= alt_secs= 0;
/* Initially all possible H/S combinations are possible. HS[h][0]
* bit 0 is used to rule out a head value.
*/
for (h= 1; h <= 255; h++) {
for (s= 0; s < 8; s++) HS[h][s]= 0xFF;
}
for (i= 0; i < 2*NR_PARTITIONS; i++) {
pe= &(table+1)[i >> 1];
if (pe->sysind == NO_PART) continue;
/* Get the end or start sector numbers (in that order). */
if ((i & 1) == 0) {
dos2chs(&pe->last_head, chs);
sec= pe->lowsec + pe->size - 1;
} else {
dos2chs(&pe->start_head, chs);
sec= pe->lowsec;
}
if (chs[0] >= alt_cyls) alt_cyls= chs[0]+1;
/* Which H/S combinations can be ruled out? */
for (h= 1; h <= 255; h++) {
if (HS[h][0] == 0) continue;
n = 0;
for (s= 1; s <= 63; s++) {
if ((chs[0] * h + chs[1]) * s + chs[2] != sec) {
HS[h][s/8] &= ~(1 << (s%8));
}
if (HS[h][s/8] & (1 << (s%8))) n++;
}
if (n == 0) HS[h][0]= 0;
}
}
/* See if only one remains. */
i= 0;
for (h= 1; h <= 255; h++) {
if (HS[h][0] == 0) continue;
for (s= 1; s <= 63; s++) {
if (HS[h][s/8] & (1 << (s%8))) {
i++;
alt_heads= h;
alt_secs= s;
}
}
}
/* Forget it if more than one choice... */
if (i > 1) alt_cyls= alt_heads= alt_secs= 0;
}
void geometry(void)
/* Find out the geometry of the device by querying the driver, or by looking
* at the partition table. These numbers are crosschecked to make sure that
* the geometry is correct. Master bootstraps other than the Minix one use
* the CHS numbers in the partition table to load the bootstrap of the active
* partition.
*/
{
struct stat dst;
int err= 0;
struct part_geom geometry;
if (submerged) {
/* Geometry already known. */
sort();
return;
}
precise= 0;
cylinders= 0;
recompute0();
if (device < 0) return;
/* Try to guess the geometry from the partition table. */
guess_geometry();
/* Try to get the geometry from the driver. */
(void) fstat(device, &dst);
if (S_ISBLK(dst.st_mode) || S_ISCHR(dst.st_mode)) {
/* Try to get the drive's geometry from the driver. */
if (ioctl(device, DIOCGETP, &geometry) < 0)
err= errno;
else {
table[0].lowsec= (unsigned long)(geometry.base /
SECTOR_SIZE);
table[0].size= (unsigned long)(geometry.size /
SECTOR_SIZE);
cylinders= geometry.cylinders;
heads= geometry.heads;
sectors= geometry.sectors;
precise= 1;
}
} else {
err= ENODEV;
}
if (err != 0) {
/* Getting the geometry from the driver failed, so use the
* alternate geometry.
*/
if (alt_heads == 0) {
alt_cyls= table[0].size / (64 * 32);
alt_heads= 64;
alt_secs= 32;
}
cylinders= alt_cyls;
heads= alt_heads;
sectors= alt_secs;
stat_start(1);
printf("Failure to get the geometry of %s: %s", curdev->name,
errno == ENOTTY ? "No driver support" : strerror(err));
stat_end(5);
stat_start(0);
printf("The geometry has been guessed as %ux%ux%u",
cylinders, heads, sectors);
stat_end(5);
} else {
if (alt_heads == 0) {
alt_cyls= cylinders;
alt_heads= heads;
alt_secs= sectors;
}
if (heads != alt_heads || sectors != alt_secs) {
stat_start(1);
printf("WARNING:");
stat_end(10);
stat_start(0);
printf(
"The %ux%ux%u geometry obtained from the device driver does not match",
cylinders, heads, sectors);
stat_end(10);
stat_start(0);
printf(
"the %ux%ux%u geometry implied by the partition table. Hit 'X' to switch",
alt_cyls, alt_heads, alt_secs);
stat_end(10);
stat_start(0);
printf(
"between the two geometries to see what is best. Note that the geometry");
stat_end(10);
stat_start(0);
printf(
"must be correct when the table is written or the system may not boot!");
stat_end(10);
}
}
/* Show the base and size of the device instead of the whole drive.
* This makes sense for subpartitioning primary partitions.
*/
if (precise && ioctl(device, DIOCGETP, &geometry) >= 0) {
table[0].lowsec= (unsigned long)(geometry.base / SECTOR_SIZE);
table[0].size = (unsigned long)(geometry.size / SECTOR_SIZE);
} else {
precise= 0;
}
recompute0();
sort();
}
typedef struct indicators { /* Partition type to partition name. */
unsigned char ind;
char name[10];
} indicators_t;
indicators_t ind_table[]= {
{ 0x00, "None" },
{ 0x01, "FAT-12" },
{ 0x02, "XENIX /" },
{ 0x03, "XENIX usr" },
{ 0x04, "FAT-16" },
{ 0x05, "EXTENDED" },
{ 0x06, "FAT-16" },
{ 0x07, "HPFS/NTFS" },
{ 0x08, "AIX" },
{ 0x09, "COHERENT" },
{ 0x0A, "OS/2" },
{ 0x0B, "FAT-32" },
{ 0x0C, "FAT?" },
{ 0x0E, "FAT?" },
{ 0x0F, "EXTENDED" },
{ 0x10, "OPUS" },
{ 0x40, "VENIX286" },
{ 0x42, "W2000 Dyn" },
{ 0x52, "MICROPORT" },
{ 0x63, "386/IX" },
{ 0x64, "NOVELL286" },
{ 0x65, "NOVELL386" },
{ 0x75, "PC/IX" },
{ 0x80, "MINIX-OLD" },
{ 0x81, "MINIX" },
{ 0x82, "LINUXswap" },
{ 0x83, "LINUX" },
{ 0x93, "AMOEBA" },
{ 0x94, "AMOEBAbad" },
{ 0xA5, "386BSD" },
{ 0xB7, "BSDI" },
{ 0xB8, "BSDI swap" },
{ 0xC7, "SYRINX" },
{ 0xDB, "CPM" },
{ 0xFF, "BADBLOCKS" },
};
char *typ2txt(int ind)
/* Translate a numeric partition indicator for human eyes. */
{
indicators_t *pind;
for (pind= ind_table; pind < arraylimit(ind_table); pind++) {
if (pind->ind == ind) return pind->name;
}
return "";
}
int round_sysind(int ind, int delta)
/* Find the next known partition type starting with ind in direction delta. */
{
indicators_t *pind;
ind= (ind + delta) & 0xFF;
if (delta < 0) {
for (pind= arraylimit(ind_table)-1; pind->ind > ind; pind--) {}
} else {
for (pind= ind_table; pind->ind < ind; pind++) {}
}
return pind->ind;
}
/* Objects on the screen, either simple pieces of the text or the cylinder
* number of the start of partition three.
*/
typedef enum objtype {
O_INFO, O_TEXT, O_DEV, O_SUB,
O_TYPTXT, O_SORT, O_NUM, O_TYPHEX,
O_CYL, O_HEAD, O_SEC,
O_SCYL, O_SHEAD, O_SSEC, O_LCYL, O_LHEAD, O_LSEC, O_BASE, O_SIZE, O_KB
} objtype_t;
#define rjust(type) ((type) >= O_TYPHEX)
#define computed(type) ((type) >= O_TYPTXT)
typedef struct object {
struct object *next;
objtype_t type; /* Text field, cylinder number, etc. */
char flags; /* Modifiable? */
char row;
char col;
char len;
struct part_entry *entry; /* What does the object refer to? */
char *text;
char value[20]; /* Value when printed. */
} object_t;
#define OF_MOD 0x01 /* Object value is modifiable. */
#define OF_ODD 0x02 /* It has a somewhat odd value. */
#define OF_BAD 0x04 /* Its value is no good at all. */
/* Events: (Keypress events are the value of the key pressed.) */
#define E_ENTER (-1) /* Cursor moves onto object. */
#define E_LEAVE (-2) /* Cursor leaves object. */
#define E_WRITE (-3) /* Write, but not by typing 'w'. */
/* The O_SIZE objects have a dual identity. */
enum howend { SIZE, LAST } howend= SIZE;
object_t *world= nil;
object_t *curobj= nil;
object_t *newobject(objtype_t type, int flags, int row, int col, int len)
/* Make a new object given a type, flags, position and length on the screen. */
{
object_t *new;
object_t **aop= &world;
new= alloc(sizeof(*new));
new->type= type;
new->flags= flags;
new->row= row;
new->col= col;
new->len= len;
new->entry= nil;
new->text= "";
new->value[0]= 0;
new->next= *aop;
*aop= new;
return new;
}
unsigned long entry2base(struct part_entry *pe)
/* Return the base sector of the partition if defined. */
{
return pe->sysind == NO_PART ? 0 : pe->lowsec;
}
unsigned long entry2last(struct part_entry *pe)
{
return pe->sysind == NO_PART ? -1 : pe->lowsec + pe->size - 1;
}
unsigned long entry2size(struct part_entry *pe)
{
return pe->sysind == NO_PART ? 0 : pe->size;
}
int overlap(unsigned long sec)
/* See if sec is part of another partition. */
{
struct part_entry *pe;
for (pe= table + 1; pe <= table + NR_PARTITIONS; pe++) {
if (pe->sysind == NO_PART) continue;
if (pe->lowsec < sec && sec < pe->lowsec + pe->size)
return 1;
}
return 0;
}
int aligned(unsigned long sec, unsigned unit)
/* True if sec is aligned to unit or if it is no problem if it is unaligned. */
{
return (offset != 0 && extbase == 0) || (sec % unit == 0);
}
void print(object_t *op)
/* Print an object's value if it changed. */
{
struct part_entry *pe= op->entry;
int n;
unsigned long t;
char *name;
int oldflags;
char oldvalue[20];
/* Remember the old flags and value. */
oldflags= op->flags;
strcpy(oldvalue, op->value);
op->flags&= ~(OF_ODD | OF_BAD);
switch (op->type) {
case O_INFO: {
/* Current field. */
static struct field { int type; char *name; } fields[]= {
{ O_DEV, "Select device" },
{ O_NUM, "Active flag" },
{ O_TYPHEX, "Hex partition type" },
{ O_TYPTXT, "Partition type" },
{ O_SCYL, "Start cylinder" },
{ O_SHEAD, "Start head" },
{ O_SSEC, "Start sector" },
{ O_CYL, "Number of cylinders" },
{ O_HEAD, "Number of heads" },
{ O_SEC, "Sectors per track" },
{ O_LCYL, "Last cylinder" },
{ O_LHEAD, "Last head" },
{ O_LSEC, "Last sector" },
{ O_BASE, "Base sector" },
{ O_SIZE, "Size in sectors" },
{ O_KB, "Size in kilobytes" },
{ -1, "?" },
};
struct field *fp= fields;
while (fp->type >= 0 && fp->type != curobj->type) fp++;
strcpy(op->value, fp->name);
op->flags|= OF_ODD;
break; }
case O_TEXT:
/* Simple text field. */
strcpy(op->value, op->text);
break;
case O_DEV:
case O_SUB:
/* Name of currently edited device. */
name= op->type == O_DEV ? curdev->name :
offset == 0 ? "" : curdev->subname;
if ((n= strlen(name)) < op->len) n= op->len;
strcpy(op->value, name + (n - op->len));
if (device < 0 && op->type == O_DEV) op->flags|= OF_BAD;
break;
case O_NUM:
/* Position and active flag. */
sprintf(op->value, "%d%c", (int) (pe - table - 1),
pe->bootind & ACTIVE_FLAG ? '*' : ' ');
break;
case O_SORT:
/* Position if the driver sorts the table. */
sprintf(op->value, "%s%d",
curdev->parttype >= PRIMARY ? "p" :
curdev->parttype == SUBPART ? "s" : "",
(curdev->parttype == SUBPART ||
curdev->parttype == FLOPPY ? pe - table
: sort_index[pe - table]) - 1);
break;
case O_TYPHEX:
/* Hex partition type indicator. */
sprintf(op->value, "%02X", pe->sysind);
break;
case O_TYPTXT:
/* Ascii partition type indicator. */
strcpy(op->value, typ2txt(pe->sysind));
break;
case O_SCYL:
/* Partition's start cylinder. */
sprintf(op->value, "%lu", entry2base(pe) / secpcyl);
break;
case O_SHEAD:
/* Start head. */
t= entry2base(pe);
sprintf(op->value, "%lu", t % secpcyl / sectors);
if (!aligned(t, secpcyl) && t != table[0].lowsec + sectors)
op->flags|= OF_ODD;
break;
case O_SSEC:
/* Start sector. */
t= entry2base(pe);
sprintf(op->value, "%lu", t % sectors);
if (!aligned(t, sectors)) op->flags|= OF_ODD;
break;
case O_CYL:
/* Number of cylinders. */
sprintf(op->value, "%u", cylinders);
break;
case O_HEAD:
/* Number of heads. */
sprintf(op->value, "%u", heads);
break;
case O_SEC:
/* Number of sectors per track. */
sprintf(op->value, "%u", sectors);
break;
case O_LCYL:
/* Partition's last cylinder. */
t= entry2last(pe);
sprintf(op->value, "%lu", t == -1 ? 0 : t / secpcyl);
break;
case O_LHEAD:
/* Partition's last head. */
t= entry2last(pe);
sprintf(op->value, "%lu", t == -1 ? 0 : t % secpcyl / sectors);
if (!aligned(t + 1, secpcyl)) op->flags|= OF_ODD;
break;
case O_LSEC:
/* Partition's last sector. */
t= entry2last(pe);
if (t == -1) strcpy(op->value, "-1");
else sprintf(op->value, "%lu", t % sectors);
if (!aligned(t + 1, sectors)) op->flags|= OF_ODD;
break;
case O_BASE:
/* Partition's base sector. */
sprintf(op->value, "%lu", entry2base(pe));
if (pe->sysind != NO_PART && pe != &table[0]
&& (pe->lowsec <= table[0].lowsec || overlap(pe->lowsec)))
op->flags|= OF_BAD;
break;
case O_SIZE:
/* Size of partitition in sectors. */
t= howend == SIZE ? entry2size(pe) : entry2last(pe);
sprintf(op->value, "%lu", pe->sysind == NO_PART ? 0 : t);
if (pe->sysind != NO_PART && (pe->size == 0
|| pe->lowsec + pe->size > table[0].lowsec + table[0].size
|| overlap(pe->lowsec + pe->size)))
op->flags|= OF_BAD;
break;
case O_KB:
/* Size of partitition in kilobytes. */
sprintf(op->value, "%lu", entry2size(pe) / 2);
break;
default:
sprintf(op->value, "?? %d ??", op->type);
}
if (device < 0 && computed(op->type)) strcpy(op->value, "?");
/* If a value overflows the print field then show a blank
* reverse video field.
*/
if ((n= strlen(op->value)) > op->len) {
n= 0;
op->flags|= OF_BAD;
}
/* Right or left justified? */
if (rjust(op->type)) {
memmove(op->value + (op->len - n), op->value, n);
memset(op->value, ' ', op->len - n);
} else {
memset(op->value + n, ' ', op->len - n);
}
op->value[(int) op->len]= 0;
if ((op->flags & (OF_ODD | OF_BAD)) == (oldflags & (OF_ODD | OF_BAD))
&& strcmp(op->value, oldvalue) == 0) {
/* The value did not change. */
return;
}
set_cursor(op->row, rjust(op->type) ? op->col - (op->len-1) : op->col);
if (op->flags & OF_BAD) tputs(t_so, 1, putchr);
else
if (op->flags & OF_ODD) tputs(t_md, 1, putchr);
putstr(op->value);
if (op->flags & OF_BAD) tputs(t_se, 1, putchr);
else
if (op->flags & OF_ODD) tputs(t_me, 1, putchr);
}
void display(void)
/* Repaint all objects that changed. */
{
object_t *op;
for (op= world; op != nil; op= op->next) print(op);
}
int typing; /* Set if a digit has been typed to set a value. */
int magic; /* Changes when using the magic key. */
void event(int ev, object_t *op);
void m_redraw(int ev, object_t *op)
/* Redraw the screen. */
{
object_t *op2;
if (ev != ctrl('L')) return;
clear_screen();
for (op2= world; op2 != nil; op2= op2->next) op2->value[0]= 0;
}
void m_toggle(int ev, object_t *op)
/* Toggle between the driver and alternate geometry. */
{
unsigned t;
if (ev != 'X') return;
if (alt_cyls == cylinders && alt_heads == heads && alt_secs == sectors)
return;
t= cylinders; cylinders= alt_cyls; alt_cyls= t;
t= heads; heads= alt_heads; alt_heads= t;
t= sectors; sectors= alt_secs; alt_secs= t;
dirty= 1;
recompute0();
}
char size_last[]= "Size";
void m_orientation(int ev, object_t *op)
{
if (ev != ' ') return;
switch (howend) {
case SIZE:
howend= LAST;
strcpy(size_last, "Last");
break;
case LAST:
howend= SIZE;
strcpy(size_last, "Size");
}
}
void m_move(int ev, object_t *op)
/* Move to the nearest modifiably object in the intended direction. Objects
* on the same row or column are really near.
*/
{
object_t *near, *op2;
unsigned dist, d2, dr, dc;
if (ev != 'h' && ev != 'j' && ev != 'k' && ev != 'l' && ev != 'H')
return;
if (device < 0) {
/* No device open? Then try to read first. */
event('r', op);
if (device < 0) return;
}
near= op;
dist= -1;
for (op2= world; op2 != nil; op2= op2->next) {
if (op2 == op || !(op2->flags & OF_MOD)) continue;
dr= abs(op2->row - op->row);
dc= abs(op2->col - op->col);
d2= 25*dr*dr + dc*dc;
if (op2->row != op->row && op2->col != op->col) d2+= 1000;
switch (ev) {
case 'h': /* Left */
0