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
22 #include <wine/debug.h>
23 #include <wine/unicode.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
);
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
62 * Allocate buffer to use when writing to console
63 * Note: Not freed - memory will be allocated once and released when
67 if (!output_bufW
) output_bufW
= HeapAlloc(GetProcessHeap(), 0,
68 MAX_WRITECONSOLE_SIZE
);
70 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
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
);
79 if (len
== 0 && GetLastError() != NO_ERROR
) {
80 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format
));
84 /* Try to write as unicode all the time we think its a console */
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 */
93 BOOL usedDefaultChar
= 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
);
104 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
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
,
116 /* Trace whether screen or console */
118 WINE_TRACE("Writing to console? (%d)\n", toConsole
);
124 int wmain(int argc
, WCHAR
*argv
[])
129 WCHAR flags
[9] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
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
));
142 if (param
&& (param
[0] == '+' || param
[0] == '-')) {
144 /* FIXME: the real cmd can handle many more than two args; this should be in a loop */
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;
151 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI
));
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
);
167 strcpyW(name
, param
);
170 hff
= FindFirstFileW(name
, &fd
);
171 if (hff
== INVALID_HANDLE_VALUE
) {
172 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND
), name
);
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
);
183 static const WCHAR fmt
[] = {'%','1',' ',' ',' ','%','2','\n','\0'};
184 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
187 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
190 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
193 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
196 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
199 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
202 ATTRIB_wprintf(fmt
, flags
, fd
.cFileName
);
203 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
205 } while (FindNextFileW(hff
, &fd
) != 0);