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