source: trunk/dll/rename.c@ 846

Last change on this file since 846 was 846, checked in by Steven Levine, 18 years ago

Correct ULONGLONG size formatting to avoid traps

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