| 1 |  | 
|---|
| 2 | /*********************************************************************** | 
|---|
| 3 |  | 
|---|
| 4 | $Id: rename.c 1545 2010-10-23 22:00:47Z gyoung $ | 
|---|
| 5 |  | 
|---|
| 6 | Copyright (c) 1993-98 M. Kimes | 
|---|
| 7 | Copyright (c) 2004, 2010 Steven H.Levine | 
|---|
| 8 |  | 
|---|
| 9 | Revisions | 
|---|
| 10 | 01 Aug 04 SHL - Rework lstrip/rstrip usage | 
|---|
| 11 | 22 Mar 07 GKY Use QWL_USER | 
|---|
| 12 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat | 
|---|
| 13 | 27 Sep 07 SHL Correct ULONGLONG size formatting | 
|---|
| 14 | 30 Dec 07 GKY Use TestFDates for comparing dates | 
|---|
| 15 | 25 Dec 08 GKY Add code to allow write verify to be turned off on a per drive basis | 
|---|
| 16 | 07 Feb 09 GKY Allow user to turn off alert and/or error beeps in settings notebook. | 
|---|
| 17 | 07 Feb 09 GKY Add *DateFormat functions to format dates based on locale | 
|---|
| 18 | 08 Mar 09 GKY Renamed commafmt.h i18nutil.h | 
|---|
| 19 | 08 Mar 09 GKY Removed variable aurguments from docopyf and unlinkf (not used) | 
|---|
| 20 | 17 JAN 10 GKY Changes to get working with Watcom 1.9 Beta (1/16/10). Mostly cast CHAR CONSTANT * as CHAR *. | 
|---|
| 21 | 23 Oct 10 GKY Add ForwardslashToBackslash function to streamline code | 
|---|
| 22 |  | 
|---|
| 23 | ***********************************************************************/ | 
|---|
| 24 |  | 
|---|
| 25 | #include <string.h> | 
|---|
| 26 | #include <ctype.h> | 
|---|
| 27 |  | 
|---|
| 28 | #define INCL_WIN | 
|---|
| 29 | #define INCL_LONGLONG | 
|---|
| 30 |  | 
|---|
| 31 | #include "fm3dll.h" | 
|---|
| 32 | #include "fm3dll2.h"                    // #define's for UM_*, control id's, etc. | 
|---|
| 33 | #include "worker.h"                     // typedef MOVEIT | 
|---|
| 34 | #include "rename.h" | 
|---|
| 35 | #include "notebook.h"                   // Data declaration(s) | 
|---|
| 36 | #include "init.h"                       // Data declaration(s) | 
|---|
| 37 | #include "fm3dlg.h" | 
|---|
| 38 | #include "fm3str.h" | 
|---|
| 39 | #include "errutil.h"                    // Dos_Error... | 
|---|
| 40 | #include "strutil.h"                    // GetPString | 
|---|
| 41 | #include "copyf.h"                      // AdjustWildcardName | 
|---|
| 42 | #include "valid.h"                      // TestFDates | 
|---|
| 43 | #include "mkdir.h"                      // SetDir | 
|---|
| 44 | #include "i18nutil.h"                   // CommaFmtULL | 
|---|
| 45 | #include "strips.h"                     // bstrip | 
|---|
| 46 | #include "info.h"                       // driveflags | 
|---|
| 47 | #include "wrappers.h"                   // xfopen | 
|---|
| 48 | #include "pathutil.h"                   // ForwardslashToBackslash | 
|---|
| 49 |  | 
|---|
| 50 | static PSZ pszSrcFile = __FILE__; | 
|---|
| 51 |  | 
|---|
| 52 | MRESULT EXPENTRY RenameProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) | 
|---|
| 53 | { | 
|---|
| 54 | MOVEIT *mv; | 
|---|
| 55 |  | 
|---|
| 56 | switch (msg) { | 
|---|
| 57 | case WM_INITDLG: | 
|---|
| 58 | mv = (MOVEIT *) mp2; | 
|---|
| 59 | WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mv); | 
|---|
| 60 | if (!mv || !mv->source) { | 
|---|
| 61 | WinDismissDlg(hwnd, 0); | 
|---|
| 62 | break; | 
|---|
| 63 | } | 
|---|
| 64 | WinSendDlgItemMsg(hwnd, | 
|---|
| 65 | REN_SOURCE, | 
|---|
| 66 | EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID); | 
|---|
| 67 | WinSendDlgItemMsg(hwnd, | 
|---|
| 68 | REN_TARGET, | 
|---|
| 69 | EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID); | 
|---|
| 70 | if (!*mv->target) | 
|---|
| 71 | strcpy(mv->target, mv->source); | 
|---|
| 72 | WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID); | 
|---|
| 73 | if (mv->rename || !stricmp(mv->target, mv->source)) { | 
|---|
| 74 |  | 
|---|
| 75 | CHAR *p = strrchr(mv->target, '\\'); | 
|---|
| 76 | if (p) { | 
|---|
| 77 |  | 
|---|
| 78 | USHORT sello, selhi; | 
|---|
| 79 |  | 
|---|
| 80 | sello = p - mv->target + 1; | 
|---|
| 81 | selhi = strlen(mv->target); | 
|---|
| 82 |  | 
|---|
| 83 | WinSendDlgItemMsg(hwnd, | 
|---|
| 84 | REN_TARGET, | 
|---|
| 85 | EM_SETSEL, MPFROM2SHORT(sello, selhi), MPVOID); | 
|---|
| 86 | } | 
|---|
| 87 | WinShowWindow(WinWindowFromID(hwnd, REN_OVEROLD), FALSE); | 
|---|
| 88 | WinShowWindow(WinWindowFromID(hwnd, REN_OVERNEW), FALSE); | 
|---|
| 89 | } | 
|---|
| 90 | break; | 
|---|
| 91 |  | 
|---|
| 92 | case UM_RESCAN: | 
|---|
| 93 | { | 
|---|
| 94 | mv = WinQueryWindowPtr(hwnd, QWL_USER); | 
|---|
| 95 | if (mv) { | 
|---|
| 96 |  | 
|---|
| 97 | FILESTATUS3L fs1, fs2; | 
|---|
| 98 | CHAR s[CCHMAXPATH * 2], *p, chkname[CCHMAXPATH], szCmmaFmtFileSize[81], szDate[DATE_BUF_BYTES]; | 
|---|
| 99 | INT sourceexists = 0, targetexists = 0, | 
|---|
| 100 | sourcenewer = 0, sourcesmaller = 0; | 
|---|
| 101 | ForwardslashToBackslash(mv->target); | 
|---|
| 102 | if (!MakeFullName(mv->target)) | 
|---|
| 103 | WinSetDlgItemText(hwnd, REN_TARGET, mv->target); | 
|---|
| 104 | if (!MakeFullName(mv->source)) | 
|---|
| 105 | WinSetDlgItemText(hwnd, REN_SOURCE, mv->source); | 
|---|
| 106 | if (!DosQueryPathInfo(mv->source, FIL_STANDARDL, &fs1, sizeof(fs1))) { | 
|---|
| 107 | CommaFmtULL(szCmmaFmtFileSize, | 
|---|
| 108 | sizeof(szCmmaFmtFileSize), fs1.cbFile, ' '); | 
|---|
| 109 | FDateFormat(szDate, fs1.fdateLastWrite); | 
|---|
| 110 | sprintf(s, " %s%s %ss %s %02u%s%02u%s%02u", | 
|---|
| 111 | fs1.attrFile & FILE_DIRECTORY ? | 
|---|
| 112 | GetPString(IDS_DIRBRKTTEXT) : NullStr, | 
|---|
| 113 | szCmmaFmtFileSize, | 
|---|
| 114 | GetPString(IDS_BYTETEXT), | 
|---|
| 115 | szDate, | 
|---|
| 116 | fs1.ftimeLastWrite.hours, TimeSeparator, | 
|---|
| 117 | fs1.ftimeLastWrite.minutes, TimeSeparator, fs1.ftimeLastWrite.twosecs * 2); | 
|---|
| 118 | WinSetDlgItemText(hwnd, REN_SOURCEINFO, s); | 
|---|
| 119 | sourceexists = 1; | 
|---|
| 120 | if (fs1.attrFile & FILE_DIRECTORY) | 
|---|
| 121 | sourceexists = 3; | 
|---|
| 122 | } | 
|---|
| 123 | else | 
|---|
| 124 | WinSetDlgItemText(hwnd, | 
|---|
| 125 | REN_SOURCEINFO, (CHAR *) GetPString(IDS_DOESNTEXIST2TEXT)); | 
|---|
| 126 | strcpy(chkname, mv->target); | 
|---|
| 127 | p = strrchr(s, '\\'); | 
|---|
| 128 | if (p && (strchr(p, '*') || strchr(p, '?'))) { | 
|---|
| 129 | if (!AdjustWildcardName(mv->target, chkname)) | 
|---|
| 130 | strcpy(chkname, mv->target); | 
|---|
| 131 | } | 
|---|
| 132 | if (!DosQueryPathInfo(chkname, FIL_STANDARDL, &fs2, sizeof(fs2))) { | 
|---|
| 133 | CommaFmtULL(szCmmaFmtFileSize, | 
|---|
| 134 | sizeof(szCmmaFmtFileSize), fs2.cbFile, ' '); | 
|---|
| 135 | FDateFormat(szDate, fs2.fdateLastWrite); | 
|---|
| 136 | sprintf(s, " %s%s %ss %s %02u%s%02u%s%02u", | 
|---|
| 137 | fs2.attrFile & FILE_DIRECTORY ? | 
|---|
| 138 | GetPString(IDS_DIRBRKTTEXT) : NullStr, | 
|---|
| 139 | szCmmaFmtFileSize, | 
|---|
| 140 | GetPString(IDS_BYTETEXT), | 
|---|
| 141 | szDate, | 
|---|
| 142 | fs2.ftimeLastWrite.hours, TimeSeparator, | 
|---|
| 143 | fs2.ftimeLastWrite.minutes, TimeSeparator, fs2.ftimeLastWrite.twosecs * 2); | 
|---|
| 144 | WinSetDlgItemText(hwnd, REN_TARGETINFO, s); | 
|---|
| 145 | targetexists = 1; | 
|---|
| 146 | if (fs2.attrFile & (FILE_DIRECTORY)) | 
|---|
| 147 | targetexists = 3; | 
|---|
| 148 | WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), TRUE); | 
|---|
| 149 | } | 
|---|
| 150 | else { | 
|---|
| 151 | WinSetDlgItemText(hwnd, | 
|---|
| 152 | REN_TARGETINFO, (CHAR *) GetPString(IDS_DOESNTEXIST2TEXT)); | 
|---|
| 153 | WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE); | 
|---|
| 154 | } | 
|---|
| 155 | *s = 0; | 
|---|
| 156 | if (sourceexists) | 
|---|
| 157 | sprintf(s, | 
|---|
| 158 | GetPString(IDS_SOURCEISATEXT), | 
|---|
| 159 | sourceexists & 2 ? GetPString(IDS_DIRECTORYTEXT) : | 
|---|
| 160 | GetPString(IDS_FILETEXT)); | 
|---|
| 161 | { | 
|---|
| 162 | FILE *fp = NULL; | 
|---|
| 163 | CHAR *modeab = "ab"; | 
|---|
| 164 | if (~sourceexists & 2) | 
|---|
| 165 | fp = xfopen(mv->source, modeab, pszSrcFile, __LINE__, TRUE); | 
|---|
| 166 | if ((!fp && ~sourceexists & 2) || !sourceexists) | 
|---|
| 167 | strcpy(s, GetPString(IDS_CANTACCESSSOURCETEXT)); | 
|---|
| 168 | if (fp) | 
|---|
| 169 | fclose(fp); | 
|---|
| 170 | } | 
|---|
| 171 | if (targetexists && stricmp(mv->source, mv->target)) | 
|---|
| 172 | sprintf(&s[strlen(s)], | 
|---|
| 173 | GetPString(IDS_TARGETEXISTSISATEXT), | 
|---|
| 174 | targetexists & 2 ? GetPString(IDS_DIRECTORYTEXT) : | 
|---|
| 175 | GetPString(IDS_FILETEXT)); | 
|---|
| 176 | if (targetexists && stricmp(mv->source, mv->target)) | 
|---|
| 177 | strcpy(&s[strlen(s)], GetPString(IDS_CLICKOVERWRITETEXT)); | 
|---|
| 178 | else if (targetexists && !stricmp(mv->source, mv->target)) | 
|---|
| 179 | strcpy(&s[strlen(s)], GetPString(IDS_ENTERNEWTARGETTEXT)); | 
|---|
| 180 | WinEnableWindow(WinWindowFromID(hwnd, REN_OVERWRITE), | 
|---|
| 181 | stricmp(mv->target, mv->source) && | 
|---|
| 182 | (!mv->rename || strcmp(mv->target, mv->source))); | 
|---|
| 183 |  | 
|---|
| 184 | if (targetexists == 1 && sourceexists == 1) { | 
|---|
| 185 | sourcenewer = TestFDates(NULL, NULL, | 
|---|
| 186 | &fs1.fdateLastWrite, &fs1.ftimeLastWrite, | 
|---|
| 187 | &fs2.fdateLastWrite, &fs2.ftimeLastWrite); | 
|---|
| 188 | sourcesmaller = (fs1.cbFile < fs2.cbFile) ? -1 : | 
|---|
| 189 | (fs1.cbFile > fs2.cbFile) ? 1 : 0; | 
|---|
| 190 | sprintf(&s[strlen(s)], GetPString(IDS_SOURCEISTEXT), | 
|---|
| 191 | (sourcenewer == -1) ? GetPString(IDS_NEWERTEXT) : | 
|---|
| 192 | (sourcenewer == 1) ? GetPString(IDS_OLDERTEXT) : | 
|---|
| 193 | GetPString(IDS_SAMEDATETEXT), | 
|---|
| 194 | (sourcesmaller == -1) ? GetPString(IDS_SMALLERTEXT) : | 
|---|
| 195 | (sourcesmaller == 1) ? GetPString(IDS_LARGERTEXT) : | 
|---|
| 196 | GetPString(IDS_SAMESIZETEXT)); | 
|---|
| 197 | } | 
|---|
| 198 | WinSetDlgItemText(hwnd, REN_INFORMATION, s); | 
|---|
| 199 | if (targetexists && stricmp(mv->source, mv->target)) { | 
|---|
| 200 | if (WinQueryButtonCheckstate(hwnd, REN_DONTASK)) | 
|---|
| 201 | return 0; | 
|---|
| 202 | return MRFROM2SHORT(1, 0); | 
|---|
| 203 | } | 
|---|
| 204 | else if (targetexists && !stricmp(mv->source, mv->target)) { | 
|---|
| 205 | if (mv->rename && strcmp(mv->source, mv->target)) | 
|---|
| 206 | return 0; | 
|---|
| 207 | WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE); | 
|---|
| 208 | return MRFROM2SHORT(2, 0); | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 | } | 
|---|
| 212 | return 0; | 
|---|
| 213 |  | 
|---|
| 214 | case WM_COMMAND: | 
|---|
| 215 | switch (SHORT1FROMMP(mp1)) { | 
|---|
| 216 | case DID_CANCEL: | 
|---|
| 217 | WinDismissDlg(hwnd, 0); | 
|---|
| 218 | break; | 
|---|
| 219 |  | 
|---|
| 220 | case IDM_HELP: | 
|---|
| 221 | if (hwndHelp) | 
|---|
| 222 | WinSendMsg(hwndHelp, | 
|---|
| 223 | HM_DISPLAY_HELP, | 
|---|
| 224 | MPFROM2SHORT(HELP_RENAME, 0), MPFROMSHORT(HM_RESOURCEID)); | 
|---|
| 225 | break; | 
|---|
| 226 |  | 
|---|
| 227 | case REN_SKIP: | 
|---|
| 228 | mv = WinQueryWindowPtr(hwnd, QWL_USER); | 
|---|
| 229 | if (mv) { | 
|---|
| 230 | mv->skip = TRUE; | 
|---|
| 231 | *mv->target = 0; | 
|---|
| 232 | WinDismissDlg(hwnd, 2); | 
|---|
| 233 | } | 
|---|
| 234 | else | 
|---|
| 235 | WinDismissDlg(hwnd, 0); | 
|---|
| 236 | break; | 
|---|
| 237 |  | 
|---|
| 238 | case REN_RENEXIST: | 
|---|
| 239 | mv = WinQueryWindowPtr(hwnd, QWL_USER); | 
|---|
| 240 | if (mv) { | 
|---|
| 241 |  | 
|---|
| 242 | CHAR newexist[CCHMAXPATH], fullname[CCHMAXPATH]; | 
|---|
| 243 | INT was; | 
|---|
| 244 | APIRET rc; | 
|---|
| 245 | BOOL fResetVerify = FALSE; | 
|---|
| 246 |  | 
|---|
| 247 | *newexist = 0; | 
|---|
| 248 | WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, newexist); | 
|---|
| 249 | if (*newexist) { | 
|---|
| 250 | if (DosQueryPathInfo(newexist, | 
|---|
| 251 | FIL_QUERYFULLNAME, fullname, sizeof(fullname))) | 
|---|
| 252 | strcpy(fullname, newexist); | 
|---|
| 253 | was = IsFile(fullname); | 
|---|
| 254 | if (was == -1) { | 
|---|
| 255 | if (fVerify && (driveflags[toupper(*mv->target) - 'A'] & DRIVE_WRITEVERIFYOFF || | 
|---|
| 256 | driveflags[toupper(*fullname) - 'A'] & DRIVE_WRITEVERIFYOFF)) { | 
|---|
| 257 | DosSetVerify(FALSE); | 
|---|
| 258 | fResetVerify = TRUE; | 
|---|
| 259 | } | 
|---|
| 260 | rc = docopyf(MOVE, mv->target, fullname); | 
|---|
| 261 | if (fResetVerify) { | 
|---|
| 262 | DosSetVerify(fVerify); | 
|---|
| 263 | fResetVerify = FALSE; | 
|---|
| 264 | } | 
|---|
| 265 | if (rc) { | 
|---|
| 266 | if ((LONG) rc > 0) | 
|---|
| 267 | Dos_Error(MB_CANCEL, | 
|---|
| 268 | rc, | 
|---|
| 269 | hwnd, | 
|---|
| 270 | __FILE__, | 
|---|
| 271 | __LINE__, | 
|---|
| 272 | GetPString(IDS_COMPMOVEFAILEDTEXT), | 
|---|
| 273 | mv->target, fullname); | 
|---|
| 274 | else | 
|---|
| 275 | saymsg(MB_CANCEL | MB_ICONEXCLAMATION, | 
|---|
| 276 | hwnd, | 
|---|
| 277 | GetPString(IDS_SORRYTEXT), | 
|---|
| 278 | GetPString(IDS_COMPMOVEFAILEDTEXT), | 
|---|
| 279 | mv->target, fullname); | 
|---|
| 280 | } | 
|---|
| 281 | else | 
|---|
| 282 | saymsg(MB_ENTER, | 
|---|
| 283 | hwnd, | 
|---|
| 284 | GetPString(IDS_SUCCESSTEXT), | 
|---|
| 285 | GetPString(IDS_WASMOVEDTOTEXT), mv->target, fullname); | 
|---|
| 286 | } | 
|---|
| 287 | else | 
|---|
| 288 | saymsg(MB_CANCEL | MB_ICONEXCLAMATION, | 
|---|
| 289 | hwnd, | 
|---|
| 290 | GetPString(IDS_SORRYTEXT), | 
|---|
| 291 | GetPString(IDS_EXISTSASATEXT), | 
|---|
| 292 | fullname, | 
|---|
| 293 | (was) ? | 
|---|
| 294 | GetPString(IDS_FILETEXT) : GetPString(IDS_DIRECTORYTEXT)); | 
|---|
| 295 | } | 
|---|
| 296 | WinSetDlgItemText(hwnd, REN_TARGET, mv->target); | 
|---|
| 297 | } | 
|---|
| 298 | break; | 
|---|
| 299 |  | 
|---|
| 300 | case REN_OVERWRITE: | 
|---|
| 301 | case DID_OK: | 
|---|
| 302 | mv = WinQueryWindowPtr(hwnd, QWL_USER); | 
|---|
| 303 | if (mv) { | 
|---|
| 304 |  | 
|---|
| 305 | MRESULT mr; | 
|---|
| 306 |  | 
|---|
| 307 | if (WinQueryButtonCheckstate(hwnd, REN_DONTASK)) | 
|---|
| 308 | mv->dontask = TRUE; | 
|---|
| 309 | if (WinQueryButtonCheckstate(hwnd, REN_OVEROLD)) | 
|---|
| 310 | mv->overold = TRUE; | 
|---|
| 311 | if (WinQueryButtonCheckstate(hwnd, REN_OVERNEW)) | 
|---|
| 312 | mv->overnew = TRUE; | 
|---|
| 313 | *mv->target = 0; | 
|---|
| 314 | WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, mv->target); | 
|---|
| 315 | bstrip(mv->target); | 
|---|
| 316 | mr = WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID); | 
|---|
| 317 | if (!mr || | 
|---|
| 318 | (SHORT1FROMMR(mr) != 2 && SHORT1FROMMP(mp1) == REN_OVERWRITE)) { | 
|---|
| 319 |  | 
|---|
| 320 | CHAR path[CCHMAXPATH], *p; | 
|---|
| 321 |  | 
|---|
| 322 | mv->overwrite = (SHORT1FROMMP(mp1) == REN_OVERWRITE); | 
|---|
| 323 | strcpy(path, mv->target); | 
|---|
| 324 | p = strrchr(path, '\\'); | 
|---|
| 325 | if (p) { | 
|---|
| 326 | p++; | 
|---|
| 327 | *p = 0; | 
|---|
| 328 | if (SetDir(WinQueryWindow(WinQueryWindow(hwnd, QW_PARENT), | 
|---|
| 329 | QW_OWNER), hwnd, path, 0)) { | 
|---|
| 330 | if (!fAlertBeepOff) | 
|---|
| 331 | DosBeep(250, 100); | 
|---|
| 332 | WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET)); | 
|---|
| 333 | break; | 
|---|
| 334 | } | 
|---|
| 335 | } | 
|---|
| 336 | WinDismissDlg(hwnd, 1); | 
|---|
| 337 | } | 
|---|
| 338 | else { | 
|---|
| 339 | if (!fAlertBeepOff) | 
|---|
| 340 | DosBeep(250, 100); | 
|---|
| 341 | WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET)); | 
|---|
| 342 | } | 
|---|
| 343 | } | 
|---|
| 344 | break; | 
|---|
| 345 | } | 
|---|
| 346 | return 0; | 
|---|
| 347 | } | 
|---|
| 348 | return WinDefDlgProc(hwnd, msg, mp1, mp2); | 
|---|
| 349 | } | 
|---|
| 350 |  | 
|---|
| 351 | #pragma alloc_text(FMRENAME,RenameProc) | 
|---|