msscript: Implement SetClientSite()/GetClientSite().
[wine.git] / programs / attrib / attrib.c
blob0938cb8307d0af6c4ee548b424b342af365ffd05
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 <wine/unicode.h>
24 #include "attrib.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(attrib);
28 static const WCHAR starW[] = {'*','\0'};
30 /* =========================================================================
31 * Load a string from the resource file, handling any error
32 * Returns string retrieved from resource file
33 * ========================================================================= */
34 static WCHAR *ATTRIB_LoadMessage(UINT id)
36 static WCHAR msg[MAXSTRING];
37 const WCHAR failedMsg[] = {'F', 'a', 'i', 'l', 'e', 'd', '!', 0};
39 if (!LoadStringW(GetModuleHandleW(NULL), id, msg, sizeof(msg)/sizeof(WCHAR))) {
40 WINE_FIXME("LoadString failed with %d\n", GetLastError());
41 lstrcpyW(msg, failedMsg);
43 return msg;
46 /* =========================================================================
47 * Output a formatted unicode string. Ideally this will go to the console
48 * and hence required WriteConsoleW to output it, however if file i/o is
49 * redirected, it needs to be WriteFile'd using OEM (not ANSI) format
50 * ========================================================================= */
51 static int __cdecl ATTRIB_wprintf(const WCHAR *format, ...)
53 static WCHAR *output_bufW = NULL;
54 static char *output_bufA = NULL;
55 static BOOL toConsole = TRUE;
56 static BOOL traceOutput = FALSE;
57 #define MAX_WRITECONSOLE_SIZE 65535
59 __ms_va_list parms;
60 DWORD nOut;
61 int len;
62 DWORD res = 0;
65 * Allocate buffer to use when writing to console
66 * Note: Not freed - memory will be allocated once and released when
67 * attrib ends
70 if (!output_bufW) output_bufW = HeapAlloc(GetProcessHeap(), 0,
71 MAX_WRITECONSOLE_SIZE*sizeof(WCHAR));
72 if (!output_bufW) {
73 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
74 return 0;
77 __ms_va_start(parms, format);
78 SetLastError(NO_ERROR);
79 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, output_bufW,
80 MAX_WRITECONSOLE_SIZE/sizeof(*output_bufW), &parms);
81 __ms_va_end(parms);
82 if (len == 0 && GetLastError() != NO_ERROR) {
83 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
84 return 0;
87 /* Try to write as unicode all the time we think it's a console */
88 if (toConsole) {
89 res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
90 output_bufW, len, &nOut, NULL);
93 /* If writing to console has failed (ever) we assume it's file
94 i/o so convert to OEM codepage and output */
95 if (!res) {
96 BOOL usedDefaultChar = FALSE;
97 DWORD convertedChars;
99 toConsole = FALSE;
102 * Allocate buffer to use when writing to file. Not freed, as above
104 if (!output_bufA) output_bufA = HeapAlloc(GetProcessHeap(), 0,
105 MAX_WRITECONSOLE_SIZE);
106 if (!output_bufA) {
107 WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
108 return 0;
111 /* Convert to OEM, then output */
112 convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 0, output_bufW,
113 len, output_bufA, MAX_WRITECONSOLE_SIZE,
114 "?", &usedDefaultChar);
115 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), output_bufA, convertedChars,
116 &nOut, FALSE);
119 /* Trace whether screen or console */
120 if (!traceOutput) {
121 WINE_TRACE("Writing to console? (%d)\n", toConsole);
122 traceOutput = TRUE;
124 return nOut;
127 /* =========================================================================
128 * Handle the processing for a single directory, optionally recursing into
129 * subdirectories if needed.
130 * Parameters:
131 * rootdir [I] The directory to search in
132 * filespec [I] The filespec to search for
133 * recurse [I] Whether to recurse (search subdirectories before
134 * current directory)
135 * includedirs [I] Whether to set directory attributes as well
136 * attrib_set [I] Attributes to set
137 * attrib_clear [I] Attributes to clear
139 * Returns TRUE if at least one file displayed / modified
140 * ========================================================================= */
141 static BOOL ATTRIB_processdirectory(const WCHAR *rootdir, const WCHAR *filespec,
142 BOOL recurse, BOOL includedirs,
143 DWORD attrib_set, DWORD attrib_clear)
145 BOOL found = FALSE;
146 WCHAR buffer[MAX_PATH];
147 HANDLE hff;
148 WIN32_FIND_DATAW fd;
149 WCHAR flags[] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
150 static const WCHAR slashW[] = {'\\','\0'};
152 WINE_TRACE("Processing dir '%s', spec '%s', %d,%x,%x\n",
153 wine_dbgstr_w(rootdir), wine_dbgstr_w(filespec),
154 recurse, attrib_set, attrib_clear);
156 if (recurse) {
158 /* Build spec to search for */
159 strcpyW(buffer, rootdir);
160 strcatW(buffer, starW);
162 /* Search for directories in the location and recurse if necessary */
163 WINE_TRACE("Searching for directories with '%s'\n", wine_dbgstr_w(buffer));
164 hff = FindFirstFileW(buffer, &fd);
165 if (hff != INVALID_HANDLE_VALUE) {
166 do {
167 const WCHAR dot[] = {'.', 0};
168 const WCHAR dotdot[] = {'.', '.', 0};
170 /* Only interested in directories, and not . nor .. */
171 if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
172 !strcmpW(fd.cFileName, dot) || !strcmpW(fd.cFileName, dotdot))
173 continue;
175 /* Build new root dir to go searching in */
176 strcpyW(buffer, rootdir);
177 strcatW(buffer, fd.cFileName);
178 strcatW(buffer, slashW);
179 ATTRIB_processdirectory(buffer, filespec, recurse, includedirs,
180 attrib_set, attrib_clear);
182 } while (FindNextFileW(hff, &fd) != 0);
184 FindClose (hff);
187 /* Build spec to search for */
188 strcpyW(buffer, rootdir);
189 strcatW(buffer, filespec);
190 WINE_TRACE("Searching for files as '%s'\n", wine_dbgstr_w(buffer));
192 /* Search for files in the location with the filespec supplied */
193 hff = FindFirstFileW(buffer, &fd);
194 if (hff != INVALID_HANDLE_VALUE) {
195 do {
196 const WCHAR dot[] = {'.', 0};
197 const WCHAR dotdot[] = {'.', '.', 0};
198 DWORD count;
199 WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd.cFileName));
201 if (!strcmpW(fd.cFileName, dot) || !strcmpW(fd.cFileName, dotdot))
202 continue;
204 if (!includedirs && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
205 continue;
207 if (attrib_set || attrib_clear) {
208 fd.dwFileAttributes &= ~attrib_clear;
209 fd.dwFileAttributes |= attrib_set;
210 if (!fd.dwFileAttributes)
211 fd.dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
212 strcpyW(buffer, rootdir);
213 strcatW(buffer, fd.cFileName);
214 SetFileAttributesW(buffer, fd.dwFileAttributes);
215 found = TRUE;
216 } else {
217 static const WCHAR fmt[] = {'%','1',' ',' ',' ',' ',' ','%','2','\n','\0'};
218 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
219 flags[4] = 'H';
221 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
222 flags[1] = 'S';
224 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
225 flags[0] = 'A';
227 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
228 flags[5] = 'R';
230 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
231 flags[6] = 'T';
233 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
234 flags[7] = 'C';
236 strcpyW(buffer, rootdir);
237 strcatW(buffer, fd.cFileName);
238 ATTRIB_wprintf(fmt, flags, buffer);
239 for (count = 0; count < (sizeof(flags)/sizeof(WCHAR) - 1); count++) flags[count] = ' ';
240 found = TRUE;
242 } while (FindNextFileW(hff, &fd) != 0);
244 FindClose (hff);
245 return found;
248 int wmain(int argc, WCHAR *argv[])
250 WCHAR name[MAX_PATH];
251 WCHAR *namepart;
252 WCHAR curdir[MAX_PATH];
253 WCHAR originalname[MAX_PATH];
254 DWORD attrib_set = 0;
255 DWORD attrib_clear = 0;
256 BOOL attrib_recurse = FALSE;
257 BOOL attrib_includedirs = FALSE;
258 static const WCHAR help_option[] = {'/','?','\0'};
259 static const WCHAR wildcardsW[] = {'*','?','\0'};
260 int i = 1;
261 BOOL found = FALSE;
263 if ((argc >= 2) && !strcmpW(argv[1], help_option)) {
264 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
265 return 0;
268 /* By default all files from current directory are taken into account */
269 strcpyW(name, starW);
271 while (i < argc) {
272 WCHAR *param = argv[i++];
273 WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param));
274 if ((param[0] == '+') || (param[0] == '-')) {
275 DWORD attrib = 0;
276 switch (param[1]) {
277 case 'H': case 'h': attrib |= FILE_ATTRIBUTE_HIDDEN; break;
278 case 'S': case 's': attrib |= FILE_ATTRIBUTE_SYSTEM; break;
279 case 'R': case 'r': attrib |= FILE_ATTRIBUTE_READONLY; break;
280 case 'A': case 'a': attrib |= FILE_ATTRIBUTE_ARCHIVE; break;
281 default:
282 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
283 return 0;
285 switch (param[0]) {
286 case '+': attrib_set = attrib; break;
287 case '-': attrib_clear = attrib; break;
289 } else if (param[0] == '/') {
290 if (((param[1] == 'D') || (param[1] == 'd')) && !param[2]) {
291 attrib_includedirs = TRUE;
292 } else if (((param[1] == 'S') || (param[1] == 's')) && !param[2]) {
293 attrib_recurse = TRUE;
294 } else {
295 WINE_FIXME("Unknown option %s\n", debugstr_w(param));
297 } else if (param[0]) {
298 strcpyW(originalname, param);
302 /* Name may be a relative or explicit path, so calculate curdir based on
303 current locations, stripping off the filename */
304 WINE_TRACE("Supplied name: '%s'\n", wine_dbgstr_w(originalname));
305 GetFullPathNameW(originalname, sizeof(curdir)/sizeof(WCHAR), curdir, &namepart);
306 WINE_TRACE("Result: '%s'\n", wine_dbgstr_w(curdir));
307 if (namepart) {
308 strcpyW(name, namepart);
309 *namepart = 0;
310 } else {
311 name[0] = 0;
314 /* If a directory is explicitly supplied on the command line, and no
315 wildcards are in the name, then allow it to be changed/displayed */
316 if (strpbrkW(originalname, wildcardsW) == NULL) attrib_includedirs = TRUE;
318 /* Do all the processing based on the filename arg */
319 found = ATTRIB_processdirectory(curdir, name, attrib_recurse,
320 attrib_includedirs, attrib_set, attrib_clear);
321 if (!found) {
322 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), originalname);
324 return 0;