| 1 | 
 | 
|---|
| 2 | /***********************************************************************
 | 
|---|
| 3 | 
 | 
|---|
| 4 |   $Id: rename.c 1226 2008-09-13 23:12:40Z jbs $
 | 
|---|
| 5 | 
 | 
|---|
| 6 |   Copyright (c) 1993-98 M. Kimes
 | 
|---|
| 7 |   Copyright (c) 2004, 2007 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 | 
 | 
|---|
| 16 | ***********************************************************************/
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <string.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #define INCL_WIN
 | 
|---|
| 21 | #define INCL_LONGLONG
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "fm3dll.h"
 | 
|---|
| 24 | #include "fm3dll2.h"                    // #define's for UM_*, control id's, etc.
 | 
|---|
| 25 | #include "worker.h"                     // typedef MOVEIT
 | 
|---|
| 26 | #include "rename.h"
 | 
|---|
| 27 | #include "notebook.h"                   // Data declaration(s)
 | 
|---|
| 28 | #include "init.h"                       // Data declaration(s)
 | 
|---|
| 29 | #include "fm3dlg.h"
 | 
|---|
| 30 | #include "fm3str.h"
 | 
|---|
| 31 | #include "errutil.h"                    // Dos_Error...
 | 
|---|
| 32 | #include "strutil.h"                    // GetPString
 | 
|---|
| 33 | #include "copyf.h"                      // AdjustWildcardName
 | 
|---|
| 34 | #include "valid.h"                      // TestFDates
 | 
|---|
| 35 | #include "mkdir.h"                      // SetDir
 | 
|---|
| 36 | #include "commafmt.h"                   // CommaFmtULL
 | 
|---|
| 37 | #include "strips.h"                     // bstrip
 | 
|---|
| 38 | 
 | 
|---|
| 39 | MRESULT EXPENTRY RenameProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   MOVEIT *mv;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   switch (msg) {
 | 
|---|
| 44 |   case WM_INITDLG:
 | 
|---|
| 45 |     mv = (MOVEIT *) mp2;
 | 
|---|
| 46 |     WinSetWindowPtr(hwnd, QWL_USER, (PVOID) mv);
 | 
|---|
| 47 |     if (!mv || !mv->source) {
 | 
|---|
| 48 |       WinDismissDlg(hwnd, 0);
 | 
|---|
| 49 |       break;
 | 
|---|
| 50 |     }
 | 
|---|
| 51 |     WinSendDlgItemMsg(hwnd,
 | 
|---|
| 52 |                       REN_SOURCE,
 | 
|---|
| 53 |                       EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
 | 
|---|
| 54 |     WinSendDlgItemMsg(hwnd,
 | 
|---|
| 55 |                       REN_TARGET,
 | 
|---|
| 56 |                       EM_SETTEXTLIMIT, MPFROM2SHORT(CCHMAXPATH, 0), MPVOID);
 | 
|---|
| 57 |     if (!*mv->target)
 | 
|---|
| 58 |       strcpy(mv->target, mv->source);
 | 
|---|
| 59 |     WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID);
 | 
|---|
| 60 |     if (mv->rename || !stricmp(mv->target, mv->source)) {
 | 
|---|
| 61 | 
 | 
|---|
| 62 |       CHAR *p = strrchr(mv->target, '\\');
 | 
|---|
| 63 |       if (p) {
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         USHORT sello, selhi;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         sello = p - mv->target + 1;
 | 
|---|
| 68 |         selhi = strlen(mv->target);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         WinSendDlgItemMsg(hwnd,
 | 
|---|
| 71 |                           REN_TARGET,
 | 
|---|
| 72 |                           EM_SETSEL, MPFROM2SHORT(sello, selhi), MPVOID);
 | 
|---|
| 73 |       }
 | 
|---|
| 74 |       WinShowWindow(WinWindowFromID(hwnd, REN_OVEROLD), FALSE);
 | 
|---|
| 75 |       WinShowWindow(WinWindowFromID(hwnd, REN_OVERNEW), FALSE);
 | 
|---|
| 76 |     }
 | 
|---|
| 77 |     break;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   case UM_RESCAN:
 | 
|---|
| 80 |     {
 | 
|---|
| 81 |       mv = WinQueryWindowPtr(hwnd, QWL_USER);
 | 
|---|
| 82 |       if (mv) {
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         FILESTATUS3L fs1, fs2;
 | 
|---|
| 85 |         CHAR s[CCHMAXPATH * 2], *p, chkname[CCHMAXPATH], szCmmaFmtFileSize[81];
 | 
|---|
| 86 |         INT sourceexists = 0, targetexists = 0,
 | 
|---|
| 87 |             sourcenewer = 0, sourcesmaller = 0;
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         p = mv->target;
 | 
|---|
| 90 |         while (*p) {
 | 
|---|
| 91 |           if (*p == '/')
 | 
|---|
| 92 |             *p = '\\';
 | 
|---|
| 93 |           p++;
 | 
|---|
| 94 |         }
 | 
|---|
| 95 |         if (!MakeFullName(mv->target))
 | 
|---|
| 96 |           WinSetDlgItemText(hwnd, REN_TARGET, mv->target);
 | 
|---|
| 97 |         if (!MakeFullName(mv->source))
 | 
|---|
| 98 |           WinSetDlgItemText(hwnd, REN_SOURCE, mv->source);
 | 
|---|
| 99 |         if (!DosQueryPathInfo(mv->source, FIL_STANDARDL, &fs1, sizeof(fs1))) {
 | 
|---|
| 100 |           CommaFmtULL(szCmmaFmtFileSize,
 | 
|---|
| 101 |                       sizeof(szCmmaFmtFileSize), fs1.cbFile, ' ');
 | 
|---|
| 102 |           sprintf(s,
 | 
|---|
| 103 |                   " %s%s %ss %04u/%02u/%02u %02u:%02u:%02u",
 | 
|---|
| 104 |                   fs1.attrFile & FILE_DIRECTORY ?
 | 
|---|
| 105 |                     GetPString(IDS_DIRBRKTTEXT) : NullStr,
 | 
|---|
| 106 |                   szCmmaFmtFileSize,
 | 
|---|
| 107 |                   GetPString(IDS_BYTETEXT),
 | 
|---|
| 108 |                   fs1.fdateLastWrite.year + 1980,
 | 
|---|
| 109 |                   fs1.fdateLastWrite.month,
 | 
|---|
| 110 |                   fs1.fdateLastWrite.day,
 | 
|---|
| 111 |                   fs1.ftimeLastWrite.hours,
 | 
|---|
| 112 |                   fs1.ftimeLastWrite.minutes, fs1.ftimeLastWrite.twosecs * 2);
 | 
|---|
| 113 |           WinSetDlgItemText(hwnd, REN_SOURCEINFO, s);
 | 
|---|
| 114 |           sourceexists = 1;
 | 
|---|
| 115 |           if (fs1.attrFile & FILE_DIRECTORY)
 | 
|---|
| 116 |             sourceexists = 3;
 | 
|---|
| 117 |         }
 | 
|---|
| 118 |         else
 | 
|---|
| 119 |           WinSetDlgItemText(hwnd,
 | 
|---|
| 120 |                             REN_SOURCEINFO, GetPString(IDS_DOESNTEXIST2TEXT));
 | 
|---|
| 121 |         strcpy(chkname, mv->target);
 | 
|---|
| 122 |         p = strrchr(s, '\\');
 | 
|---|
| 123 |         if (p && (strchr(p, '*') || strchr(p, '?'))) {
 | 
|---|
| 124 |           if (!AdjustWildcardName(mv->target, chkname))
 | 
|---|
| 125 |             strcpy(chkname, mv->target);
 | 
|---|
| 126 |         }
 | 
|---|
| 127 |         if (!DosQueryPathInfo(chkname, FIL_STANDARDL, &fs2, sizeof(fs2))) {
 | 
|---|
| 128 |           CommaFmtULL(szCmmaFmtFileSize,
 | 
|---|
| 129 |                       sizeof(szCmmaFmtFileSize), fs2.cbFile, ' ');
 | 
|---|
| 130 |           sprintf(s,
 | 
|---|
| 131 |                   " %s%s %ss %04u/%02u/%02u %02u:%02u:%02u",
 | 
|---|
| 132 |                   fs2.attrFile & FILE_DIRECTORY ?
 | 
|---|
| 133 |                     GetPString(IDS_DIRBRKTTEXT) : NullStr,
 | 
|---|
| 134 |                   szCmmaFmtFileSize,
 | 
|---|
| 135 |                   GetPString(IDS_BYTETEXT),
 | 
|---|
| 136 |                   fs2.fdateLastWrite.year + 1980,
 | 
|---|
| 137 |                   fs2.fdateLastWrite.month,
 | 
|---|
| 138 |                   fs2.fdateLastWrite.day,
 | 
|---|
| 139 |                   fs2.ftimeLastWrite.hours,
 | 
|---|
| 140 |                   fs2.ftimeLastWrite.minutes, fs2.ftimeLastWrite.twosecs * 2);
 | 
|---|
| 141 |           WinSetDlgItemText(hwnd, REN_TARGETINFO, s);
 | 
|---|
| 142 |           targetexists = 1;
 | 
|---|
| 143 |           if (fs2.attrFile & (FILE_DIRECTORY))
 | 
|---|
| 144 |             targetexists = 3;
 | 
|---|
| 145 |           WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), TRUE);
 | 
|---|
| 146 |         }
 | 
|---|
| 147 |         else {
 | 
|---|
| 148 |           WinSetDlgItemText(hwnd,
 | 
|---|
| 149 |                             REN_TARGETINFO, GetPString(IDS_DOESNTEXIST2TEXT));
 | 
|---|
| 150 |           WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE);
 | 
|---|
| 151 |         }
 | 
|---|
| 152 |         *s = 0;
 | 
|---|
| 153 |         if (sourceexists)
 | 
|---|
| 154 |           sprintf(s,
 | 
|---|
| 155 |                   GetPString(IDS_SOURCEISATEXT),
 | 
|---|
| 156 |                   sourceexists & 2 ? GetPString(IDS_DIRECTORYTEXT) :
 | 
|---|
| 157 |                                      GetPString(IDS_FILETEXT));
 | 
|---|
| 158 |         {
 | 
|---|
| 159 |           FILE *fp = NULL;
 | 
|---|
| 160 |           if (~sourceexists & 2)
 | 
|---|
| 161 |             fp = fopen(mv->source, "ab");
 | 
|---|
| 162 |           if ((!fp && ~sourceexists & 2) || !sourceexists)
 | 
|---|
| 163 |             strcpy(s, GetPString(IDS_CANTACCESSSOURCETEXT));
 | 
|---|
| 164 |           if (fp)
 | 
|---|
| 165 |             fclose(fp);
 | 
|---|
| 166 |         }
 | 
|---|
| 167 |         if (targetexists && stricmp(mv->source, mv->target))
 | 
|---|
| 168 |           sprintf(&s[strlen(s)],
 | 
|---|
| 169 |                   GetPString(IDS_TARGETEXISTSISATEXT),
 | 
|---|
| 170 |                   targetexists & 2 ? GetPString(IDS_DIRECTORYTEXT) :
 | 
|---|
| 171 |                                      GetPString(IDS_FILETEXT));
 | 
|---|
| 172 |         if (targetexists && stricmp(mv->source, mv->target))
 | 
|---|
| 173 |           strcpy(&s[strlen(s)], GetPString(IDS_CLICKOVERWRITETEXT));
 | 
|---|
| 174 |         else if (targetexists && !stricmp(mv->source, mv->target))
 | 
|---|
| 175 |           strcpy(&s[strlen(s)], GetPString(IDS_ENTERNEWTARGETTEXT));
 | 
|---|
| 176 |         WinEnableWindow(WinWindowFromID(hwnd, REN_OVERWRITE),
 | 
|---|
| 177 |                         stricmp(mv->target, mv->source) &&
 | 
|---|
| 178 |                           (!mv->rename || strcmp(mv->target, mv->source)));
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         if (targetexists == 1 && sourceexists == 1) {
 | 
|---|
| 181 |           sourcenewer = TestFDates(NULL, NULL,
 | 
|---|
| 182 |                                    &fs1.fdateLastWrite, &fs1.ftimeLastWrite,
 | 
|---|
| 183 |                                    &fs2.fdateLastWrite, &fs2.ftimeLastWrite);
 | 
|---|
| 184 |           sourcesmaller = (fs1.cbFile < fs2.cbFile) ? -1 :
 | 
|---|
| 185 |                           (fs1.cbFile > fs2.cbFile) ? 1 : 0;
 | 
|---|
| 186 |           sprintf(&s[strlen(s)], GetPString(IDS_SOURCEISTEXT),
 | 
|---|
| 187 |                   (sourcenewer == -1) ? GetPString(IDS_NEWERTEXT) :
 | 
|---|
| 188 |                     (sourcenewer == 1) ? GetPString(IDS_OLDERTEXT) :
 | 
|---|
| 189 |                       GetPString(IDS_SAMEDATETEXT),
 | 
|---|
| 190 |                   (sourcesmaller == -1) ? GetPString(IDS_SMALLERTEXT) :
 | 
|---|
| 191 |                     (sourcesmaller == 1) ? GetPString(IDS_LARGERTEXT) :
 | 
|---|
| 192 |                       GetPString(IDS_SAMESIZETEXT));
 | 
|---|
| 193 |         }
 | 
|---|
| 194 |         WinSetDlgItemText(hwnd, REN_INFORMATION, s);
 | 
|---|
| 195 |         if (targetexists && stricmp(mv->source, mv->target)) {
 | 
|---|
| 196 |           if (WinQueryButtonCheckstate(hwnd, REN_DONTASK))
 | 
|---|
| 197 |             return 0;
 | 
|---|
| 198 |           return MRFROM2SHORT(1, 0);
 | 
|---|
| 199 |         }
 | 
|---|
| 200 |         else if (targetexists && !stricmp(mv->source, mv->target)) {
 | 
|---|
| 201 |           if (mv->rename && strcmp(mv->source, mv->target))
 | 
|---|
| 202 |             return 0;
 | 
|---|
| 203 |           WinEnableWindow(WinWindowFromID(hwnd, REN_RENEXIST), FALSE);
 | 
|---|
| 204 |           return MRFROM2SHORT(2, 0);
 | 
|---|
| 205 |         }
 | 
|---|
| 206 |       }
 | 
|---|
| 207 |     }
 | 
|---|
| 208 |     return 0;
 | 
|---|
| 209 | 
 | 
|---|
| 210 |   case WM_COMMAND:
 | 
|---|
| 211 |     switch (SHORT1FROMMP(mp1)) {
 | 
|---|
| 212 |     case DID_CANCEL:
 | 
|---|
| 213 |       WinDismissDlg(hwnd, 0);
 | 
|---|
| 214 |       break;
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     case IDM_HELP:
 | 
|---|
| 217 |       if (hwndHelp)
 | 
|---|
| 218 |         WinSendMsg(hwndHelp,
 | 
|---|
| 219 |                    HM_DISPLAY_HELP,
 | 
|---|
| 220 |                    MPFROM2SHORT(HELP_RENAME, 0), MPFROMSHORT(HM_RESOURCEID));
 | 
|---|
| 221 |       break;
 | 
|---|
| 222 | 
 | 
|---|
| 223 |     case REN_SKIP:
 | 
|---|
| 224 |       mv = WinQueryWindowPtr(hwnd, QWL_USER);
 | 
|---|
| 225 |       if (mv) {
 | 
|---|
| 226 |         mv->skip = TRUE;
 | 
|---|
| 227 |         *mv->target = 0;
 | 
|---|
| 228 |         WinDismissDlg(hwnd, 2);
 | 
|---|
| 229 |       }
 | 
|---|
| 230 |       else
 | 
|---|
| 231 |         WinDismissDlg(hwnd, 0);
 | 
|---|
| 232 |       break;
 | 
|---|
| 233 | 
 | 
|---|
| 234 |     case REN_RENEXIST:
 | 
|---|
| 235 |       mv = WinQueryWindowPtr(hwnd, QWL_USER);
 | 
|---|
| 236 |       if (mv) {
 | 
|---|
| 237 | 
 | 
|---|
| 238 |         CHAR newexist[CCHMAXPATH], fullname[CCHMAXPATH];
 | 
|---|
| 239 |         INT was;
 | 
|---|
| 240 |         APIRET rc;
 | 
|---|
| 241 | 
 | 
|---|
| 242 |         *newexist = 0;
 | 
|---|
| 243 |         WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, newexist);
 | 
|---|
| 244 |         if (*newexist) {
 | 
|---|
| 245 |           if (DosQueryPathInfo(newexist,
 | 
|---|
| 246 |                                FIL_QUERYFULLNAME, fullname, sizeof(fullname)))
 | 
|---|
| 247 |             strcpy(fullname, newexist);
 | 
|---|
| 248 |           was = IsFile(fullname);
 | 
|---|
| 249 |           if (was == -1) {
 | 
|---|
| 250 |             rc = docopyf(MOVE, mv->target, "%s", fullname);
 | 
|---|
| 251 |             if (rc) {
 | 
|---|
| 252 |               if ((LONG) rc > 0)
 | 
|---|
| 253 |                 Dos_Error(MB_CANCEL,
 | 
|---|
| 254 |                           rc,
 | 
|---|
| 255 |                           hwnd,
 | 
|---|
| 256 |                           __FILE__,
 | 
|---|
| 257 |                           __LINE__,
 | 
|---|
| 258 |                           GetPString(IDS_COMPMOVEFAILEDTEXT),
 | 
|---|
| 259 |                           mv->target, fullname);
 | 
|---|
| 260 |               else
 | 
|---|
| 261 |                 saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
 | 
|---|
| 262 |                        hwnd,
 | 
|---|
| 263 |                        GetPString(IDS_SORRYTEXT),
 | 
|---|
| 264 |                        GetPString(IDS_COMPMOVEFAILEDTEXT),
 | 
|---|
| 265 |                        mv->target, fullname);
 | 
|---|
| 266 |             }
 | 
|---|
| 267 |             else
 | 
|---|
| 268 |               saymsg(MB_ENTER,
 | 
|---|
| 269 |                      hwnd,
 | 
|---|
| 270 |                      GetPString(IDS_SUCCESSTEXT),
 | 
|---|
| 271 |                      GetPString(IDS_WASMOVEDTOTEXT), mv->target, fullname);
 | 
|---|
| 272 |           }
 | 
|---|
| 273 |           else
 | 
|---|
| 274 |             saymsg(MB_CANCEL | MB_ICONEXCLAMATION,
 | 
|---|
| 275 |                    hwnd,
 | 
|---|
| 276 |                    GetPString(IDS_SORRYTEXT),
 | 
|---|
| 277 |                    GetPString(IDS_EXISTSASATEXT),
 | 
|---|
| 278 |                    fullname,
 | 
|---|
| 279 |                    (was) ?
 | 
|---|
| 280 |                    GetPString(IDS_FILETEXT) : GetPString(IDS_DIRECTORYTEXT));
 | 
|---|
| 281 |         }
 | 
|---|
| 282 |         WinSetDlgItemText(hwnd, REN_TARGET, mv->target);
 | 
|---|
| 283 |       }
 | 
|---|
| 284 |       break;
 | 
|---|
| 285 | 
 | 
|---|
| 286 |     case REN_OVERWRITE:
 | 
|---|
| 287 |     case DID_OK:
 | 
|---|
| 288 |       mv = WinQueryWindowPtr(hwnd, QWL_USER);
 | 
|---|
| 289 |       if (mv) {
 | 
|---|
| 290 | 
 | 
|---|
| 291 |         MRESULT mr;
 | 
|---|
| 292 | 
 | 
|---|
| 293 |         if (WinQueryButtonCheckstate(hwnd, REN_DONTASK))
 | 
|---|
| 294 |           mv->dontask = TRUE;
 | 
|---|
| 295 |         if (WinQueryButtonCheckstate(hwnd, REN_OVEROLD))
 | 
|---|
| 296 |           mv->overold = TRUE;
 | 
|---|
| 297 |         if (WinQueryButtonCheckstate(hwnd, REN_OVERNEW))
 | 
|---|
| 298 |           mv->overnew = TRUE;
 | 
|---|
| 299 |         *mv->target = 0;
 | 
|---|
| 300 |         WinQueryDlgItemText(hwnd, REN_TARGET, CCHMAXPATH, mv->target);
 | 
|---|
| 301 |         bstrip(mv->target);
 | 
|---|
| 302 |         mr = WinSendMsg(hwnd, UM_RESCAN, MPVOID, MPVOID);
 | 
|---|
| 303 |         if (!mr ||
 | 
|---|
| 304 |             (SHORT1FROMMR(mr) != 2 && SHORT1FROMMP(mp1) == REN_OVERWRITE)) {
 | 
|---|
| 305 | 
 | 
|---|
| 306 |           CHAR path[CCHMAXPATH], *p;
 | 
|---|
| 307 | 
 | 
|---|
| 308 |           mv->overwrite = (SHORT1FROMMP(mp1) == REN_OVERWRITE);
 | 
|---|
| 309 |           strcpy(path, mv->target);
 | 
|---|
| 310 |           p = strrchr(path, '\\');
 | 
|---|
| 311 |           if (p) {
 | 
|---|
| 312 |             p++;
 | 
|---|
| 313 |             *p = 0;
 | 
|---|
| 314 |             if (SetDir(WinQueryWindow(WinQueryWindow(hwnd, QW_PARENT),
 | 
|---|
| 315 |                                       QW_OWNER), hwnd, path, 0)) {
 | 
|---|
| 316 |               DosBeep(250, 100);
 | 
|---|
| 317 |               WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET));
 | 
|---|
| 318 |               break;
 | 
|---|
| 319 |             }
 | 
|---|
| 320 |           }
 | 
|---|
| 321 |           WinDismissDlg(hwnd, 1);
 | 
|---|
| 322 |         }
 | 
|---|
| 323 |         else {
 | 
|---|
| 324 |           DosBeep(250, 100);
 | 
|---|
| 325 |           WinSetFocus(HWND_DESKTOP, WinWindowFromID(hwnd, REN_TARGET));
 | 
|---|
| 326 |         }
 | 
|---|
| 327 |       }
 | 
|---|
| 328 |       break;
 | 
|---|
| 329 |     }
 | 
|---|
| 330 |     return 0;
 | 
|---|
| 331 |   }
 | 
|---|
| 332 |   return WinDefDlgProc(hwnd, msg, mp1, mp2);
 | 
|---|
| 333 | }
 | 
|---|
| 334 | 
 | 
|---|
| 335 | #pragma alloc_text(FMRENAME,RenameProc)
 | 
|---|