2 * ATTRIB - Wine-compatible attrib program
4 * Copyright 2010-2012 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <wine/debug.h>
25 WINE_DEFAULT_DEBUG_CHANNEL(attrib
);
27 static const WCHAR starW
[] = {'*','\0'};
29 /* =========================================================================
30 * Load a string from the resource file, handling any error
31 * Returns string retrieved from resource file
32 * ========================================================================= */
33 static WCHAR
*ATTRIB_LoadMessage(UINT id
)
35 static WCHAR msg
[MAXSTRING
];
36 const WCHAR failedMsg
[] = {'F', 'a', 'i', 'l', 'e', 'd', '!', 0};
38 if (!LoadStringW(GetModuleHandleW(NULL
), id
, msg
, ARRAY_SIZE(msg
))) {
39 WINE_FIXME("LoadString failed with %d\n", GetLastError());
40 lstrcpyW(msg
, failedMsg
);
45 /* =========================================================================
46 * Output a formatted unicode string. Ideally this will go to the console
47 * and hence required WriteConsoleW to output it, however if file i/o is
48 * redirected, it needs to be WriteFile'd using OEM (not ANSI) format
49 * ========================================================================= */
50 static int WINAPIV
ATTRIB_wprintf(const WCHAR
*format
, ...)
52 static WCHAR
*output_bufW
= NULL
;
53 static char *output_bufA
= NULL
;
54 static BOOL toConsole
= TRUE
;
55 static BOOL traceOutput
= FALSE
;
56 #define MAX_WRITECONSOLE_SIZE 65535
64 * Allocate buffer to use when writing to console
65 * Note: Not freed - memory will be allocated once and released when
69 if (!output_bufW
) output_bufW
= HeapAlloc(GetProcessHeap(), 0,
70 MAX_WRITECONSOLE_SIZE
*sizeof(WCHAR
));
72 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
76 __ms_va_start(parms
, format
);
77 SetLastError(NO_ERROR
);
78 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, format
, 0, 0, output_bufW
,
79 MAX_WRITECONSOLE_SIZE
/sizeof(*output_bufW
), &parms
);
81 if (len
== 0 && GetLastError() != NO_ERROR
) {
82 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format
));
86 /* Try to write as unicode all the time we think it's a console */
88 res
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
89 output_bufW
, len
, &nOut
, NULL
);
92 /* If writing to console has failed (ever) we assume it's file
93 i/o so convert to OEM codepage and output */
95 BOOL usedDefaultChar
= FALSE
;
101 * Allocate buffer to use when writing to file. Not freed, as above
103 if (!output_bufA
) output_bufA
= HeapAlloc(GetProcessHeap(), 0,
104 MAX_WRITECONSOLE_SIZE
);
106 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
110 /* Convert to OEM, then output */
111 convertedChars
= WideCharToMultiByte(GetConsoleOutputCP(), 0, output_bufW
,
112 len
, output_bufA
, MAX_WRITECONSOLE_SIZE
,
113 "?", &usedDefaultChar
);
114 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), output_bufA
, convertedChars
,
118 /* Trace whether screen or console */
120 WINE_TRACE("Writing to console? (%d)\n", toConsole
);
126 /* =========================================================================
127 * Handle the processing for a single directory, optionally recursing into
128 * subdirectories if needed.
130 * rootdir [I] The directory to search in
131 * filespec [I] The filespec to search for
132 * recurse [I] Whether to recurse (search subdirectories before
134 * includedirs [I] Whether to set directory attributes as well
135 * attrib_set [I] Attributes to set
136 * attrib_clear [I] Attributes to clear
138 * Returns TRUE if at least one file displayed / modified
139 * ========================================================================= */
140 static BOOL
ATTRIB_processdirectory(const WCHAR
*rootdir
, const WCHAR
*filespec
,
141 BOOL recurse
, BOOL includedirs
,
142 DWORD attrib_set
, DWORD attrib_clear
)
145 WCHAR buffer
[MAX_PATH
];
148 WCHAR flags
[] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
149 static const WCHAR slashW
[] = {'\\','\0'};
151 WINE_TRACE("Processing dir '%s', spec '%s', %d,%x,%x\n",
152 wine_dbgstr_w(rootdir
), wine_dbgstr_w(filespec
),
153 recurse
, attrib_set
, attrib_clear
);
157 /* Build spec to search for */
158 lstrcpyW(buffer
, rootdir
);
159 lstrcatW(buffer
, starW
);
161 /* Search for directories in the location and recurse if necessary */
162 WINE_TRACE("Searching for directories with '%s'\n", wine_dbgstr_w(buffer
));
163 hff
= FindFirstFileW(buffer
, &fd
);
164 if (hff
!= INVALID_HANDLE_VALUE
) {
166 const WCHAR dot
[] = {'.', 0};
167 const WCHAR dotdot
[] = {'.', '.', 0};
169 /* Only interested in directories, and not . nor .. */
170 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
171 !lstrcmpW(fd
.cFileName
, dot
) || !lstrcmpW(fd
.cFileName
, dotdot
))
174 /* Build new root dir to go searching in */
175 lstrcpyW(buffer
, rootdir
);
176 lstrcatW(buffer
, fd
.cFileName
);
177 lstrcatW(buffer
, slashW
);
178 ATTRIB_processdirectory(buffer
, filespec
, recurse
, includedirs
,
179 attrib_set
, attrib_clear
);
181 } while (FindNextFileW(hff
, &fd
) != 0);
186 /* Build spec to search for */
187 lstrcpyW(buffer
, rootdir
);
188 lstrcatW(buffer
, filespec
);
189 WINE_TRACE("Searching for files as '%s'\n", wine_dbgstr_w(buffer
));
191 /* Search for files in the location with the filespec supplied */
192 hff
= FindFirstFileW(buffer
, &fd
);
193 if (hff
!= INVALID_HANDLE_VALUE
) {
195 const WCHAR dot
[] = {'.', 0};
196 const WCHAR dotdot
[] = {'.', '.', 0};
198 WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd
.cFileName
));
200 if (!lstrcmpW(fd
.cFileName
, dot
) || !lstrcmpW(fd
.cFileName
, dotdot
))
203 if (!includedirs
&& (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
206 if (attrib_set
|| attrib_clear
) {
207 fd
.dwFileAttributes
&= ~attrib_clear
;
208 fd
.dwFileAttributes
|= attrib_set
;
209 if (!fd
.dwFileAttributes
)
210 fd
.dwFileAttributes
|= FILE_ATTRIBUTE_NORMAL
;
211 lstrcpyW(buffer
, rootdir
);
212 lstrcatW(buffer
, fd
.cFileName
);
213 SetFileAttributesW(buffer
, fd
.dwFileAttributes
);
216 static const WCHAR fmt
[] = {'%','1',' ',' ',' ',' ',' ','%','2','\n','\0'};
217 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
220 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
223 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
226 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
229 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
232 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
235 lstrcpyW(buffer
, rootdir
);
236 lstrcatW(buffer
, fd
.cFileName
);
237 ATTRIB_wprintf(fmt
, flags
, buffer
);
238 for (count
= 0; count
< (ARRAY_SIZE(flags
) - 1); count
++) flags
[count
] = ' ';
241 } while (FindNextFileW(hff
, &fd
) != 0);
247 int wmain(int argc
, WCHAR
*argv
[])
249 WCHAR name
[MAX_PATH
];
251 WCHAR curdir
[MAX_PATH
];
252 WCHAR originalname
[MAX_PATH
];
253 DWORD attrib_set
= 0;
254 DWORD attrib_clear
= 0;
255 BOOL attrib_recurse
= FALSE
;
256 BOOL attrib_includedirs
= FALSE
;
257 static const WCHAR help_option
[] = {'/','?','\0'};
258 static const WCHAR wildcardsW
[] = {'*','?','\0'};
262 if ((argc
>= 2) && !lstrcmpW(argv
[1], help_option
)) {
263 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP
));
267 /* By default all files from current directory are taken into account */
268 lstrcpyW(name
, starW
);
271 WCHAR
*param
= argv
[i
++];
272 WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param
));
273 if ((param
[0] == '+') || (param
[0] == '-')) {
276 case 'H': case 'h': attrib
|= FILE_ATTRIBUTE_HIDDEN
; break;
277 case 'S': case 's': attrib
|= FILE_ATTRIBUTE_SYSTEM
; break;
278 case 'R': case 'r': attrib
|= FILE_ATTRIBUTE_READONLY
; break;
279 case 'A': case 'a': attrib
|= FILE_ATTRIBUTE_ARCHIVE
; break;
281 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI
));
285 case '+': attrib_set
= attrib
; break;
286 case '-': attrib_clear
= attrib
; break;
288 } else if (param
[0] == '/') {
289 if (((param
[1] == 'D') || (param
[1] == 'd')) && !param
[2]) {
290 attrib_includedirs
= TRUE
;
291 } else if (((param
[1] == 'S') || (param
[1] == 's')) && !param
[2]) {
292 attrib_recurse
= TRUE
;
294 WINE_FIXME("Unknown option %s\n", debugstr_w(param
));
296 } else if (param
[0]) {
297 lstrcpyW(originalname
, param
);
301 /* Name may be a relative or explicit path, so calculate curdir based on
302 current locations, stripping off the filename */
303 WINE_TRACE("Supplied name: '%s'\n", wine_dbgstr_w(originalname
));
304 GetFullPathNameW(originalname
, ARRAY_SIZE(curdir
), curdir
, &namepart
);
305 WINE_TRACE("Result: '%s'\n", wine_dbgstr_w(curdir
));
307 lstrcpyW(name
, namepart
);
313 /* If a directory is explicitly supplied on the command line, and no
314 wildcards are in the name, then allow it to be changed/displayed */
315 if (wcspbrk(originalname
, wildcardsW
) == NULL
) attrib_includedirs
= TRUE
;
317 /* Do all the processing based on the filename arg */
318 found
= ATTRIB_processdirectory(curdir
, name
, attrib_recurse
,
319 attrib_includedirs
, attrib_set
, attrib_clear
);
321 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND
), originalname
);