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
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
)
34 static WCHAR msg
[MAXSTRING
];
35 const WCHAR failedMsg
[] = {'F', 'a', 'i', 'l', 'e', 'd', '!', 0};
37 if (!LoadStringW(GetModuleHandleW(NULL
), id
, msg
, sizeof(msg
)/sizeof(WCHAR
))) {
38 WINE_FIXME("LoadString failed with %d\n", GetLastError());
39 lstrcpyW(msg
, failedMsg
);
44 /* =========================================================================
45 * Output a formatted unicode string. Ideally this will go to the console
46 * and hence required WriteConsoleW to output it, however if file i/o is
47 * redirected, it needs to be WriteFile'd using OEM (not ANSI) format
48 * ========================================================================= */
49 static int __cdecl
ATTRIB_wprintf(const WCHAR
*format
, ...)
51 static WCHAR
*output_bufW
= NULL
;
52 static char *output_bufA
= NULL
;
53 static BOOL toConsole
= TRUE
;
54 static BOOL traceOutput
= FALSE
;
55 #define MAX_WRITECONSOLE_SIZE 65535
63 * Allocate buffer to use when writing to console
64 * Note: Not freed - memory will be allocated once and released when
68 if (!output_bufW
) output_bufW
= HeapAlloc(GetProcessHeap(), 0,
69 MAX_WRITECONSOLE_SIZE
);
71 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
75 __ms_va_start(parms
, format
);
76 SetLastError(NO_ERROR
);
77 len
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, format
, 0, 0, output_bufW
,
78 MAX_WRITECONSOLE_SIZE
/sizeof(*output_bufW
), &parms
);
80 if (len
== 0 && GetLastError() != NO_ERROR
) {
81 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format
));
85 /* Try to write as unicode all the time we think its a console */
87 res
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
),
88 output_bufW
, len
, &nOut
, NULL
);
91 /* If writing to console has failed (ever) we assume its file
92 i/o so convert to OEM codepage and output */
94 BOOL usedDefaultChar
= FALSE
;
100 * Allocate buffer to use when writing to file. Not freed, as above
102 if (!output_bufA
) output_bufA
= HeapAlloc(GetProcessHeap(), 0,
103 MAX_WRITECONSOLE_SIZE
);
105 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
109 /* Convert to OEM, then output */
110 convertedChars
= WideCharToMultiByte(GetConsoleOutputCP(), 0, output_bufW
,
111 len
, output_bufA
, MAX_WRITECONSOLE_SIZE
,
112 "?", &usedDefaultChar
);
113 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), output_bufA
, convertedChars
,
117 /* Trace whether screen or console */
119 WINE_TRACE("Writing to console? (%d)\n", toConsole
);
125 int wmain(int argc
, WCHAR
*argv
[])
130 WCHAR flags
[] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
132 DWORD attrib_set
= 0;
133 DWORD attrib_clear
= 0;
134 const WCHAR help_option
[] = {'/','?','\0'};
135 const WCHAR slashStarW
[] = {'\\','*','\0'};
138 if ((argc
>= 2) && !strcmpW(argv
[1], help_option
)) {
139 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP
));
143 /* By default all files from current directory are taken into account */
144 GetCurrentDirectoryW(sizeof(name
)/sizeof(WCHAR
), name
);
145 strcatW (name
, slashStarW
);
148 WCHAR
*param
= argv
[i
++];
149 if ((param
[0] == '+') || (param
[0] == '-')) {
152 case 'H': case 'h': attrib
|= FILE_ATTRIBUTE_HIDDEN
; break;
153 case 'S': case 's': attrib
|= FILE_ATTRIBUTE_SYSTEM
; break;
154 case 'R': case 'r': attrib
|= FILE_ATTRIBUTE_READONLY
; break;
155 case 'A': case 'a': attrib
|= FILE_ATTRIBUTE_ARCHIVE
; break;
157 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI
));
161 case '+': attrib_set
= attrib
; break;
162 case '-': attrib_clear
= attrib
; break;
164 } else if (param
[0] == '/') {
165 if (((param
[1] == 'D') || (param
[1] == 'd')) && !param
[2]) {
166 WINE_FIXME("Option /D not yet supported\n");
167 } else if (((param
[1] == 'R') || (param
[1] == 'r')) && !param
[2]) {
168 WINE_FIXME("Option /R not yet supported\n");
170 WINE_FIXME("Unrecognized option\n");
172 } else if (param
[0]) {
173 strcpyW(name
, param
);
177 hff
= FindFirstFileW(name
, &fd
);
178 if (hff
== INVALID_HANDLE_VALUE
) {
179 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND
), name
);
183 if (attrib_set
|| attrib_clear
) {
184 fd
.dwFileAttributes
&= ~attrib_clear
;
185 fd
.dwFileAttributes
|= attrib_set
;
186 if (!fd
.dwFileAttributes
)
187 fd
.dwFileAttributes
|= FILE_ATTRIBUTE_NORMAL
;
188 SetFileAttributesW(name
, fd
.dwFileAttributes
);
190 static const WCHAR fmt
[] = {'%','1',' ',' ',' ','%','2','\n','\0'};
191 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
194 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
197 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
200 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
203 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
206 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
209 ATTRIB_wprintf(fmt
, flags
, fd
.cFileName
);
210 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
212 } while (FindNextFileW(hff
, &fd
) != 0);