po: Remove English strings from the Greek translation.
[wine] / programs / attrib / attrib.c
1 /*
2  * ATTRIB - Wine-compatible attrib program
3  *
4  * Copyright 2010-2011 Christian Costa
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <windows.h>
22 #include <wine/debug.h>
23 #include <wine/unicode.h>
24 #include "attrib.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(attrib);
27
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};
35
36     if (!LoadStringW(GetModuleHandleW(NULL), id, msg, sizeof(msg)/sizeof(WCHAR))) {
37         WINE_FIXME("LoadString failed with %d\n", GetLastError());
38         lstrcpyW(msg, failedMsg);
39     }
40     return msg;
41 }
42
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, ...) {
49
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
55
56     __ms_va_list parms;
57     DWORD   nOut;
58     int len;
59     DWORD   res = 0;
60
61     /*
62      * Allocate buffer to use when writing to console
63      * Note: Not freed - memory will be allocated once and released when
64      *         xcopy ends
65      */
66
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;
72     }
73
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;
82     }
83
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);
88     }
89
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;
95
96         toConsole = FALSE;
97
98         /*
99          * Allocate buffer to use when writing to file. Not freed, as above
100          */
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;
106         }
107
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);
114     }
115
116     /* Trace whether screen or console */
117     if (!traceOutput) {
118         WINE_TRACE("Writing to console? (%d)\n", toConsole);
119         traceOutput = TRUE;
120     }
121     return nOut;
122 }
123
124 int wmain(int argc, WCHAR *argv[])
125 {
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'};
135
136     if (param && !strcmpW(param, help_option))
137     {
138         ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
139         return 0;
140     }
141
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;
153         }
154         switch (param[0]) {
155         case '+': attrib_set = attrib; break;
156         case '-': attrib_clear = attrib; break;
157         }
158         param = argc >= 3 ? argv[2] : NULL;
159     }
160
161     if (!param || strlenW(param) == 0) {
162         static const WCHAR slashStarW[]  = {'\\','*','\0'};
163
164         GetCurrentDirectoryW(sizeof(name)/sizeof(WCHAR), name);
165         strcatW (name, slashStarW);
166     } else {
167         strcpyW(name, param);
168     }
169
170     hff = FindFirstFileW(name, &fd);
171     if (hff == INVALID_HANDLE_VALUE) {
172         ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), name);
173     }
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';
186                 }
187                 if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
188                     flags[1] = 'S';
189                 }
190                 if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
191                     flags[2] = 'A';
192                 }
193                 if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
194                     flags[3] = 'R';
195                 }
196                 if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
197                     flags[4] = 'T';
198                 }
199                 if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
200                     flags[5] = 'C';
201                 }
202                 ATTRIB_wprintf(fmt, flags, fd.cFileName);
203                 for (count=0; count < 8; count++) flags[count] = ' ';
204             }
205         } while (FindNextFileW(hff, &fd) != 0);
206     }
207     FindClose (hff);
208
209     return 0;
210 }