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 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 va_start(parms
, format
);
75 len
= vsnprintfW(output_bufW
, MAX_WRITECONSOLE_SIZE
/sizeof(WCHAR
), format
, parms
);
78 WINE_FIXME("String too long.\n");
82 /* Try to write as unicode all the time we think its a console */
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 */
91 BOOL usedDefaultChar
= 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
);
102 WINE_FIXME("Out of memory - could not allocate 2 x 64K buffers\n");
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
,
114 /* Trace whether screen or console */
116 WINE_TRACE("Writing to console? (%d)\n", toConsole
);
122 int wmain(int argc
, WCHAR
*argv
[])
127 WCHAR flags
[9] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
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
));
140 if (param
&& (param
[0] == '+' || param
[0] == '-')) {
142 /* FIXME: the real cmd can handle many more than two args; this should be in a loop */
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;
149 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI
));
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
);
165 strcpyW(name
, param
);
168 hff
= FindFirstFileW(name
, &fd
);
169 if (hff
== INVALID_HANDLE_VALUE
) {
170 ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND
), name
);
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
);
181 static const WCHAR fmt
[] = {'%','s',' ',' ',' ','%','s','\n','\0'};
182 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_HIDDEN
) {
185 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_SYSTEM
) {
188 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_ARCHIVE
) {
191 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) {
194 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_TEMPORARY
) {
197 if (fd
.dwFileAttributes
& FILE_ATTRIBUTE_COMPRESSED
) {
200 ATTRIB_wprintf(fmt
, flags
, fd
.cFileName
);
201 for (count
=0; count
< 8; count
++) flags
[count
] = ' ';
203 } while (FindNextFileW(hff
, &fd
) != 0);