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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(extrac32
);
34 static BOOL force_mode
;
35 static BOOL show_content
;
37 static void create_target_directory(LPWSTR Target
)
42 lstrcpyW(dir
, Target
);
43 *PathFindFileNameW(dir
) = 0; /* Truncate file name */
44 if(!PathIsDirectoryW(dir
))
46 res
= SHCreateDirectoryExW(NULL
, dir
, NULL
);
47 if(res
!= ERROR_SUCCESS
&& res
!= ERROR_ALREADY_EXISTS
)
48 WINE_ERR("Can't create directory: %s\n", wine_dbgstr_w(dir
));
52 static UINT WINAPI
ExtCabCallback(PVOID Context
, UINT Notification
, UINT_PTR Param1
, UINT_PTR Param2
)
54 FILE_IN_CABINET_INFO_W
*pInfo
;
55 FILEPATHS_W
*pFilePaths
;
59 case SPFILENOTIFY_FILEINCABINET
:
60 pInfo
= (FILE_IN_CABINET_INFO_W
*)Param1
;
65 CHAR date
[12], time
[12], buf
[2 * MAX_PATH
];
69 /* DosDate and DosTime already represented at local time */
70 DosDateTimeToFileTime(pInfo
->DosDate
, pInfo
->DosTime
, &ft
);
71 FileTimeToSystemTime(&ft
, &st
);
72 GetDateFormatA(0, 0, &st
, "MM'-'dd'-'yyyy", date
, sizeof date
);
73 GetTimeFormatA(0, 0, &st
, "HH':'mm':'ss", time
, sizeof time
);
74 count
= wsprintfA(buf
, "%s %s %c%c%c%c %15u %S\n", date
, time
,
75 pInfo
->DosAttribs
& FILE_ATTRIBUTE_ARCHIVE
? 'A' : '-',
76 pInfo
->DosAttribs
& FILE_ATTRIBUTE_HIDDEN
? 'H' : '-',
77 pInfo
->DosAttribs
& FILE_ATTRIBUTE_READONLY
? 'R' : '-',
78 pInfo
->DosAttribs
& FILE_ATTRIBUTE_SYSTEM
? 'S' : '-',
79 pInfo
->FileSize
, pInfo
->NameInCabinet
);
80 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, count
, &dummy
, NULL
);
85 lstrcpyW(pInfo
->FullTargetName
, (LPCWSTR
)Context
);
86 lstrcatW(pInfo
->FullTargetName
, pInfo
->NameInCabinet
);
87 /* SetupIterateCabinet() doesn't create full path to target by itself,
88 so we should do it manually */
89 create_target_directory(pInfo
->FullTargetName
);
92 case SPFILENOTIFY_FILEEXTRACTED
:
93 pFilePaths
= (FILEPATHS_W
*)Param1
;
94 WINE_TRACE("Extracted %s\n", wine_dbgstr_w(pFilePaths
->Target
));
100 static void extract(LPCWSTR cabfile
, LPWSTR destdir
)
102 if (!SetupIterateCabinetW(cabfile
, 0, ExtCabCallback
, destdir
))
103 WINE_ERR("Could not extract cab file %s\n", wine_dbgstr_w(cabfile
));
106 static void copy_file(LPCWSTR source
, LPCWSTR destination
)
108 WCHAR destfile
[MAX_PATH
];
110 /* append source filename if destination is a directory */
111 if (PathIsDirectoryW(destination
))
113 PathCombineW(destfile
, destination
, PathFindFileNameW(source
));
114 destination
= destfile
;
117 if (PathFileExistsW(destination
) && !force_mode
)
119 WCHAR msg
[MAX_PATH
+100];
120 swprintf(msg
, ARRAY_SIZE(msg
), L
"Overwrite \"%s\"?", destination
);
121 if (MessageBoxW(NULL
, msg
, L
"Extract", MB_YESNO
| MB_ICONWARNING
) != IDYES
)
125 WINE_TRACE("copying %s to %s\n", wine_dbgstr_w(source
), wine_dbgstr_w(destination
));
126 CopyFileW(source
, destination
, FALSE
);
129 static LPWSTR
*get_extrac_args(LPWSTR cmdline
, int *pargc
)
131 enum {OUTSIDE_ARG
, INSIDE_ARG
, INSIDE_QUOTED_ARG
} state
;
138 WINE_TRACE("cmdline: %s\n", wine_dbgstr_w(cmdline
));
139 str
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline
) + 1) * sizeof(WCHAR
));
140 if(!str
) return NULL
;
141 lstrcpyW(str
, cmdline
);
142 argv
= HeapAlloc(GetProcessHeap(), 0, (max_argc
+ 1) * sizeof(LPWSTR
));
145 HeapFree(GetProcessHeap(), 0, str
);
149 /* Split command line to separate arg-strings and fill argv */
155 /* Check character */
156 if(iswspace(*str
)) /* white space */
158 if(state
== INSIDE_ARG
)
164 else if(*str
== '"') /* double quote */
167 case INSIDE_QUOTED_ARG
:
175 if(!*++str
) continue;
176 state
= INSIDE_QUOTED_ARG
;
180 else /* regular character */
181 if(state
== OUTSIDE_ARG
)
187 /* Add new argv entry, if need */
190 if(argc
>= max_argc
- 1)
192 /* Realloc argv here because there always should be
193 at least one reserved cell for terminating NULL */
195 argv
= HeapReAlloc(GetProcessHeap(), 0, argv
,
196 (max_argc
+ 1) * sizeof(LPWSTR
));
199 HeapFree(GetProcessHeap(), 0, str
);
212 if(TRACE_ON(extrac32
))
215 for(i
= 0; i
< argc
; i
++)
216 WINE_TRACE("arg %d: %s\n", i
, wine_dbgstr_w(argv
[i
]));
221 int PASCAL
wWinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPWSTR cmdline
, int show
)
226 WCHAR check
, cmd
= 0;
227 WCHAR path
[MAX_PATH
];
228 LPCWSTR cabfile
= NULL
;
230 InitCommonControls();
234 /* Do not use CommandLineToArgvW() or __wgetmainargs() to parse
235 * command line for this program. It should treat each quote as argument
236 * delimiter. This doesn't match with behavior of mentioned functions.
237 * Do not use args provided by wmain() for the same reason.
239 argv
= get_extrac_args(cmdline
, &argc
);
243 WINE_ERR("Command line parsing failed\n");
247 /* Parse arguments */
248 for(i
= 0; i
< argc
; i
++)
251 if (argv
[i
][0] != '/' && argv
[i
][0] != '-')
260 /* Get parameters for commands */
261 check
= towupper( argv
[i
][1] );
265 WINE_FIXME("/A not implemented\n");
271 if ((i
+ 1) >= argc
) return 0;
272 if (!GetFullPathNameW(argv
[++i
], MAX_PATH
, path
, NULL
))
291 if ((i
+ 1) != argc
) return 0;
292 if (!GetFullPathNameW(argv
[i
], MAX_PATH
, path
, NULL
))
296 /* Use extraction by default if names of required files presents */
297 cmd
= i
< argc
? 'E' : 'D';
299 if (cmd
== 'E' && !path
[0])
300 GetCurrentDirectoryW(MAX_PATH
, path
);
302 PathAddBackslashW(path
);
304 /* Execute the specified command */
309 copy_file(cabfile
, path
);
312 /* Display CAB archive */
316 /* Extract CAB archive */
317 extract(cabfile
, path
);