home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
PMFLOPPY.ZIP
/
PMFLOPPY.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-04-24
|
12KB
|
394 lines
// pmfloppy.c
// PM based disk utility. Use separate threads for disk actions to prevent slowing
// up PM too much.
#define INCL_PM
#define INCL_BASE
#define INCL_DOSERRORS
#define INCL_DOSPROCESS
#define INCL_DOSDEVIOCTL
#define INCL_DOSSESMGR
#define INCL_GPILCIDS
#include <os2.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "pmfloppy.h"
// prototypes for current file
MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM);
MRESULT Panic(PCH, USHORT);
MRESULT DisplayStatus(HWND, MPARAM, MPARAM, PSZ);
MRESULT DisplayDone(HWND , MPARAM, PSZ);
// prototypes from copydlgs.c
extern MRESULT EXPENTRY ReadDlgProc(HWND, USHORT, MPARAM, MPARAM);
extern MRESULT EXPENTRY WriteDlgProc(HWND, USHORT, MPARAM, MPARAM);
extern MRESULT EXPENTRY FmtDlgProc(HWND, USHORT, MPARAM, MPARAM);
extern MRESULT EXPENTRY AboutDlgProc(HWND, USHORT, MPARAM, MPARAM);
extern MRESULT EXPENTRY QuitDlgProc(HWND, USHORT, MPARAM, MPARAM);
// prototypes from dskcpy.c
extern VOID FAR readsource(USHORT);
extern VOID FAR writetarget(USHORT);
extern VOID FAR fmtdisk(USHORT);
/* GLOBAL VARIABLES */
// PM vbls
HAB hab;
HMQ hmq;
HWND hWndFrame ;
static CHAR szClientClass[]="PMFloppy";
ULONG ctldata = FCF_STANDARD & ~FCF_TASKLIST;
HWND hWndClient;
HWND hwndDeskTop;
QMSG qmsg;
LONG CharHeight;
LONG CharWidth;
// User response vbls
USHORT DriveActive; // Bit map indicating active drives
USHORT FormatOptions; // Bit map indicating formatting choice
USHORT usReadDrive; // Drive to read from (from dialog box)
CHAR szReadDrive[] = "A:"; // Drive to read from (from dialog box)
USHORT usWriteDrive; // Drive to write to (from dialog box)
CHAR szWriteDrive[] = "A:"; // Drive to write to (from dialog box)
USHORT usFmtDrive; // Drive to format (from dialog box)
CHAR szFmtDrive[] = "A:"; // Drive to format (from dialog box)
CHAR Volume[11]; // Volume Name
// Thread vbls
TID CopyID; // Thread ID for the subprocess
SEL rgselStack;
// Disk handling vbls
int gotSource = FALSE; /* Bool: Source disk has been read */
int cdecl main(void)
{
hab = WinInitialize(0);
hmq = WinCreateMsgQueue(hab, 0);
hwndDeskTop = WinQueryDesktopWindow(hab, NULL);
if (!WinRegisterClass(hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0))
return(0);
hWndFrame = WinCreateStdWindow(HWND_DESKTOP
, WS_VISIBLE
, &ctldata
, szClientClass
, "Disk Utility"
, 0L
, NULL
, IDR_PMFLOPPY
, &hWndClient);
if (hWndFrame != NULL)
{
while ( WinGetMsg(hab, &qmsg, NULL, 0, 0) )
WinDispatchMsg(hab, &qmsg);
WinDestroyWindow(hWndFrame);
}
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
DosExit(EXIT_PROCESS, 0);
} /* main */
// ClientWndProc - main window processing
//
// note: UM messages are User-defined (in pmfloppy.h).
// all UM messages use mp2 to indicate the drive the message concerns,
// and mp1 is data specific to that message. Then UM_xxxMSG is a
// generic msg. It is currently only used for DONE messages. If I
// need more, set up some magic numbers and pass them through mp1.
//
MRESULT EXPENTRY ClientWndProc(HWND hwnd
, USHORT id
, MPARAM mp1
, MPARAM mp2)
{
HPS hPS;
HWND hMenu;
CHAR szTxtBuf[40];
USHORT usDrive;
int far *stkptr;
RECTL rctStart;
FONTMETRICS fm;
switch (id)
{
case WM_CREATE:
DriveActive = 0;
hPS = WinGetPS(hwnd);
GpiQueryFontMetrics(hPS, (long)sizeof(fm), &fm);
CharHeight = fm.lMaxBaselineExt;
CharWidth = fm.lMaxCharInc;
WinReleasePS(hPS);
break;
case WM_PAINT:
hPS = WinGetPS(hwnd);
GpiErase(hPS);
WinReleasePS(hPS);
WinQueryUpdateRect(hwnd, &rctStart);
WinValidateRect(hwnd, &rctStart, FALSE);
break;
case WM_COMMAND:
switch (COMMANDMSG(&id)->cmd)
{
case IDM_READ:
if (WinDlgBox(HWND_DESKTOP,hWndFrame,ReadDlgProc,NULL,READ_DLG,NULL))
{
// send off thread to read
DosAllocSeg(STACK_SIZE, &rgselStack, 0);
stkptr = (int far *)MAKEP(rgselStack, STACK_SIZE);
*(--stkptr) = (int) szReadDrive[0];
if (DosCreateThread(readsource, &(CopyID), (PBYTE)stkptr) )
{
WinMessageBox(HWND_DESKTOP, hWndFrame, "Creating a thread failed."
, "Drive Read"
, 0, MB_OK | MB_ICONEXCLAMATION);
DosFreeSeg(rgselStack);
return FALSE;
} /* if can't create one thread. */
DriveActive |= usReadDrive;
}
break;
case IDM_WRITE:
// put up dialog, return drive indicators, format type
if (WinDlgBox(HWND_DESKTOP,hWndFrame,WriteDlgProc,NULL,WRITE_DLG,NULL))
{
// send off thread to write
DosAllocSeg(STACK_SIZE, &rgselStack, 0);
stkptr = (int far *)MAKEP(rgselStack, STACK_SIZE);
*(--stkptr) = (int) szWriteDrive[0];
if (DosCreateThread(writetarget, &(CopyID), (PBYTE)stkptr) )
{
WinMessageBox(HWND_DESKTOP, hWndFrame, "Creating a thread failed."
, "Drive Write"
, 0, MB_OK | MB_ICONEXCLAMATION);
DosFreeSeg(rgselStack);
return FALSE;
} /* if can't create one thread. */
DriveActive |= usWriteDrive;
}
break;
case IDM_FORMAT:
// put up dialog, return drive indicators, format type
if (WinDlgBox(HWND_DESKTOP,hWndFrame,FmtDlgProc,NULL,FORMAT_DLG,NULL))
{
// send off thread to format
DosAllocSeg(STACK_SIZE, &rgselStack, 0);
stkptr = (int far *)MAKEP(rgselStack, STACK_SIZE);
*(--stkptr) = (int) szFmtDrive[0];
if (DosCreateThread(fmtdisk, &(CopyID), (PBYTE)stkptr) )
{
WinMessageBox(HWND_DESKTOP, hWndFrame, "Creating a thread failed."
, "Drive Format"
, 0, MB_OK | MB_ICONEXCLAMATION);
DosFreeSeg(rgselStack);
return FALSE;
} /* if can't create one thread. */
DriveActive |= usFmtDrive;
}
break;
case IDM_ABOUT:
WinDlgBox(HWND_DESKTOP,hWndFrame,AboutDlgProc,NULL,ABOUT_DLG,NULL);
break;
case IDM_EXIT:
WinPostMsg(hWndFrame, WM_CLOSE, NULL, NULL);
break;
}
break;
case WM_INITMENU:
//set the allowable menu choices.
hMenu = WinWindowFromID(hWndFrame,FID_MENU);
// If we are reading, disable the read option
WinSendMsg(hMenu,MM_SETITEMATTR,
MPFROM2SHORT(IDM_READ,TRUE),
MPFROM2SHORT(MIA_DISABLED,(DriveActive && usReadDrive) ? MIA_DISABLED : 0));
// If we don't have anything to write, disable the write menu
WinSendMsg(hMenu,MM_SETITEMATTR,
MPFROM2SHORT(IDM_WRITE,TRUE),
MPFROM2SHORT(MIA_DISABLED,gotSource ? 0 : MIA_DISABLED));
// until format is working, leave it disabled.
WinSendMsg(hMenu,MM_SETITEMATTR,
MPFROM2SHORT(IDM_FORMAT,TRUE),
MPFROM2SHORT(MIA_DISABLED,MIA_DISABLED));
break;
case UM_READSTATUS:
DisplayStatus(hwnd,mp1,mp2,"read");
break;
case UM_READMSG:
DisplayDone(hwnd, mp2, "Read");
break;
case UM_READERROR:
usDrive = SHORT1FROMMP(mp2);
sprintf(szTxtBuf,"Read Error on drive %c", (CHAR)usDrive);
DriveActive &= ~((usDrive - (USHORT)'A') + 1);
Panic(szTxtBuf,SHORT1FROMMP(mp1));
break;
case UM_WRITESTATUS:
DisplayStatus(hwnd,mp1,mp2,"written");
break;
case UM_WRITEMSG:
DisplayDone(hwnd, mp2, "Write");
break;
case UM_WRITEERROR:
usDrive = SHORT1FROMMP(mp2);
sprintf(szTxtBuf,"Write Error on drive %c", (CHAR)usDrive);
DriveActive &= ~((usDrive - (USHORT)'A') + 1);
Panic(szTxtBuf,SHORT1FROMMP(mp1));
break;
case UM_FMTSTATUS:
DisplayStatus(hwnd,mp1,mp2,"formatted");
break;
case UM_FMTMSG:
DisplayDone(hwnd, mp2, "Format");
break;
case UM_FMTERROR:
usDrive = SHORT1FROMMP(mp2);
sprintf(szTxtBuf,"Format Error on drive %c", (CHAR)usDrive);
DriveActive &= ~((usDrive - (USHORT)'A') + 1);
Panic(szTxtBuf,SHORT1FROMMP(mp1));
break;
case WM_CLOSE:
if (DriveActive)
{
if (!WinDlgBox(HWND_DESKTOP,hWndFrame,QuitDlgProc,NULL,QUIT_DLG,NULL))
break;
}
// else just fall through to default to get to the default proc
default:
return(WinDefWindowProc(hwnd, id, mp1, mp2));
} /* switch id */
return 0L;
} /* clientwndproc */
// Panic -- Put up a message box with an error message.
//
// Inputs: pszCaption -- Caption text for message box
//
// Returns: 1L, for error signalling from window procedures.
MRESULT Panic(PCH pszCaption,USHORT ErrorNum)
{
HWND hwndCurFocus;
CHAR buf[1024];
USHORT cbBuf;
if (ErrorNum == DSKCPY_ERROR_WRONG_FORMAT)
sprintf(buf, "Error - target disk incorrect format");
else if (DosGetMessage(NULL, 0, buf, 1024, ErrorNum, "oso001.msg", &cbBuf))
{
sprintf(buf, "SYS%04d: error text unavailable", ErrorNum);
cbBuf = 31;
}
buf[cbBuf] = (char)0;
// if (hf) DosClose(hf);
hwndCurFocus = WinQueryFocus(HWND_DESKTOP, FALSE);
WinAlarm(HWND_DESKTOP, WA_NOTE);
WinMessageBox(HWND_DESKTOP, hWndFrame, buf, pszCaption,
0, MB_OK | MB_ICONEXCLAMATION);
return(1);
} // panic
// DisplayStatus - Display the status for the drive in the PS
//
// in: mp1,mp2 message parameters from the wndproc
// hwnd window handle to display on
// op operation (eg read, write)
//
MRESULT DisplayStatus(HWND hwnd, MPARAM mp1, MPARAM mp2, PSZ op)
{
USHORT usPos;
USHORT usTotal;
USHORT usDrive;
RECTL rctStart;
CHAR szPct[3];
HPS hPS;
CHAR szTxtBuf[40];
usPos = SHORT1FROMMP(mp1);
usTotal = SHORT2FROMMP(mp1);
usDrive = SHORT1FROMMP(mp2);
rctStart.xLeft = 5L;
rctStart.xRight = 5L + (CharWidth * 40);
rctStart.yBottom = ((usDrive - (USHORT)'A') * CharHeight) + 5;
rctStart.yTop = rctStart.yBottom + CharHeight;
itoa((USHORT)(((float)usPos/(float)usTotal)*100.0),szPct,10);
hPS = WinGetPS(hwnd);
sprintf(szTxtBuf,"%s%% of disk %s (Drive %c)",szPct, op, (CHAR)usDrive);
WinDrawText(hPS, -1, szTxtBuf, &rctStart, CLR_NEUTRAL, CLR_BACKGROUND,
DT_LEFT | DT_ERASERECT);
WinReleasePS(hPS);
return 0;
}
// DisplayDone - Display an operation complete
//
// in: mp2 message parameters from the wndproc
// hwnd window handle to display on
// op operation (eg read, write)
//
MRESULT DisplayDone(HWND hwnd, MPARAM mp2, PSZ op)
{
USHORT usDrive;
RECTL rctStart;
HPS hPS;
CHAR szTxtBuf[40];
usDrive = SHORT1FROMMP(mp2);
rctStart.xLeft = 5L;
rctStart.xRight = 5L + (CharWidth * 40);
rctStart.yBottom = ((usDrive - (USHORT)'A') * CharHeight) + 5;
rctStart.yTop = rctStart.yBottom + CharHeight;
WinAlarm(HWND_DESKTOP, WA_NOTE);
hPS = WinGetPS(hwnd);
sprintf(szTxtBuf,"%s complete (Drive %c)", op, (CHAR)usDrive);
WinDrawText(hPS, -1, szTxtBuf, &rctStart, CLR_NEUTRAL, CLR_BACKGROUND,
DT_LEFT | DT_ERASERECT);
WinReleasePS(hPS);
DriveActive &= ~((usDrive - (USHORT)'A') + 1);
return 0;
}