| 1 |  | 
|---|
| 2 | /*********************************************************************** | 
|---|
| 3 |  | 
|---|
| 4 | $Id: dirsize.c 1341 2008-12-14 22:18:26Z stevenhl $ | 
|---|
| 5 |  | 
|---|
| 6 | Directory sizes applet | 
|---|
| 7 |  | 
|---|
| 8 | Copyright (c) 1993-98 M. Kimes | 
|---|
| 9 | Copyright (c) 2004, 2008 Steven H.Levine | 
|---|
| 10 |  | 
|---|
| 11 | 11 Jun 02 SHL Baseline | 
|---|
| 12 | 06 Jan 04 SHL Total drives >4GB better | 
|---|
| 13 | 08 Jul 08 SHL Avoid WARNALL warning | 
|---|
| 14 | 14 Dec 08 SHL Add exception handler support | 
|---|
| 15 | 14 Dec 08 SHL Drop NEVER used code | 
|---|
| 16 |  | 
|---|
| 17 | ***********************************************************************/ | 
|---|
| 18 |  | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <ctype.h>                      // toupper | 
|---|
| 21 |  | 
|---|
| 22 | #define INCL_DOS | 
|---|
| 23 | #define INCL_WIN | 
|---|
| 24 | #define INCL_DOSEXCEPTIONS              // XCTP_... | 
|---|
| 25 | #define INCL_DOSERRORS                  // NO_ERROR | 
|---|
| 26 |  | 
|---|
| 27 | #include "dll\fm3dll.h" | 
|---|
| 28 | #include "dll\fm3dll2.h"                // #define's for UM_*, control id's, etc. | 
|---|
| 29 | #include "dll\info.h"                   // driveflags | 
|---|
| 30 | #include "dll\mainwnd.h"                // FM3ModHandle | 
|---|
| 31 | #include "dll\fm3dlg.h" | 
|---|
| 32 | #include "dll\valid.h"                  // CheckDrive | 
|---|
| 33 | #include "dll\init.h"                   // InitFM3DLL | 
|---|
| 34 | #include "dll\dirsize.h"                // DirSizeProc | 
|---|
| 35 | #include "dll\errutil.h"                // Error reporting | 
|---|
| 36 | #include "dll\excputil.h"               // Exception handlers | 
|---|
| 37 |  | 
|---|
| 38 | static PSZ pszSrcFile = __FILE__; | 
|---|
| 39 |  | 
|---|
| 40 | MRESULT EXPENTRY DirMainProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) | 
|---|
| 41 | { | 
|---|
| 42 | static CHAR curdir[4]; | 
|---|
| 43 |  | 
|---|
| 44 | switch (msg) { | 
|---|
| 45 | case WM_INITDLG: | 
|---|
| 46 | *curdir = 0; | 
|---|
| 47 | WinSendMsg(hwnd, UM_UNDO, MPVOID, MPVOID); | 
|---|
| 48 | break; | 
|---|
| 49 |  | 
|---|
| 50 | case UM_UNDO: | 
|---|
| 51 | { | 
|---|
| 52 | ULONG x; | 
|---|
| 53 | ULONG ulDriveMap; | 
|---|
| 54 | ULONG ulDriveNum; | 
|---|
| 55 | CHAR dirname[] = " :\\"; | 
|---|
| 56 | BOOL first = TRUE; | 
|---|
| 57 |  | 
|---|
| 58 | WinSendDlgItemMsg(hwnd, DIRSIZE_LISTBOX, LM_DELETEALL, MPVOID, MPVOID); | 
|---|
| 59 |  | 
|---|
| 60 | DosError(FERR_DISABLEHARDERR); | 
|---|
| 61 | DosQCurDisk(&ulDriveNum, &ulDriveMap); | 
|---|
| 62 |  | 
|---|
| 63 | for (x = 2; x < 26; x++) { | 
|---|
| 64 | if ((ulDriveMap & (1L << x)) && !(driveflags[x] & DRIVE_IGNORE)) { | 
|---|
| 65 | *dirname = (CHAR) x + 'A'; | 
|---|
| 66 | WinSendDlgItemMsg(hwnd, DIRSIZE_LISTBOX, LM_INSERTITEM, | 
|---|
| 67 | MPFROM2SHORT(LIT_END, 0), MPFROMP(dirname)); | 
|---|
| 68 | if (first) { | 
|---|
| 69 | WinSendDlgItemMsg(hwnd, DIRSIZE_LISTBOX, LM_SELECTITEM, | 
|---|
| 70 | MPFROMSHORT(0), MPFROMSHORT(TRUE)); | 
|---|
| 71 | first = FALSE; | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 | return 0; | 
|---|
| 77 |  | 
|---|
| 78 | case UM_RESCAN: | 
|---|
| 79 | { | 
|---|
| 80 | CHAR FileSystem[CCHMAXPATH]; | 
|---|
| 81 | CHAR s[CCHMAXPATH * 2]; | 
|---|
| 82 | FSALLOCATE fsa; | 
|---|
| 83 | ULONG type; | 
|---|
| 84 | USHORT percentused; | 
|---|
| 85 | USHORT percentfree; | 
|---|
| 86 | struct | 
|---|
| 87 | { | 
|---|
| 88 | ULONG serial; | 
|---|
| 89 | CHAR volumelength; | 
|---|
| 90 | CHAR volumelabel[CCHMAXPATH]; | 
|---|
| 91 | } | 
|---|
| 92 | volser; | 
|---|
| 93 | INT removable; | 
|---|
| 94 |  | 
|---|
| 95 | WinSetDlgItemText(hwnd, DIRSIZE_LABEL, ""); | 
|---|
| 96 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSFREE, ""); | 
|---|
| 97 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSUSED, ""); | 
|---|
| 98 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSIZE, ""); | 
|---|
| 99 | WinSetDlgItemText(hwnd, DIRSIZE_PERCENT, ""); | 
|---|
| 100 | WinSetDlgItemText(hwnd, DIRSIZE_BYTESUSED, ""); | 
|---|
| 101 | WinSetDlgItemText(hwnd, DIRSIZE_BYTESFREE, ""); | 
|---|
| 102 | WinSetDlgItemText(hwnd, DIRSIZE_IFS, ""); | 
|---|
| 103 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, ""); | 
|---|
| 104 | WinSendDlgItemMsg(hwnd, DIRSIZE_SLIDER, SLM_SETSLIDERINFO, | 
|---|
| 105 | MPFROM2SHORT(SMA_SLIDERARMPOSITION, | 
|---|
| 106 | SMA_INCREMENTVALUE), MPFROMSHORT(0)); | 
|---|
| 107 | removable = CheckDrive(toupper(*curdir), FileSystem, &type); | 
|---|
| 108 | if (removable != -1) { | 
|---|
| 109 | if (type & DRIVE_ZIPSTREAM) | 
|---|
| 110 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, "Zipstream drive"); | 
|---|
| 111 | else if (type & DRIVE_REMOTE) | 
|---|
| 112 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, "Remote drive"); | 
|---|
| 113 | else if (type & DRIVE_VIRTUAL) | 
|---|
| 114 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, "Virtual drive"); | 
|---|
| 115 | else if (type & DRIVE_RAMDISK) | 
|---|
| 116 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, "Ramdisk"); | 
|---|
| 117 | else { | 
|---|
| 118 | sprintf(s, "Local drive%s", (removable) ? " (removable)" : ""); | 
|---|
| 119 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, s); | 
|---|
| 120 | } | 
|---|
| 121 | sprintf(s, "IFS:  %s", FileSystem); | 
|---|
| 122 | WinSetDlgItemText(hwnd, DIRSIZE_IFS, s); | 
|---|
| 123 | memset(&volser, 0, sizeof(volser)); | 
|---|
| 124 | DosError(FERR_DISABLEHARDERR); | 
|---|
| 125 | if (!DosQueryFSInfo(toupper(*curdir) - '@', FSIL_VOLSER, | 
|---|
| 126 | &volser, (ULONG) sizeof(volser))) { | 
|---|
| 127 | sprintf(s, "Label:  %s", volser.volumelabel); | 
|---|
| 128 | WinSetDlgItemText(hwnd, DIRSIZE_LABEL, s); | 
|---|
| 129 | } | 
|---|
| 130 | if (!DosQueryFSInfo(toupper(*curdir) - '@', | 
|---|
| 131 | FSIL_ALLOC, &fsa, sizeof(FSALLOCATE))) { | 
|---|
| 132 | percentfree = fsa.cUnit ? (USHORT)((fsa.cUnitAvail * 100) / fsa.cUnit) : 0;   // 27 May 08 SHL | 
|---|
| 133 | if (!percentfree && fsa.cUnitAvail) | 
|---|
| 134 | percentfree = 1; | 
|---|
| 135 | percentused = 100 - percentfree; | 
|---|
| 136 | sprintf(s, "Units free:  %lu", fsa.cUnitAvail); | 
|---|
| 137 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSFREE, s); | 
|---|
| 138 | sprintf(s, "Unit size:  %lu x %u = %lu", | 
|---|
| 139 | fsa.cSectorUnit, | 
|---|
| 140 | fsa.cbSector, fsa.cSectorUnit * fsa.cbSector); | 
|---|
| 141 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSIZE, s); | 
|---|
| 142 | sprintf(s, "Units used:  %lu", fsa.cUnit - fsa.cUnitAvail); | 
|---|
| 143 | WinSetDlgItemText(hwnd, DIRSIZE_UNITSUSED, s); | 
|---|
| 144 | sprintf(s, "Bytes free:  %.0f", | 
|---|
| 145 | (float)fsa.cUnitAvail * (fsa.cSectorUnit * fsa.cbSector)); | 
|---|
| 146 | WinSetDlgItemText(hwnd, DIRSIZE_BYTESFREE, s); | 
|---|
| 147 | sprintf(s, "Bytes used:  %.0f", | 
|---|
| 148 | (float)(fsa.cUnit - fsa.cUnitAvail) * | 
|---|
| 149 | (fsa.cSectorUnit * fsa.cbSector)); | 
|---|
| 150 | WinSetDlgItemText(hwnd, DIRSIZE_BYTESUSED, s); | 
|---|
| 151 | sprintf(s, "Percent used:  %u%%", percentused); | 
|---|
| 152 | WinSetDlgItemText(hwnd, DIRSIZE_PERCENT, s); | 
|---|
| 153 | WinSendDlgItemMsg(hwnd, DIRSIZE_SLIDER, SLM_SETSLIDERINFO, | 
|---|
| 154 | MPFROM2SHORT(SMA_SLIDERARMPOSITION, | 
|---|
| 155 | SMA_INCREMENTVALUE), | 
|---|
| 156 | MPFROMSHORT(percentused)); | 
|---|
| 157 | WinShowWindow(WinWindowFromID(hwnd, DIRSIZE_SLIDER), TRUE); | 
|---|
| 158 | } | 
|---|
| 159 | } | 
|---|
| 160 | else { | 
|---|
| 161 | WinSetDlgItemText(hwnd, DIRSIZE_LOCAL, "Drive not ready."); | 
|---|
| 162 | WinShowWindow(WinWindowFromID(hwnd, DIRSIZE_SLIDER), FALSE); | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 | return 0; | 
|---|
| 166 |  | 
|---|
| 167 | case WM_CONTROL: | 
|---|
| 168 | switch (SHORT1FROMMP(mp1)) { | 
|---|
| 169 | case DIRSIZE_LISTBOX: | 
|---|
| 170 | switch (SHORT2FROMMP(mp1)) { | 
|---|
| 171 | case LN_ENTER: | 
|---|
| 172 | WinPostMsg(hwnd, WM_COMMAND, MPFROM2SHORT(DID_OK, 0), MPVOID); | 
|---|
| 173 | break; | 
|---|
| 174 | case LN_SELECT: | 
|---|
| 175 | { | 
|---|
| 176 | SHORT x; | 
|---|
| 177 |  | 
|---|
| 178 | x = (SHORT) WinSendDlgItemMsg(hwnd, DIRSIZE_LISTBOX, | 
|---|
| 179 | LM_QUERYSELECTION, | 
|---|
| 180 | MPFROMSHORT(LIT_FIRST), MPVOID); | 
|---|
| 181 | if (x >= 0) { | 
|---|
| 182 | WinSendDlgItemMsg(hwnd, DIRSIZE_LISTBOX, | 
|---|
| 183 | LM_QUERYITEMTEXT, | 
|---|
| 184 | MPFROM2SHORT(x, sizeof(curdir)), | 
|---|
| 185 | MPFROMP(curdir)); | 
|---|
| 186 | WinPostMsg(hwnd, UM_RESCAN, MPVOID, MPVOID); | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | break; | 
|---|
| 190 | } | 
|---|
| 191 | break; | 
|---|
| 192 | } | 
|---|
| 193 | return 0; | 
|---|
| 194 |  | 
|---|
| 195 | case WM_COMMAND: | 
|---|
| 196 | switch (SHORT1FROMMP(mp1)) { | 
|---|
| 197 | case DID_CANCEL: | 
|---|
| 198 | WinDismissDlg(hwnd, 0); | 
|---|
| 199 | break; | 
|---|
| 200 |  | 
|---|
| 201 | case DID_OK: | 
|---|
| 202 | if (*curdir) { | 
|---|
| 203 | WinSetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_HIDE); | 
|---|
| 204 | WinDlgBox(HWND_DESKTOP, hwnd, | 
|---|
| 205 | DirSizeProc, FM3ModHandle, DSZ_FRAME, curdir); | 
|---|
| 206 | WinSetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOW); | 
|---|
| 207 | } | 
|---|
| 208 | else | 
|---|
| 209 | DosBeep(50, 100); | 
|---|
| 210 | break; | 
|---|
| 211 | } | 
|---|
| 212 | return 0; | 
|---|
| 213 | } // switch | 
|---|
| 214 | return WinDefDlgProc(hwnd, msg, mp1, mp2); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | int main(int argc, char *argv[]) | 
|---|
| 218 | { | 
|---|
| 219 | HAB hab; | 
|---|
| 220 | HMQ hmq; | 
|---|
| 221 | CHAR fullname[CCHMAXPATH] = { 0 };    // 14 Dec 08 SHL was static | 
|---|
| 222 | UINT x; | 
|---|
| 223 | ULONG rcl; | 
|---|
| 224 | APIRET regRet; | 
|---|
| 225 | EXCEPTIONREGISTRATIONRECORD regRec = { NULL, NULL }; | 
|---|
| 226 |  | 
|---|
| 227 | DosError(FERR_DISABLEHARDERR); | 
|---|
| 228 |  | 
|---|
| 229 | regRec.ExceptionHandler = HandleException; | 
|---|
| 230 | regRet = DosSetExceptionHandler(®Rec); | 
|---|
| 231 | if (regRet != NO_ERROR) { | 
|---|
| 232 | DbgMsg(pszSrcFile, __LINE__, | 
|---|
| 233 | "DosSetExceptionHandler failed with error %u", regRet); | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | for (x = 1; x < argc; x++) { | 
|---|
| 237 | if (!strchr("/;,`\'", *argv[x]) && | 
|---|
| 238 | !*fullname && (IsRoot(argv[x]) || IsFile(argv[x]) == 0)) { | 
|---|
| 239 | if (IsRoot(argv[x])) | 
|---|
| 240 | strcpy(fullname, argv[x]); | 
|---|
| 241 | else if (DosQueryPathInfo(argv[x], | 
|---|
| 242 | FIL_QUERYFULLNAME, | 
|---|
| 243 | fullname, sizeof(fullname))) | 
|---|
| 244 | *fullname = 0;                  // Forget name | 
|---|
| 245 | } | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | hab = WinInitialize(0); | 
|---|
| 249 | if (hab) { | 
|---|
| 250 | hmq = WinCreateMsgQueue(hab, 384); | 
|---|
| 251 | if (hmq) { | 
|---|
| 252 | if (InitFM3DLL(hab, argc, argv)) { | 
|---|
| 253 | if (!*fullname) | 
|---|
| 254 | rcl = WinDlgBox(HWND_DESKTOP, | 
|---|
| 255 | HWND_DESKTOP, DirMainProc, 0, DIRSIZE_FRAME, NULL); | 
|---|
| 256 | else | 
|---|
| 257 | rcl = WinDlgBox(HWND_DESKTOP, | 
|---|
| 258 | HWND_DESKTOP, | 
|---|
| 259 | DirSizeProc, FM3ModHandle, DSZ_FRAME, fullname); | 
|---|
| 260 | if (rcl == DID_ERROR) | 
|---|
| 261 | rcl = WinGetLastError(hab); | 
|---|
| 262 | } | 
|---|
| 263 | WinDestroyMsgQueue(hmq); | 
|---|
| 264 | } | 
|---|
| 265 | WinTerminate(hab); | 
|---|
| 266 | } | 
|---|
| 267 | return 0; | 
|---|
| 268 |  | 
|---|
| 269 | } // main | 
|---|