mshtml: Add stub for QUERY_IS_CACHED_AND_USABLE_OFFLINE.
[wine/multimedia.git] / programs / attrib / attrib.c
blob0ca64c319b58aab4791f9ae519dc1c8cd0df0116
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 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 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 va_start(parms, format);
75 len = vsnprintfW(output_bufW, MAX_WRITECONSOLE_SIZE/sizeof(WCHAR), format, parms);
76 va_end(parms);
77 if (len < 0) {
78 WINE_FIXME("String too long.\n");
79 return 0;
82 /* Try to write as unicode all the time we think its 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 its 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 64K buffers\n");
103 return 0;
106 /* Convert to OEM, then output */
107 convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 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 int wmain(int argc, WCHAR *argv[])
124 DWORD count;
125 HANDLE hff;
126 WIN32_FIND_DATAW fd;
127 WCHAR flags[9] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
128 WCHAR name[128];
129 WCHAR *param = argc >= 2 ? argv[1] : NULL;
130 DWORD attrib_set = 0;
131 DWORD attrib_clear = 0;
132 WCHAR help_option[] = {'/','?','\0'};
134 if (param && !strcmpW(param, help_option))
136 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
137 return 0;
140 if (param && (param[0] == '+' || param[0] == '-')) {
141 DWORD attrib = 0;
142 /* FIXME: the real cmd can handle many more than two args; this should be in a loop */
143 switch (param[1]) {
144 case 'H': case 'h': attrib |= FILE_ATTRIBUTE_HIDDEN; break;
145 case 'S': case 's': attrib |= FILE_ATTRIBUTE_SYSTEM; break;
146 case 'R': case 'r': attrib |= FILE_ATTRIBUTE_READONLY; break;
147 case 'A': case 'a': attrib |= FILE_ATTRIBUTE_ARCHIVE; break;
148 default:
149 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
150 return 0;
152 switch (param[0]) {
153 case '+': attrib_set = attrib; break;
154 case '-': attrib_clear = attrib; break;
156 param = argc >= 3 ? argv[2] : NULL;
159 if (!param || strlenW(param) == 0) {
160 static const WCHAR slashStarW[] = {'\\','*','\0'};
162 GetCurrentDirectoryW(sizeof(name)/sizeof(WCHAR), name);
163 strcatW (name, slashStarW);
164 } else {
165 strcpyW(name, param);
168 hff = FindFirstFileW(name, &fd);
169 if (hff == INVALID_HANDLE_VALUE) {
170 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), name);
172 else {
173 do {
174 if (attrib_set || attrib_clear) {
175 fd.dwFileAttributes &= ~attrib_clear;
176 fd.dwFileAttributes |= attrib_set;
177 if (!fd.dwFileAttributes)
178 fd.dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
179 SetFileAttributesW(name, fd.dwFileAttributes);
180 } else {
181 static const WCHAR fmt[] = {'%','s',' ',' ',' ','%','s','\n','\0'};
182 if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
183 flags[0] = 'H';
185 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
186 flags[1] = 'S';
188 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
189 flags[2] = 'A';
191 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
192 flags[3] = 'R';
194 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
195 flags[4] = 'T';
197 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
198 flags[5] = 'C';
200 ATTRIB_wprintf(fmt, flags, fd.cFileName);
201 for (count=0; count < 8; count++) flags[count] = ' ';
203 } while (FindNextFileW(hff, &fd) != 0);
205 FindClose (hff);
207 return 0;