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 /* =========================================================================
28 * Load a string from the resource file, handling any error
29 * Returns string retrieved from resource file
30 * ========================================================================= */
31 static WCHAR
*ATTRIB_LoadMessage(UINT id
)
33 static WCHAR msg
[MAXSTRING
];
35 if (!LoadStringW(GetModuleHandleW(NULL
), id
, msg
, ARRAY_SIZE(msg
))) {
36 WINE_FIXME("LoadString failed with %ld\n", GetLastError());
37 lstrcpyW(msg
, L
"Failed!");
42 /* =========================================================================
43 * Output a formatted unicode string. Ideally this will go to the console
44 * and hence required WriteConsoleW to output it, however if file i/o is
45 * redirected, it needs to be WriteFile'd using OEM (not ANSI) format
46 * ========================================================================= */
47 static int WINAPIV
ATTRIB_wprintf(const WCHAR
*format
, ...)
49 static WCHAR
*output_bufW
= NULL
;
50 static char *output_bufA
= NULL
;
51 static BOOL toConsole
= TRUE
;
52 static BOOL traceOutput
= FALSE
;
53 #define MAX_WRITECONSOLE_SIZE 65535
61 * Allocate buffer to use when writing to console
62 * Note: Not freed - memory will be allocated once and released when
66 if (!output_bufW
) output_bufW
= HeapAlloc(GetProcessHeap(), 0,
67 MAX_WRITECONSOLE_SIZE
*sizeof(WCHAR
));
69 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
73 va_start(parms
, format
);
74 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, format
, 0, 0, output_bufW
,
75 MAX_WRITECONSOLE_SIZE
/sizeof(*output_bufW
), &parms
);
77 if (len
== 0 && GetLastError() != ERROR_NO_WORK_DONE
) {
78 WINE_FIXME("Could not format string: le=%lu, fmt=%s\n", GetLastError(), wine_dbgstr_w(format
));
82 /* Try to write as unicode all the time we think it's a console */
84 res
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
85 output_bufW
, len
, &nOut
, NULL
);
88 /* If writing to console has failed (ever) we assume it's file
89 i/o so convert to OEM codepage and output */
91 BOOL usedDefaultChar
= FALSE
;
97 * Allocate buffer to use when writing to file. Not freed, as above
99 if (!output_bufA
) output_bufA
= HeapAlloc(GetProcessHeap(), 0,
100 MAX_WRITECONSOLE_SIZE
);
102 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
106 /* Convert to OEM, then output */
107 convertedChars
= WideCharToMultiByte(GetOEMCP(), 0, output_bufW
,
108 len
, output_bufA
, MAX_WRITECONSOLE_SIZE
,
109 "?", &usedDefaultChar
);
110 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), output_bufA
, convertedChars
,
114 /* Trace whether screen or console */
116 WINE_TRACE("Writing to console? (%d)\n", toConsole
);
122 /* =========================================================================
123 * Handle the processing for a single directory, optionally recursing into
124 * subdirectories if needed.
126 * rootdir [I] The directory to search in
127 * filespec [I] The filespec to search for
128 * recurse [I] Whether to recurse (search subdirectories before
130 * includedirs [I] Whether to set directory attributes as well
131 * attrib_set [I] Attributes to set
132 * attrib_clear [I] Attributes to clear
134 * Returns TRUE if at least one file displayed / modified
135 * ========================================================================= */
136 static BOOL
ATTRIB_processdirectory(const WCHAR
*rootdir
, const WCHAR
*filespec
,
137 BOOL recurse
, BOOL includedirs
,
138 DWORD attrib_set
, DWORD attrib_clear
)
141 WCHAR buffer
[MAX_PATH
];
144 WCHAR flags
[] = L
" ";
146 WINE_TRACE("Processing dir '%s', spec '%s', %d,%lx,%lx\n",
147 wine_dbgstr_w(rootdir
), wine_dbgstr_w(filespec
),
148 recurse
, attrib_set
, attrib_clear
);
152 /* Build spec to search for */
153 lstrcpyW(buffer
, rootdir
);
154 lstrcatW(buffer
, L
"*");
156 /* Search for directories in the location and recurse if necessary */
157 WINE_TRACE("Searching for directories with '%s'\n", wine_dbgstr_w(buffer
));
158 hff
= FindFirstFileW(buffer
, &fd
);
159 if (hff
!= INVALID_HANDLE_VALUE
) {
161 /* Only interested in directories, and not . nor .. */
162 if (!(fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ||
163 !lstrcmpW(fd
.cFileName
, L
".") || !lstrcmpW(fd
.cFileName
, L
".."))
166 /* Build new root dir to go searching in */
167 lstrcpyW(buffer
, rootdir
);
168 lstrcatW(buffer
, fd
.cFileName
);
169 lstrcatW(buffer
, L
"\\");
170 ATTRIB_processdirectory(buffer
, filespec
, recurse
, includedirs
,
171 attrib_set
, attrib_clear
);
173 } while (FindNextFileW(hff
, &fd
) != 0);
178 /* Build spec to search for */
179 lstrcpyW(buffer
, rootdir
);
180 lstrcatW(buffer
, filespec
);
181 WINE_TRACE("Searching for files as '%s'\n", wine_dbgstr_w(buffer
));
183 /* Search for files in the location with the filespec supplied */
184 hff
= FindFirstFileW(buffer
, &fd
);
185 if (hff
!= INVALID_HANDLE_VALUE
) {
188 WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd
.cFileName
));
190 if (!lstrcmpW(fd
.cFileName
, L
".") || !lstrcmpW(fd
.cFileName
, L
".."))
193 if (!includedirs
&& (fd
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
196 if (attrib_set
|| attrib_clear
) {
197 fd
.dwFileAttributes
&= ~attrib_clear
;
198 fd
.dwFileAttributes
|= attrib_set
;
199 if (!fd
.dwFileAttributes
)
200 fd
.dwFileAttributes
|= FILE_ATTRIBUTE_NORMAL
;
201 lstrcpyW(buffer
, rootdir
);
202 lstrcatW(buffer
, fd
.cFileName
);
203 SetFileAttributesW(buffer
, fd
.dwFileAttributes
);
206 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
209 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
212 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
215 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
218 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
221 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
224 lstrcpyW(buffer
, rootdir
);
225 lstrcatW(buffer
, fd
.cFileName
);
226 ATTRIB_wprintf(L
"%1 %2\n", flags
, buffer
);
227 for (count
= 0; count
< (ARRAY_SIZE(flags
) - 1); count
++) flags
[count
] = ' ';
230 } while (FindNextFileW(hff
, &fd
) != 0);
236 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
238 WCHAR name
[MAX_PATH
];
240 WCHAR curdir
[MAX_PATH
];
241 WCHAR originalname
[MAX_PATH
];
242 DWORD attrib_set
= 0;
243 DWORD attrib_clear
= 0;
244 BOOL attrib_recurse
= FALSE
;
245 BOOL attrib_includedirs
= FALSE
;
249 if ((argc
>= 2) && !lstrcmpW(argv
[1], L
"/?")) {
250 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP
));
254 /* By default all files from current directory are taken into account */
255 lstrcpyW(originalname
, L
".\\*");
258 WCHAR
*param
= argv
[i
++];
259 WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param
));
260 if ((param
[0] == '+') || (param
[0] == '-')) {
263 case 'H': case 'h': attrib
|= FILE_ATTRIBUTE_HIDDEN
; break;
264 case 'S': case 's': attrib
|= FILE_ATTRIBUTE_SYSTEM
; break;
265 case 'R': case 'r': attrib
|= FILE_ATTRIBUTE_READONLY
; break;
266 case 'A': case 'a': attrib
|= FILE_ATTRIBUTE_ARCHIVE
; break;
268 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI
));
272 case '+': attrib_set
= attrib
; break;
273 case '-': attrib_clear
= attrib
; break;
275 } else if (param
[0] == '/') {
276 if (((param
[1] == 'D') || (param
[1] == 'd')) && !param
[2]) {
277 attrib_includedirs
= TRUE
;
278 } else if (((param
[1] == 'S') || (param
[1] == 's')) && !param
[2]) {
279 attrib_recurse
= TRUE
;
281 WINE_FIXME("Unknown option %s\n", debugstr_w(param
));
283 } else if (param
[0]) {
284 lstrcpyW(originalname
, param
);
288 /* Name may be a relative or explicit path, so calculate curdir based on
289 current locations, stripping off the filename */
290 WINE_TRACE("Supplied name: '%s'\n", wine_dbgstr_w(originalname
));
291 GetFullPathNameW(originalname
, ARRAY_SIZE(curdir
), curdir
, &namepart
);
292 WINE_TRACE("Result: '%s'\n", wine_dbgstr_w(curdir
));
294 lstrcpyW(name
, namepart
);
300 /* If a directory is explicitly supplied on the command line, and no
301 wildcards are in the name, then allow it to be changed/displayed */
302 if (wcspbrk(originalname
, L
"*?") == NULL
) attrib_includedirs
= TRUE
;
304 /* Do all the processing based on the filename arg */
305 found
= ATTRIB_processdirectory(curdir
, name
, attrib_recurse
,
306 attrib_includedirs
, attrib_set
, attrib_clear
);
308 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND
), originalname
);