include: Fix a typo in _InterlockedXor64() intrinsic declaration.
[wine.git] / programs / attrib / attrib.c
blobf4bcc7df779056e0b4e199d5e01e382fef9b0f19
1 /*
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
21 #include <windows.h>
22 #include <wine/debug.h>
23 #include "attrib.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!");
39 return msg;
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
55 va_list parms;
56 DWORD nOut;
57 int len;
58 DWORD res = 0;
61 * Allocate buffer to use when writing to console
62 * Note: Not freed - memory will be allocated once and released when
63 * attrib ends
66 if (!output_bufW) output_bufW = HeapAlloc(GetProcessHeap(), 0,
67 MAX_WRITECONSOLE_SIZE*sizeof(WCHAR));
68 if (!output_bufW) {
69 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
70 return 0;
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);
76 va_end(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));
79 return 0;
82 /* Try to write as unicode all the time we think it's a console */
83 if (toConsole) {
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 */
90 if (!res) {
91 BOOL usedDefaultChar = FALSE;
92 DWORD convertedChars;
94 toConsole = 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);
101 if (!output_bufA) {
102 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
103 return 0;
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,
111 &nOut, FALSE);
114 /* Trace whether screen or console */
115 if (!traceOutput) {
116 WINE_TRACE("Writing to console? (%d)\n", toConsole);
117 traceOutput = TRUE;
119 return nOut;
122 /* =========================================================================
123 * Handle the processing for a single directory, optionally recursing into
124 * subdirectories if needed.
125 * Parameters:
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
129 * current directory)
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)
140 BOOL found = FALSE;
141 WCHAR buffer[MAX_PATH];
142 HANDLE hff;
143 WIN32_FIND_DATAW fd;
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);
150 if (recurse) {
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) {
160 do {
161 /* Only interested in directories, and not . nor .. */
162 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
163 !lstrcmpW(fd.cFileName, L".") || !lstrcmpW(fd.cFileName, L".."))
164 continue;
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);
175 FindClose (hff);
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) {
186 do {
187 DWORD count;
188 WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd.cFileName));
190 if (!lstrcmpW(fd.cFileName, L".") || !lstrcmpW(fd.cFileName, L".."))
191 continue;
193 if (!includedirs && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
194 continue;
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);
204 found = TRUE;
205 } else {
206 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
207 flags[4] = 'H';
209 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
210 flags[1] = 'S';
212 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
213 flags[0] = 'A';
215 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
216 flags[5] = 'R';
218 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
219 flags[6] = 'T';
221 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
222 flags[7] = 'C';
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] = ' ';
228 found = TRUE;
230 } while (FindNextFileW(hff, &fd) != 0);
232 FindClose (hff);
233 return found;
236 int __cdecl wmain(int argc, WCHAR *argv[])
238 WCHAR name[MAX_PATH];
239 WCHAR *namepart;
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;
246 int i = 1;
247 BOOL found = FALSE;
249 if ((argc >= 2) && !lstrcmpW(argv[1], L"/?")) {
250 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
251 return 0;
254 /* By default all files from current directory are taken into account */
255 lstrcpyW(originalname, L".\\*");
257 while (i < argc) {
258 WCHAR *param = argv[i++];
259 WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param));
260 if ((param[0] == '+') || (param[0] == '-')) {
261 DWORD attrib = 0;
262 switch (param[1]) {
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;
267 default:
268 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
269 return 0;
271 switch (param[0]) {
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;
280 } else {
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));
293 if (namepart) {
294 lstrcpyW(name, namepart);
295 *namepart = 0;
296 } else {
297 name[0] = 0;
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);
307 if (!found) {
308 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), originalname);
310 return 0;