msxml3/tests: Tests for IMXAttributes::clear().
[wine/multimedia.git] / programs / attrib / attrib.c
blobeb497ee54eae280eef706f937ac971f0a018a60b
1 /*
2 * ATTRIB - Wine-compatible attrib program
4 * Copyright 2010-2011 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 /* =========================================================================
29 * Load a string from the resource file, handling any error
30 * Returns string retrieved from resource file
31 * ========================================================================= */
32 static WCHAR *ATTRIB_LoadMessage(UINT id) {
33 static WCHAR msg[MAXSTRING];
34 const WCHAR failedMsg[] = {'F', 'a', 'i', 'l', 'e', 'd', '!', 0};
36 if (!LoadStringW(GetModuleHandleW(NULL), id, msg, sizeof(msg)/sizeof(WCHAR))) {
37 WINE_FIXME("LoadString failed with %d\n", GetLastError());
38 lstrcpyW(msg, failedMsg);
40 return msg;
43 /* =========================================================================
44 * Output a formatted unicode string. Ideally this will go to the console
45 * and hence required WriteConsoleW to output it, however if file i/o is
46 * redirected, it needs to be WriteFile'd using OEM (not ANSI) format
47 * ========================================================================= */
48 static int __cdecl ATTRIB_wprintf(const WCHAR *format, ...) {
50 static WCHAR *output_bufW = NULL;
51 static char *output_bufA = NULL;
52 static BOOL toConsole = TRUE;
53 static BOOL traceOutput = FALSE;
54 #define MAX_WRITECONSOLE_SIZE 65535
56 __ms_va_list parms;
57 DWORD nOut;
58 int len;
59 DWORD res = 0;
62 * Allocate buffer to use when writing to console
63 * Note: Not freed - memory will be allocated once and released when
64 * xcopy ends
67 if (!output_bufW) output_bufW = HeapAlloc(GetProcessHeap(), 0,
68 MAX_WRITECONSOLE_SIZE);
69 if (!output_bufW) {
70 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
71 return 0;
74 __ms_va_start(parms, format);
75 SetLastError(NO_ERROR);
76 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, output_bufW,
77 MAX_WRITECONSOLE_SIZE/sizeof(*output_bufW), &parms);
78 __ms_va_end(parms);
79 if (len == 0 && GetLastError() != NO_ERROR) {
80 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
81 return 0;
84 /* Try to write as unicode all the time we think its a console */
85 if (toConsole) {
86 res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
87 output_bufW, len, &nOut, NULL);
90 /* If writing to console has failed (ever) we assume its file
91 i/o so convert to OEM codepage and output */
92 if (!res) {
93 BOOL usedDefaultChar = FALSE;
94 DWORD convertedChars;
96 toConsole = FALSE;
99 * Allocate buffer to use when writing to file. Not freed, as above
101 if (!output_bufA) output_bufA = HeapAlloc(GetProcessHeap(), 0,
102 MAX_WRITECONSOLE_SIZE);
103 if (!output_bufA) {
104 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
105 return 0;
108 /* Convert to OEM, then output */
109 convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 0, output_bufW,
110 len, output_bufA, MAX_WRITECONSOLE_SIZE,
111 "?", &usedDefaultChar);
112 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), output_bufA, convertedChars,
113 &nOut, FALSE);
116 /* Trace whether screen or console */
117 if (!traceOutput) {
118 WINE_TRACE("Writing to console? (%d)\n", toConsole);
119 traceOutput = TRUE;
121 return nOut;
124 int wmain(int argc, WCHAR *argv[])
126 DWORD count;
127 HANDLE hff;
128 WIN32_FIND_DATAW fd;
129 WCHAR flags[] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
130 WCHAR name[128];
131 WCHAR *param = argc >= 2 ? argv[1] : NULL;
132 DWORD attrib_set = 0;
133 DWORD attrib_clear = 0;
134 WCHAR help_option[] = {'/','?','\0'};
136 if (param && !strcmpW(param, help_option))
138 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
139 return 0;
142 if (param && (param[0] == '+' || param[0] == '-')) {
143 DWORD attrib = 0;
144 /* FIXME: the real cmd can handle many more than two args; this should be in a loop */
145 switch (param[1]) {
146 case 'H': case 'h': attrib |= FILE_ATTRIBUTE_HIDDEN; break;
147 case 'S': case 's': attrib |= FILE_ATTRIBUTE_SYSTEM; break;
148 case 'R': case 'r': attrib |= FILE_ATTRIBUTE_READONLY; break;
149 case 'A': case 'a': attrib |= FILE_ATTRIBUTE_ARCHIVE; break;
150 default:
151 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
152 return 0;
154 switch (param[0]) {
155 case '+': attrib_set = attrib; break;
156 case '-': attrib_clear = attrib; break;
158 param = argc >= 3 ? argv[2] : NULL;
161 if (!param || strlenW(param) == 0) {
162 static const WCHAR slashStarW[] = {'\\','*','\0'};
164 GetCurrentDirectoryW(sizeof(name)/sizeof(WCHAR), name);
165 strcatW (name, slashStarW);
166 } else {
167 strcpyW(name, param);
170 hff = FindFirstFileW(name, &fd);
171 if (hff == INVALID_HANDLE_VALUE) {
172 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), name);
174 else {
175 do {
176 if (attrib_set || attrib_clear) {
177 fd.dwFileAttributes &= ~attrib_clear;
178 fd.dwFileAttributes |= attrib_set;
179 if (!fd.dwFileAttributes)
180 fd.dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
181 SetFileAttributesW(name, fd.dwFileAttributes);
182 } else {
183 static const WCHAR fmt[] = {'%','1',' ',' ',' ','%','2','\n','\0'};
184 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
185 flags[0] = 'H';
187 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
188 flags[1] = 'S';
190 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
191 flags[2] = 'A';
193 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
194 flags[3] = 'R';
196 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
197 flags[4] = 'T';
199 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
200 flags[5] = 'C';
202 ATTRIB_wprintf(fmt, flags, fd.cFileName);
203 for (count=0; count < 8; count++) flags[count] = ' ';
205 } while (FindNextFileW(hff, &fd) != 0);
207 FindClose (hff);
209 return 0;