include: Fix ASM_CFI definition.
[wine.git] / programs / attrib / attrib.c
blob73e2f60ce82118a8f1bed4c99b9e5fc770b988bb
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 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);
42 return msg;
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
58 __ms_va_list parms;
59 DWORD nOut;
60 int len;
61 DWORD res = 0;
64 * Allocate buffer to use when writing to console
65 * Note: Not freed - memory will be allocated once and released when
66 * attrib ends
69 if (!output_bufW) output_bufW = HeapAlloc(GetProcessHeap(), 0,
70 MAX_WRITECONSOLE_SIZE*sizeof(WCHAR));
71 if (!output_bufW) {
72 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
73 return 0;
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);
80 __ms_va_end(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));
83 return 0;
86 /* Try to write as unicode all the time we think it's a console */
87 if (toConsole) {
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 */
94 if (!res) {
95 BOOL usedDefaultChar = FALSE;
96 DWORD convertedChars;
98 toConsole = 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);
105 if (!output_bufA) {
106 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
107 return 0;
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,
115 &nOut, FALSE);
118 /* Trace whether screen or console */
119 if (!traceOutput) {
120 WINE_TRACE("Writing to console? (%d)\n", toConsole);
121 traceOutput = TRUE;
123 return nOut;
126 /* =========================================================================
127 * Handle the processing for a single directory, optionally recursing into
128 * subdirectories if needed.
129 * Parameters:
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
133 * current directory)
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)
144 BOOL found = FALSE;
145 WCHAR buffer[MAX_PATH];
146 HANDLE hff;
147 WIN32_FIND_DATAW fd;
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);
155 if (recurse) {
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) {
165 do {
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))
172 continue;
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);
183 FindClose (hff);
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) {
194 do {
195 const WCHAR dot[] = {'.', 0};
196 const WCHAR dotdot[] = {'.', '.', 0};
197 DWORD count;
198 WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd.cFileName));
200 if (!lstrcmpW(fd.cFileName, dot) || !lstrcmpW(fd.cFileName, dotdot))
201 continue;
203 if (!includedirs && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
204 continue;
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);
214 found = TRUE;
215 } else {
216 static const WCHAR fmt[] = {'%','1',' ',' ',' ',' ',' ','%','2','\n','\0'};
217 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
218 flags[4] = 'H';
220 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
221 flags[1] = 'S';
223 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
224 flags[0] = 'A';
226 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
227 flags[5] = 'R';
229 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
230 flags[6] = 'T';
232 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
233 flags[7] = 'C';
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] = ' ';
239 found = TRUE;
241 } while (FindNextFileW(hff, &fd) != 0);
243 FindClose (hff);
244 return found;
247 int wmain(int argc, WCHAR *argv[])
249 WCHAR name[MAX_PATH];
250 WCHAR *namepart;
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'};
259 int i = 1;
260 BOOL found = FALSE;
262 if ((argc >= 2) && !lstrcmpW(argv[1], help_option)) {
263 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
264 return 0;
267 /* By default all files from current directory are taken into account */
268 lstrcpyW(name, starW);
270 while (i < argc) {
271 WCHAR *param = argv[i++];
272 WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param));
273 if ((param[0] == '+') || (param[0] == '-')) {
274 DWORD attrib = 0;
275 switch (param[1]) {
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;
280 default:
281 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
282 return 0;
284 switch (param[0]) {
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;
293 } else {
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));
306 if (namepart) {
307 lstrcpyW(name, namepart);
308 *namepart = 0;
309 } else {
310 name[0] = 0;
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);
320 if (!found) {
321 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), originalname);
323 return 0;