2 * Extract - Wine-compatible program for extract *.cab files.
4 * Copyright 2007 Etersoft (Lyutin Anatoly)
5 * Copyright 2009 Ilya Shpigor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(extrac32
);
33 static BOOL force_mode
;
34 static BOOL show_content
;
36 static void create_target_directory(LPWSTR Target
)
42 *PathFindFileNameW(dir
) = 0; /* Truncate file name */
43 if(!PathIsDirectoryW(dir
))
45 res
= SHCreateDirectoryExW(NULL
, dir
, NULL
);
46 if(res
!= ERROR_SUCCESS
&& res
!= ERROR_ALREADY_EXISTS
)
47 WINE_ERR("Can't create directory: %s\n", wine_dbgstr_w(dir
));
51 static UINT WINAPI
ExtCabCallback(PVOID Context
, UINT Notification
, UINT_PTR Param1
, UINT_PTR Param2
)
53 FILE_IN_CABINET_INFO_W
*pInfo
;
54 FILEPATHS_W
*pFilePaths
;
58 case SPFILENOTIFY_FILEINCABINET
:
59 pInfo
= (FILE_IN_CABINET_INFO_W
*)Param1
;
64 CHAR date
[12], time
[12], buf
[2 * MAX_PATH
];
68 /* DosDate and DosTime already represented at local time */
69 DosDateTimeToFileTime(pInfo
->DosDate
, pInfo
->DosTime
, &ft
);
70 FileTimeToSystemTime(&ft
, &st
);
71 GetDateFormatA(0, 0, &st
, "MM'-'dd'-'yyyy", date
, sizeof date
);
72 GetTimeFormatA(0, 0, &st
, "HH':'mm':'ss", time
, sizeof time
);
73 count
= wsprintfA(buf
, "%s %s %c%c%c%c %15u %S\n", date
, time
,
74 pInfo
->DosAttribs
& FILE_ATTRIBUTE_ARCHIVE
? 'A' : '-',
75 pInfo
->DosAttribs
& FILE_ATTRIBUTE_HIDDEN
? 'H' : '-',
76 pInfo
->DosAttribs
& FILE_ATTRIBUTE_READONLY
? 'R' : '-',
77 pInfo
->DosAttribs
& FILE_ATTRIBUTE_SYSTEM
? 'S' : '-',
78 pInfo
->FileSize
, pInfo
->NameInCabinet
);
79 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, count
, &dummy
, NULL
);
84 lstrcpyW(pInfo
->FullTargetName
, (LPCWSTR
)Context
);
85 lstrcatW(pInfo
->FullTargetName
, pInfo
->NameInCabinet
);
86 /* SetupIterateCabinet() doesn't create full path to target by itself,
87 so we should do it manually */
88 create_target_directory(pInfo
->FullTargetName
);
91 case SPFILENOTIFY_FILEEXTRACTED
:
92 pFilePaths
= (FILEPATHS_W
*)Param1
;
93 WINE_TRACE("Extracted %s\n", wine_dbgstr_w(pFilePaths
->Target
));
99 static void extract(LPCWSTR cabfile
, LPWSTR destdir
)
101 if (!SetupIterateCabinetW(cabfile
, 0, ExtCabCallback
, destdir
))
102 WINE_ERR("Could not extract cab file %s\n", wine_dbgstr_w(cabfile
));
105 static void copy_file(LPCWSTR source
, LPCWSTR destination
)
107 WCHAR destfile
[MAX_PATH
];
109 /* append source filename if destination is a directory */
110 if (PathIsDirectoryW(destination
))
112 PathCombineW(destfile
, destination
, PathFindFileNameW(source
));
113 destination
= destfile
;
116 if (PathFileExistsW(destination
) && !force_mode
)
118 static const WCHAR overwriteMsg
[] = {'O','v','e','r','w','r','i','t','e',' ','"','%','s','"','?',0};
119 static const WCHAR titleMsg
[] = {'E','x','t','r','a','c','t',0};
120 WCHAR msg
[MAX_PATH
+100];
121 snprintfW(msg
, sizeof(msg
)/sizeof(msg
[0]), overwriteMsg
, destination
);
122 if (MessageBoxW(NULL
, msg
, titleMsg
, MB_YESNO
| MB_ICONWARNING
) != IDYES
)
126 WINE_TRACE("copying %s to %s\n", wine_dbgstr_w(source
), wine_dbgstr_w(destination
));
127 CopyFileW(source
, destination
, FALSE
);
130 static LPWSTR
*get_extrac_args(LPWSTR cmdline
, int *pargc
)
132 enum {OUTSIDE_ARG
, INSIDE_ARG
, INSIDE_QUOTED_ARG
} state
;
139 WINE_TRACE("cmdline: %s\n", wine_dbgstr_w(cmdline
));
140 str
= HeapAlloc(GetProcessHeap(), 0, (strlenW(cmdline
) + 1) * sizeof(WCHAR
));
141 if(!str
) return NULL
;
142 strcpyW(str
, cmdline
);
143 argv
= HeapAlloc(GetProcessHeap(), 0, (max_argc
+ 1) * sizeof(LPWSTR
));
146 HeapFree(GetProcessHeap(), 0, str
);
150 /* Split command line to separate arg-strings and fill argv */
156 /* Check character */
157 if(isspaceW(*str
)) /* white space */
159 if(state
== INSIDE_ARG
)
165 else if(*str
== '"') /* double quote */
168 case INSIDE_QUOTED_ARG
:
176 if(!*++str
) continue;
177 state
= INSIDE_QUOTED_ARG
;
181 else /* regular character */
182 if(state
== OUTSIDE_ARG
)
188 /* Add new argv entry, if need */
191 if(argc
>= max_argc
- 1)
193 /* Realloc argv here because there always should be
194 at least one reserved cell for terminating NULL */
196 argv
= HeapReAlloc(GetProcessHeap(), 0, argv
,
197 (max_argc
+ 1) * sizeof(LPWSTR
));
200 HeapFree(GetProcessHeap(), 0, str
);
213 if(TRACE_ON(extrac32
))
216 for(i
= 0; i
< argc
; i
++)
217 WINE_TRACE("arg %d: %s\n", i
, wine_dbgstr_w(argv
[i
]));
222 int PASCAL
wWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPWSTR cmdline
, int show
)
227 WCHAR check
, cmd
= 0;
228 WCHAR path
[MAX_PATH
];
229 LPCWSTR cabfile
= NULL
;
233 /* Do not use CommandLineToArgvW() or __wgetmainargs() to parse
234 * command line for this program. It should treat each quote as argument
235 * delimiter. This doesn't match with behavior of mentioned functions.
236 * Do not use args provided by wmain() for the same reason.
238 argv
= get_extrac_args(cmdline
, &argc
);
242 WINE_ERR("Command line parsing failed\n");
246 /* Parse arguments */
247 for(i
= 0; i
< argc
; i
++)
250 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
259 /* Get parameters for commands */
260 check
= toupperW( argv
[i
][1] );
264 WINE_FIXME("/A not implemented\n");
270 if ((i
+ 1) >= argc
) return 0;
271 if (!GetFullPathNameW(argv
[++i
], MAX_PATH
, path
, NULL
))
290 if ((i
+ 1) != argc
) return 0;
291 if (!GetFullPathNameW(argv
[i
], MAX_PATH
, path
, NULL
))
295 /* Use extraction by default if names of required files presents */
296 cmd
= i
< argc
? 'E' : 'D';
298 if (cmd
== 'E' && !path
[0])
299 GetCurrentDirectoryW(MAX_PATH
, path
);
301 PathAddBackslashW(path
);
303 /* Execute the specified command */
308 copy_file(cabfile
, path
);
311 /* Display CAB archive */
315 /* Extract CAB archive */
316 extract(cabfile
, path
);