Files
lotherkandClaude Sonnet 5.5 d53ea094e7 licence: CNCL for what Makronix wrote itself
LICENSE at the root; the 103 files new in ext/src/mcde (none was in the CDE
import) carry the CNCL line instead of the LGPL notice.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:48:02 +00:00

927 lines
29 KiB
C

/*
* main.c -- dtdiskutility: the disks of the computer, and what can be done
* to them
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* On the left the drives, each followed by its partitions. On the right
* what is chosen: its name, model, size, file system, where it is mounted
* and how full, as a bar; below, two pages:
*
* First Aid Verify Disk (fsck -n: looks, changes nothing), Verify
* Permissions and Repair Permissions (the system's files
* against the list the system keeps), with their report;
* Erase a drive made new: one partition, a file system, a name --
* after a question that says what is lost.
*
* The toolbar mounts and unmounts. Everything goes through McdeSys
* (mcde_sys_disks, _disk_mount, _unmount, _check, _erase,
* mcde_sys_permissions). Changing anything needs the administrator:
* as a user the program shows and verifies, and Unlock... starts it again
* as root in a terminal where su(1) asks for the password.
*
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <Xm/Xm.h>
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/Label.h>
#include <Xm/List.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Dt/Dt.h>
#include <Mcde/McdeSys.h>
#define MAX_DISKS 64
static XtAppContext app;
static Widget shell, diskList, title, info, bar, pageAid, pageErase;
static Widget pageRaid, pageRestore;
static Widget tabAid, tabErase, tabRaid, tabRestore, report, mountButton;
static Widget unmountButton, ejectButton, verifyTool, infoTool, logTool;
static Widget eraseName, eraseFormat, eraseButton, eraseNote, statusLine;
static Widget detail, detDevice, detMount, detFormat, detCapacity;
static Widget detAvailable, detUsed, detailsToggle;
static Widget fsButtons[3];
static McdeSysDisk disks[MAX_DISKS];
static int ndisks, chosen = -1, isRoot;
static const char *fallback[] = {
"*renderTable: mcde",
"*mcde.fontType: FONT_IS_XFT",
"*mcde.fontName: Hack",
"*mcde.fontSize: 10",
NULL
};
/* ---- small helpers ------------------------------------------------------ */
static void
SetText(Widget w, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
XtVaSetValues(w, XmNlabelString, s, NULL);
XmStringFree(s);
}
static Widget
Text(Widget parent, const char *name, const char *text)
{
XmString s = XmStringCreateLocalized((char *) text);
Widget w = XtVaCreateManagedWidget(name, xmLabelWidgetClass, parent,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING, NULL);
XmStringFree(s);
return w;
}
static Widget
Button(Widget parent, const char *label, XtCallbackProc cb, XtPointer data)
{
XmString s = XmStringCreateLocalized((char *) label);
Widget b = XtVaCreateManagedWidget("button", xmPushButtonWidgetClass,
parent, XmNlabelString, s, NULL);
XmStringFree(s);
if (cb != NULL)
XtAddCallback(b, XmNactivateCallback, cb, data);
return b;
}
static void
Size(unsigned long long b, char *buf, size_t len)
{
if (b >= 1000000000000ULL)
snprintf(buf, len, "%.2f TB", b / 1e12);
else if (b >= 1000000000ULL)
snprintf(buf, len, "%.1f GB", b / 1e9);
else if (b >= 1000000ULL)
snprintf(buf, len, "%.0f MB", b / 1e6);
else
snprintf(buf, len, "%llu bytes", b);
}
static void
ShowStatus(const char *text)
{
SetText(statusLine, text);
XmUpdateDisplay(statusLine);
}
static void
Message(const char *text, int error)
{
Widget d;
XmString s = XmStringCreateLocalized((char *) text);
d = error ? XmCreateErrorDialog(shell, "message", NULL, 0) :
XmCreateInformationDialog(shell, "message", NULL, 0);
XtVaSetValues(d, XmNmessageString, s, XmNdialogTitle,
XmStringCreateLocalized((char *) "Disk Utility"), NULL);
XmStringFree(s);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtManageChild(d);
}
/* ---- the bar: used and free ---------------------------------------------- */
static void
BarExpose(Widget w, XtPointer data, XtPointer call)
{
Display *dpy = XtDisplay(w);
Dimension width = 0, height = 0;
Pixel fg, bg, sel;
GC gc;
unsigned long long total;
int used = 0;
(void) data;
(void) call;
if (!XtIsRealized(w))
return;
XtVaGetValues(w, XmNwidth, &width, XmNheight, &height,
XmNforeground, &fg, XmNbackground, &bg, NULL);
XtVaGetValues(info, XmNbackground, &sel, NULL);
gc = XCreateGC(dpy, XtWindow(w), 0, NULL);
XSetForeground(dpy, gc, bg);
XFillRectangle(dpy, XtWindow(w), gc, 0, 0, width, height);
if (chosen >= 0 && disks[chosen].mountpoint[0] != '\0') {
total = disks[chosen].used + disks[chosen].avail;
if (total > 0)
used = (int) ((double) disks[chosen].used / total *
(width - 2));
/* the used part in the palette's select colour, as a scale */
XtVaGetValues(diskList, XmNselectColor, &sel, NULL);
XSetForeground(dpy, gc, sel);
XFillRectangle(dpy, XtWindow(w), gc, 1, 1, (unsigned) used,
height > 2 ? height - 2 : 1);
}
XSetForeground(dpy, gc, fg);
XDrawRectangle(dpy, XtWindow(w), gc, 0, 0, width - 1, height - 1);
XFreeGC(dpy, gc);
}
/* ---- the list and the information ---------------------------------------- */
static void
ShowInfo(void)
{
char text[2048], size[32], used[32], avail[32];
McdeSysDisk *d;
XtSetSensitive(mountButton, False);
XtSetSensitive(unmountButton, False);
if (chosen < 0 || chosen >= ndisks) {
SetText(title, "Choose a disk");
SetText(info, "");
if (detDevice != NULL) {
SetText(detDevice, "-");
SetText(detMount, "-");
SetText(detFormat, "-");
SetText(detCapacity, "-");
SetText(detAvailable, "-");
SetText(detUsed, "-");
XtSetSensitive(ejectButton, False);
XtSetSensitive(verifyTool, False);
XtSetSensitive(infoTool, False);
XtSetSensitive(logTool, False);
}
BarExpose(bar, NULL, NULL);
return;
}
d = &disks[chosen];
Size(d->bytes, size, sizeof size);
if (d->parent[0] == '\0')
snprintf(text, sizeof text, "%s", d->model[0] != '\0' ?
d->model : d->name);
else
snprintf(text, sizeof text, "%s", d->label[0] != '\0' ?
d->label : d->name);
SetText(title, text);
{
size_t n = 0;
n += (size_t) snprintf(text + n, sizeof text - n,
"Device: /dev/%s\nCapacity: %s\n", d->name, size);
if (d->parent[0] != '\0')
n += (size_t) snprintf(text + n, sizeof text - n,
"On drive: %s\n", d->parent);
if (d->fstype[0] != '\0')
n += (size_t) snprintf(text + n, sizeof text - n,
"File system: %s\n", d->fstype);
if (d->mountpoint[0] != '\0') {
Size(d->used, used, sizeof used);
Size(d->avail, avail, sizeof avail);
n += (size_t) snprintf(text + n, sizeof text - n,
"Mounted at: %s\nUsed: %s Available: %s\n",
d->mountpoint, used, avail);
} else if (d->parent[0] != '\0')
n += (size_t) snprintf(text + n, sizeof text - n,
"Not mounted\n");
n += (size_t) snprintf(text + n, sizeof text - n, "%s%s",
d->removable ? "Removable\n" : "",
d->system ? "Holds the system\n" : "");
}
SetText(info, text);
{
char dev[64], size[32], used[32], avail[32];
(void)snprintf(dev, sizeof dev, "/dev/%s", d->name);
SetText(detDevice, dev);
SetText(detMount, d->mountpoint[0] != '\0' ? d->mountpoint :
"-");
SetText(detFormat, d->fstype[0] != '\0' ? d->fstype : "-");
Size(d->bytes, size, sizeof size);
Size(d->used, used, sizeof used);
Size(d->avail, avail, sizeof avail);
SetText(detCapacity, size);
SetText(detAvailable, d->mountpoint[0] != '\0' ? avail : "-");
SetText(detUsed, d->mountpoint[0] != '\0' ? used : "-");
}
XtSetSensitive(ejectButton, isRoot && d->mountpoint[0] != '\0' &&
!d->system);
XtSetSensitive(verifyTool, d->parent[0] != '\0' && !d->system);
XtSetSensitive(infoTool, True);
XtSetSensitive(logTool, True);
BarExpose(bar, NULL, NULL);
if (isRoot && d->parent[0] != '\0' && !d->system) {
XtSetSensitive(mountButton, d->mountpoint[0] == '\0' &&
d->fstype[0] != '\0' && strcmp(d->fstype, "swap") != 0);
XtSetSensitive(unmountButton, d->mountpoint[0] != '\0');
}
/* erasing is for a whole drive that is not the system's */
XtSetSensitive(eraseButton, isRoot && d->parent[0] == '\0' &&
!d->system && d->bytes > 0);
SetText(eraseNote, d->parent[0] != '\0' ? "Choose a drive to erase "
"it." : d->system ? "This drive holds the system: it is not "
"erased." : "Erasing makes one partition over the whole drive; "
"all on it is lost.");
}
static void
Reload(void)
{
char text[512], size[32];
char keep[32] = "";
int i;
if (chosen >= 0 && chosen < ndisks)
snprintf(keep, sizeof keep, "%s", disks[chosen].name);
ndisks = mcde_sys_disks(disks, MAX_DISKS);
if (ndisks < 0) {
ShowStatus(mcde_sys_error());
ndisks = 0;
}
XmListDeleteAllItems(diskList);
chosen = -1;
for (i = 0; i < ndisks; i++) {
XmString s;
McdeSysDisk *d = &disks[i];
Size(d->bytes, size, sizeof size);
if (d->parent[0] == '\0')
snprintf(text, sizeof text, "%s %s",
d->model[0] != '\0' ? d->model : d->name, size);
else
snprintf(text, sizeof text, " %s %s",
d->label[0] != '\0' ? d->label :
(d->mountpoint[0] != '\0' ? d->mountpoint :
d->name), size);
s = XmStringCreateLocalized(text);
XmListAddItemUnselected(diskList, s, 0);
XmStringFree(s);
if (strcmp(d->name, keep) == 0)
chosen = i;
}
if (chosen >= 0)
XmListSelectPos(diskList, chosen + 1, False);
ShowInfo();
}
static void
Chosen(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
chosen = ((XmListCallbackStruct *) call)->item_position - 1;
ShowInfo();
}
/* ---- the actions --------------------------------------------------------- */
static void
MountCB(Widget w, XtPointer data, XtPointer call)
{
char where[300], text[400];
(void) w;
(void) data;
(void) call;
if (chosen < 0)
return;
if (mcde_sys_disk_mount(disks[chosen].name, where, sizeof where) !=
MCDE_SYS_OK)
Message(mcde_sys_error(), 1);
else {
snprintf(text, sizeof text, "Mounted at %s.", where);
ShowStatus(text);
}
Reload();
}
static void
UnmountCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (chosen < 0)
return;
if (mcde_sys_disk_unmount(disks[chosen].name) != MCDE_SYS_OK)
Message(mcde_sys_error(), 1);
else
ShowStatus("Unmounted.");
Reload();
}
static void
Report(const char *head, const char *body)
{
size_t n = strlen(head) + strlen(body) + 4;
char *t = malloc(n);
if (t == NULL)
return;
snprintf(t, n, "%s\n\n%s", head, body);
XmTextSetString(report, t);
free(t);
}
static void
VerifyDisk(Widget w, XtPointer data, XtPointer call)
{
static char log[65536];
char head[300];
(void) w;
(void) data;
(void) call;
if (chosen < 0 || disks[chosen].parent[0] == '\0') {
Report("Choose a partition to verify.", "");
return;
}
ShowStatus("Verifying ...");
if (mcde_sys_disk_check(disks[chosen].name, log, sizeof log) ==
MCDE_SYS_OK)
snprintf(head, sizeof head, "%s: no problems found.",
disks[chosen].name);
else
snprintf(head, sizeof head, "%s: %s", disks[chosen].name,
mcde_sys_error());
Report(head, log);
ShowStatus("");
}
static void
Permissions(Widget w, XtPointer data, XtPointer call)
{
static char log[262144];
int repair = (int) (intptr_t) data, n;
char head[300];
(void) w;
(void) call;
ShowStatus(repair ? "Repairing permissions ..." :
"Verifying permissions ...");
n = mcde_sys_permissions(repair, log, sizeof log);
if (n < 0)
snprintf(head, sizeof head, "%s", mcde_sys_error());
else if (n == 0)
snprintf(head, sizeof head, "Every file has the permissions "
"it should have.");
else
snprintf(head, sizeof head, repair ? "%d files were set back:"
: "%d files differ:", n);
Report(head, n > 0 ? log : "");
ShowStatus("");
}
static const char *const fsNames[3] = { "ffs", "msdos", "ext4" };
static void
EraseNow(Widget w, XtPointer data, XtPointer call)
{
char *name = XmTextFieldGetString(eraseName), part[64], text[400];
Widget chosenFs = NULL;
int i, fs = 0;
(void) w;
(void) data;
(void) call;
XtVaGetValues(eraseFormat, XmNmenuHistory, &chosenFs, NULL);
for (i = 0; i < 3; i++)
if (fsButtons[i] == chosenFs)
fs = i;
ShowStatus("Erasing ...");
if (mcde_sys_disk_erase(disks[chosen].name, fsNames[fs],
name != NULL ? name : "", part, sizeof part) != MCDE_SYS_OK)
Message(mcde_sys_error(), 1);
else {
snprintf(text, sizeof text, "%s is erased: %s, named \"%s\".",
disks[chosen].name, part, name);
ShowStatus(text);
}
XtFree(name);
Reload();
}
static void
EraseAsk(Widget w, XtPointer data, XtPointer call)
{
Widget d;
XmString s, ok;
char text[600], size[32];
(void) w;
(void) data;
(void) call;
if (chosen < 0)
return;
Size(disks[chosen].bytes, size, sizeof size);
snprintf(text, sizeof text, "Erase %s (%s, %s)?\n\nEverything on "
"this drive is lost, every partition and every file. This "
"cannot be undone.", disks[chosen].model[0] != '\0' ?
disks[chosen].model : disks[chosen].name, disks[chosen].name,
size);
d = XmCreateWarningDialog(shell, "eraseAsk", NULL, 0);
s = XmStringCreateLocalized(text);
ok = XmStringCreateLocalized("Erase");
XtVaSetValues(d, XmNmessageString, s, XmNokLabelString, ok,
XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON, NULL);
XmStringFree(s);
XmStringFree(ok);
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
XtAddCallback(d, XmNokCallback, EraseNow, NULL);
XtManageChild(d);
}
static void
RefreshCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
Reload();
}
static void
Spawn(const char *command)
{
pid_t pid = fork();
if (pid == 0) {
if (fork() == 0) {
setsid();
execl("/bin/sh", "sh", "-c", command, (char *) NULL);
_exit(127);
}
_exit(0);
}
if (pid > 0)
(void) waitpid(pid, NULL, 0);
}
static void
Unlock(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
Spawn("dtterm -title 'Disk Utility' -geometry 70x12 -e su -m root "
"-c dtdiskutility");
}
static void ShowPage(Widget w, XtPointer data, XtPointer call);
/* ---- the toolbar's own actions ------------------------------------------ */
/* NetBSD/Linux have no eject through McdeSys yet: unmounting is what can be
* done, and the status says so */
static void
EjectCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
if (chosen < 0 || chosen >= ndisks)
return;
if (disks[chosen].mountpoint[0] == '\0') {
ShowStatus("Not mounted: there is nothing to eject.");
return;
}
if (mcde_sys_disk_unmount(disks[chosen].name) != MCDE_SYS_OK)
Message(mcde_sys_error(), 1);
else
ShowStatus("Unmounted; the medium can be removed.");
Reload();
}
static void
InfoCB(Widget w, XtPointer data, XtPointer call)
{
char text[1024], size[32], used[32], avail[32];
(void) w;
(void) data;
(void) call;
if (chosen < 0 || chosen >= ndisks) {
Message("Choose a disk first.", 0);
return;
}
Size(disks[chosen].bytes, size, sizeof size);
Size(disks[chosen].used, used, sizeof used);
Size(disks[chosen].avail, avail, sizeof avail);
(void)snprintf(text, sizeof text,
"Device: /dev/%s\nFormat: %s\nMount point: %s\n"
"Capacity: %s\nAvailable: %s\nUsed: %s%s%s",
disks[chosen].name,
disks[chosen].fstype[0] != '\0' ? disks[chosen].fstype : "-",
disks[chosen].mountpoint[0] != '\0' ?
disks[chosen].mountpoint : "-", size, avail, used,
disks[chosen].removable ? "\nRemovable" : "",
disks[chosen].system ? "\nHolds the system" : "");
Message(text, 0);
}
/* "Log": the First Aid page with its report open */
static void
LogCB(Widget w, XtPointer data, XtPointer call)
{
(void) w;
(void) data;
(void) call;
ShowPage(NULL, (XtPointer) 0, NULL);
XmToggleButtonSetState(tabAid, True, False);
XmToggleButtonSetState(tabErase, False, False);
XmToggleButtonSetState(tabRaid, False, False);
XmToggleButtonSetState(tabRestore, False, False);
if (detailsToggle != NULL)
XmToggleButtonSetState(detailsToggle, True, True);
ShowStatus("Log");
}
/* ---- the detail block at the bottom ------------------------------------- */
static Widget
DetailValue(Widget parent, const char *name)
{
XmString s = XmStringCreateLocalized((char *) "");
Widget v = XtVaCreateManagedWidget(name, xmLabelWidgetClass, parent,
XmNlabelString, s, XmNalignment, XmALIGNMENT_BEGINNING,
XmNwidth, 190, XmNrecomputeSize, False, NULL);
XmStringFree(s);
return v;
}
static Widget
DetailRow(Widget parent, const char *caption, Widget *value)
{
Widget row = XtVaCreateManagedWidget("detailRow",
xmRowColumnWidgetClass, parent, XmNorientation, XmHORIZONTAL,
XmNspacing, 6, XmNmarginWidth, 0, NULL);
Widget l;
XmString s = XmStringCreateLocalized((char *) caption);
l = XtVaCreateManagedWidget("detailCaption", xmLabelWidgetClass, row,
XmNlabelString, s, XmNalignment, XmALIGNMENT_END,
XmNwidth, 100, XmNrecomputeSize, False, NULL);
XmStringFree(s);
*value = DetailValue(row, "detailValue");
(void) l;
return row;
}
/* the reference's two columns under the list: where it is, what it is, how
* full it is */
static Widget
MakeDetail(Widget form)
{
Widget box = XtVaCreateManagedWidget("detail", xmFormWidgetClass,
form, XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, statusLine, XmNbottomOffset, 4,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
XmATTACH_FORM, XmNheight, 84, NULL);
Widget left = XtVaCreateManagedWidget("detailLeft",
xmRowColumnWidgetClass, box, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, XmNspacing, 2,
XmNmarginWidth, 0, NULL);
Widget right = XtVaCreateManagedWidget("detailRight",
xmRowColumnWidgetClass, box, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, left,
XmNleftOffset, 24, XmNspacing, 2, XmNmarginWidth, 0, NULL);
(void) DetailRow(left, "Device:", &detDevice);
(void) DetailRow(left, "Mount Point:", &detMount);
(void) DetailRow(left, "Format:", &detFormat);
(void) DetailRow(right, "Capacity:", &detCapacity);
(void) DetailRow(right, "Available:", &detAvailable);
(void) DetailRow(right, "Used:", &detUsed);
return box;
}
/* ---- the pages ----------------------------------------------------------- */
static void
ShowPage(Widget w, XtPointer data, XtPointer call)
{
Widget pages[4];
int which = (int) (intptr_t) data, i;
(void) w;
if (call != NULL && !((XmToggleButtonCallbackStruct *) call)->set)
return;
pages[0] = pageAid;
pages[1] = pageErase;
pages[2] = pageRaid;
pages[3] = pageRestore;
for (i = 0; i < 4; i++)
if (i == which)
XtManageChild(pages[i]);
else
XtUnmanageChild(pages[i]);
}
/* "Show details": the report takes the space or leaves it */
static void
DetailsCB(Widget w, XtPointer data, XtPointer call)
{
Boolean on = XmToggleButtonGetState(w);
Widget sw = XtParent(report);
(void) data;
(void) call;
if (on) {
XtManageChild(sw);
XtVaSetValues(sw, XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, XtParent(w), XmNleftAttachment,
XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, NULL);
} else
XtUnmanageChild(sw);
}
static Widget
MakeAid(Widget parent)
{
Widget page = XtVaCreateWidget("firstAid", xmFormWidgetClass, parent,
NULL);
Widget row;
Arg a[8];
int n = 0;
row = XtVaCreateManagedWidget("buttons", xmRowColumnWidgetClass, page,
XmNorientation, XmHORIZONTAL, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, NULL);
(void) Button(row, "Verify Disk", VerifyDisk, NULL);
(void) Button(row, "Verify Permissions", Permissions, (XtPointer) 0);
XtSetSensitive(Button(row, "Repair Permissions", Permissions,
(XtPointer) 1), isRoot);
XtSetArg(a[n], XmNeditable, False); n++;
XtSetArg(a[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
XtSetArg(a[n], XmNcursorPositionVisible, False); n++;
XtSetArg(a[n], XmNrows, 12); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, row); n++;
report = XmCreateScrolledText(page, "report", a, n);
XtVaSetValues(XtParent(report), XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNbottomAttachment,
XmATTACH_FORM, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, row,
NULL);
XtManageChild(report);
detailsToggle = XtVaCreateManagedWidget("Show details",
xmToggleButtonWidgetClass, row, XmNset, True, NULL);
XtAddCallback(detailsToggle, XmNvalueChangedCallback, DetailsCB, NULL);
XmTextSetString(report, "Verify Disk looks at the chosen partition "
"and changes nothing.\nVerify Permissions compares the system's "
"files with the list the system keeps\nof how they should be; "
"Repair Permissions sets them back to it.");
return page;
}
static Widget
MakeErase(Widget parent)
{
static const char *const fsLabels[3] = {
"NetBSD (FFS)", "MS-DOS (FAT32)", "Linux (ext4)"
};
Widget page = XtVaCreateWidget("erase", xmRowColumnWidgetClass, parent,
XmNspacing, 8, NULL);
Widget row, pulldown;
XmString s;
Arg a[2];
int i;
row = XtVaCreateManagedWidget("name", xmRowColumnWidgetClass, page,
XmNorientation, XmHORIZONTAL, NULL);
(void) Text(row, "label", "Name:");
eraseName = XtVaCreateManagedWidget("eraseName",
xmTextFieldWidgetClass, row, XmNcolumns, 24, XmNvalue, "Untitled",
NULL);
pulldown = XmCreatePulldownMenu(page, "formats", NULL, 0);
for (i = 0; i < 3; i++) {
s = XmStringCreateLocalized((char *) fsLabels[i]);
fsButtons[i] = XtVaCreateManagedWidget("format",
xmPushButtonWidgetClass, pulldown, XmNlabelString, s, NULL);
XmStringFree(s);
}
s = XmStringCreateLocalized("Format:");
XtSetArg(a[0], XmNsubMenuId, pulldown);
XtSetArg(a[1], XmNlabelString, s);
eraseFormat = XmCreateOptionMenu(page, "format", a, 2);
XmStringFree(s);
XtManageChild(eraseFormat);
eraseNote = Text(page, "note", "");
eraseButton = Button(page, "Erase...", EraseAsk, NULL);
return page;
}
/* the two pages whose work is not built yet: they say what is missing */
static Widget
MakeNotice(Widget parent, const char *name, const char *text)
{
Widget page = XtVaCreateWidget(name, xmRowColumnWidgetClass, parent,
XmNspacing, 8, NULL);
(void) Text(page, "notice", text);
return page;
}
/* ---- the window ---------------------------------------------------------- */
int
main(int argc, char **argv)
{
Widget form, toolbar, left, right, tabs, sep, frame;
Arg a[10];
int n;
XtSetLanguageProc(NULL, NULL, NULL);
shell = XtVaAppInitialize(&app, "Dtdiskutility", NULL, 0, &argc, argv,
(String *) fallback, XmNtitle, "Disk Utility", NULL);
(void) DtInitialize(XtDisplay(shell), shell, argv[0], "Dtdiskutility");
isRoot = geteuid() == 0;
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
XmNwidth, 760, XmNheight, 520, NULL);
toolbar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass,
form, XmNorientation, XmHORIZONTAL, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
NULL);
mountButton = Button(toolbar, "Mount", MountCB, NULL);
unmountButton = Button(toolbar, "Unmount", UnmountCB, NULL);
ejectButton = Button(toolbar, "Eject", EjectCB, NULL);
verifyTool = Button(toolbar, "Verify", VerifyDisk, NULL);
infoTool = Button(toolbar, "Info", InfoCB, NULL);
logTool = Button(toolbar, "Log", LogCB, NULL);
(void) Button(toolbar, "Refresh", RefreshCB, NULL);
if (!isRoot)
(void) Button(toolbar, "Unlock...", Unlock, NULL);
statusLine = XtVaCreateManagedWidget("status", xmLabelWidgetClass, form,
XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, XmNalignment,
XmALIGNMENT_BEGINNING, NULL);
SetText(statusLine, isRoot ? "" : "Showing and verifying only: "
"Unlock... to change disks.");
detail = MakeDetail(form);
n = 0;
XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++;
XtSetArg(a[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNtopWidget, toolbar); n++;
XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(a[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
XtSetArg(a[n], XmNbottomWidget, detail); n++;
diskList = XmCreateScrolledList(form, "disks", a, n);
left = XtParent(diskList);
XtVaSetValues(left, XmNwidth, 260, NULL);
XtManageChild(diskList);
XtAddCallback(diskList, XmNbrowseSelectionCallback, Chosen, NULL);
right = XtVaCreateManagedWidget("right", xmFormWidgetClass, form,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, toolbar,
XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, left,
XmNleftOffset, 10, XmNrightAttachment, XmATTACH_FORM,
XmNrightOffset, 6, XmNbottomAttachment, XmATTACH_WIDGET,
XmNbottomWidget, detail, NULL);
title = XtVaCreateManagedWidget("title", xmLabelWidgetClass, right,
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
info = XtVaCreateManagedWidget("info", xmLabelWidgetClass, right,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, title,
XmNtopOffset, 6, XmNleftAttachment, XmATTACH_FORM,
XmNalignment, XmALIGNMENT_BEGINNING, NULL);
frame = XtVaCreateManagedWidget("barFrame", xmFrameWidgetClass, right,
XmNshadowType, XmSHADOW_IN, XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, info, XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM,
XmNheight, 18, NULL);
bar = XtVaCreateManagedWidget("bar", xmDrawingAreaWidgetClass, frame,
XmNheight, 14, XmNresizePolicy, XmRESIZE_NONE, NULL);
XtAddCallback(bar, XmNexposeCallback, BarExpose, NULL);
sep = XtVaCreateManagedWidget("sep", xmSeparatorWidgetClass, right,
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, frame,
XmNtopOffset, 10, XmNleftAttachment, XmATTACH_FORM,
XmNrightAttachment, XmATTACH_FORM, NULL);
tabs = XtVaCreateManagedWidget("tabs", xmRowColumnWidgetClass, right,
XmNorientation, XmHORIZONTAL, XmNradioBehavior, True,
XmNspacing, 0, XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, sep,
XmNleftAttachment, XmATTACH_FORM, NULL);
tabAid = XtVaCreateManagedWidget("First Aid", xmToggleButtonWidgetClass,
tabs, XmNindicatorOn, XmINDICATOR_NONE, XmNshadowThickness, 2,
XmNset, True, NULL);
tabErase = XtVaCreateManagedWidget("Erase", xmToggleButtonWidgetClass,
tabs, XmNindicatorOn, XmINDICATOR_NONE, XmNshadowThickness, 2,
NULL);
tabRaid = XtVaCreateManagedWidget("RAID", xmToggleButtonWidgetClass,
tabs, XmNindicatorOn, XmINDICATOR_NONE, XmNshadowThickness, 2,
NULL);
tabRestore = XtVaCreateManagedWidget("Restore",
xmToggleButtonWidgetClass, tabs, XmNindicatorOn,
XmINDICATOR_NONE, XmNshadowThickness, 2, NULL);
XtAddCallback(tabAid, XmNvalueChangedCallback, ShowPage, (XtPointer) 0);
XtAddCallback(tabErase, XmNvalueChangedCallback, ShowPage,
(XtPointer) 1);
XtAddCallback(tabRaid, XmNvalueChangedCallback, ShowPage,
(XtPointer) 2);
XtAddCallback(tabRestore, XmNvalueChangedCallback, ShowPage,
(XtPointer) 3);
{
Widget pages = XtVaCreateManagedWidget("pages",
xmFormWidgetClass, right, XmNtopAttachment,
XmATTACH_WIDGET, XmNtopWidget, tabs, XmNtopOffset, 6,
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM, NULL);
pageAid = MakeAid(pages);
pageErase = MakeErase(pages);
pageRaid = MakeNotice(pages, "raid", "A mirrored or striped "
"set is made with RAIDframe or ccd(4);\ndtdiskutility does "
"not build one yet. The set is configured\nwith raidctl(8), "
"which needs the administrator.");
pageRestore = MakeNotice(pages, "restore", "Restoring a volume "
"from another volume or an image is not\nbuilt yet: it would "
"copy with dump/restore or dd through\nMcdeSys, with a "
"progress window.");
XtVaSetValues(pageAid, XmNtopAttachment, XmATTACH_FORM,
XmNbottomAttachment, XmATTACH_FORM, XmNleftAttachment,
XmATTACH_FORM, XmNrightAttachment, XmATTACH_FORM, NULL);
XtVaSetValues(pageErase, XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM, NULL);
XtManageChild(pageAid);
}
{
/* the title larger, as the panes of System Settings */
XmRendition r;
XmRenderTable rt;
Arg ra[4];
int rn = 0;
XtSetArg(ra[rn], XmNfontType, XmFONT_IS_XFT); rn++;
XtSetArg(ra[rn], XmNfontName, "Hack"); rn++;
XtSetArg(ra[rn], XmNfontSize, 14); rn++;
XtSetArg(ra[rn], XmNfontStyle, "Bold"); rn++;
r = XmRenditionCreate(title, XmFONTLIST_DEFAULT_TAG, ra, rn);
rt = XmRenderTableAddRenditions(NULL, &r, 1, XmMERGE_REPLACE);
XmRenditionFree(r);
XtVaSetValues(title, XmNrenderTable, rt, NULL);
XmRenderTableFree(rt);
}
XtRealizeWidget(shell);
Reload();
XtAppMainLoop(app);
return 0;
}