1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
* ATTRIB - Wine-compatible attrib program
*
* Copyright 2010-2012 Christian Costa
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <windows.h>
#include <wine/debug.h>
#include "attrib.h"
WINE_DEFAULT_DEBUG_CHANNEL(attrib);
static const WCHAR starW[] = {'*','\0'};
/* =========================================================================
* Load a string from the resource file, handling any error
* Returns string retrieved from resource file
* ========================================================================= */
static WCHAR *ATTRIB_LoadMessage(UINT id)
{
static WCHAR msg[MAXSTRING];
const WCHAR failedMsg[] = {'F', 'a', 'i', 'l', 'e', 'd', '!', 0};
if (!LoadStringW(GetModuleHandleW(NULL), id, msg, ARRAY_SIZE(msg))) {
WINE_FIXME("LoadString failed with %d\n", GetLastError());
lstrcpyW(msg, failedMsg);
}
return msg;
}
/* =========================================================================
* Output a formatted unicode string. Ideally this will go to the console
* and hence required WriteConsoleW to output it, however if file i/o is
* redirected, it needs to be WriteFile'd using OEM (not ANSI) format
* ========================================================================= */
static int WINAPIV ATTRIB_wprintf(const WCHAR *format, ...)
{
static WCHAR *output_bufW = NULL;
static char *output_bufA = NULL;
static BOOL toConsole = TRUE;
static BOOL traceOutput = FALSE;
#define MAX_WRITECONSOLE_SIZE 65535
__ms_va_list parms;
DWORD nOut;
int len;
DWORD res = 0;
/*
* Allocate buffer to use when writing to console
* Note: Not freed - memory will be allocated once and released when
* attrib ends
*/
if (!output_bufW) output_bufW = HeapAlloc(GetProcessHeap(), 0,
MAX_WRITECONSOLE_SIZE*sizeof(WCHAR));
if (!output_bufW) {
WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
return 0;
}
__ms_va_start(parms, format);
SetLastError(NO_ERROR);
len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, format, 0, 0, output_bufW,
MAX_WRITECONSOLE_SIZE/sizeof(*output_bufW), &parms);
__ms_va_end(parms);
if (len == 0 && GetLastError() != NO_ERROR) {
WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(format));
return 0;
}
/* Try to write as unicode all the time we think it's a console */
if (toConsole) {
res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
output_bufW, len, &nOut, NULL);
}
/* If writing to console has failed (ever) we assume it's file
i/o so convert to OEM codepage and output */
if (!res) {
BOOL usedDefaultChar = FALSE;
DWORD convertedChars;
toConsole = FALSE;
/*
* Allocate buffer to use when writing to file. Not freed, as above
*/
if (!output_bufA) output_bufA = HeapAlloc(GetProcessHeap(), 0,
MAX_WRITECONSOLE_SIZE);
if (!output_bufA) {
WINE_FIXME("Out of memory - could not allocate 2 x 64 KB buffers\n");
return 0;
}
/* Convert to OEM, then output */
convertedChars = WideCharToMultiByte(GetConsoleOutputCP(), 0, output_bufW,
len, output_bufA, MAX_WRITECONSOLE_SIZE,
"?", &usedDefaultChar);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), output_bufA, convertedChars,
&nOut, FALSE);
}
/* Trace whether screen or console */
if (!traceOutput) {
WINE_TRACE("Writing to console? (%d)\n", toConsole);
traceOutput = TRUE;
}
return nOut;
}
/* =========================================================================
* Handle the processing for a single directory, optionally recursing into
* subdirectories if needed.
* Parameters:
* rootdir [I] The directory to search in
* filespec [I] The filespec to search for
* recurse [I] Whether to recurse (search subdirectories before
* current directory)
* includedirs [I] Whether to set directory attributes as well
* attrib_set [I] Attributes to set
* attrib_clear [I] Attributes to clear
*
* Returns TRUE if at least one file displayed / modified
* ========================================================================= */
static BOOL ATTRIB_processdirectory(const WCHAR *rootdir, const WCHAR *filespec,
BOOL recurse, BOOL includedirs,
DWORD attrib_set, DWORD attrib_clear)
{
BOOL found = FALSE;
WCHAR buffer[MAX_PATH];
HANDLE hff;
WIN32_FIND_DATAW fd;
WCHAR flags[] = {' ',' ',' ',' ',' ',' ',' ',' ','\0'};
static const WCHAR slashW[] = {'\\','\0'};
WINE_TRACE("Processing dir '%s', spec '%s', %d,%x,%x\n",
wine_dbgstr_w(rootdir), wine_dbgstr_w(filespec),
recurse, attrib_set, attrib_clear);
if (recurse) {
/* Build spec to search for */
lstrcpyW(buffer, rootdir);
lstrcatW(buffer, starW);
/* Search for directories in the location and recurse if necessary */
WINE_TRACE("Searching for directories with '%s'\n", wine_dbgstr_w(buffer));
hff = FindFirstFileW(buffer, &fd);
if (hff != INVALID_HANDLE_VALUE) {
do {
const WCHAR dot[] = {'.', 0};
const WCHAR dotdot[] = {'.', '.', 0};
/* Only interested in directories, and not . nor .. */
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
!lstrcmpW(fd.cFileName, dot) || !lstrcmpW(fd.cFileName, dotdot))
continue;
/* Build new root dir to go searching in */
lstrcpyW(buffer, rootdir);
lstrcatW(buffer, fd.cFileName);
lstrcatW(buffer, slashW);
ATTRIB_processdirectory(buffer, filespec, recurse, includedirs,
attrib_set, attrib_clear);
} while (FindNextFileW(hff, &fd) != 0);
}
FindClose (hff);
}
/* Build spec to search for */
lstrcpyW(buffer, rootdir);
lstrcatW(buffer, filespec);
WINE_TRACE("Searching for files as '%s'\n", wine_dbgstr_w(buffer));
/* Search for files in the location with the filespec supplied */
hff = FindFirstFileW(buffer, &fd);
if (hff != INVALID_HANDLE_VALUE) {
do {
const WCHAR dot[] = {'.', 0};
const WCHAR dotdot[] = {'.', '.', 0};
DWORD count;
WINE_TRACE("Found '%s'\n", wine_dbgstr_w(fd.cFileName));
if (!lstrcmpW(fd.cFileName, dot) || !lstrcmpW(fd.cFileName, dotdot))
continue;
if (!includedirs && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
if (attrib_set || attrib_clear) {
fd.dwFileAttributes &= ~attrib_clear;
fd.dwFileAttributes |= attrib_set;
if (!fd.dwFileAttributes)
fd.dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
lstrcpyW(buffer, rootdir);
lstrcatW(buffer, fd.cFileName);
SetFileAttributesW(buffer, fd.dwFileAttributes);
found = TRUE;
} else {
static const WCHAR fmt[] = {'%','1',' ',' ',' ',' ',' ','%','2','\n','\0'};
if (fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) {
flags[4] = 'H';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) {
flags[1] = 'S';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
flags[0] = 'A';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
flags[5] = 'R';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) {
flags[6] = 'T';
}
if (fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) {
flags[7] = 'C';
}
lstrcpyW(buffer, rootdir);
lstrcatW(buffer, fd.cFileName);
ATTRIB_wprintf(fmt, flags, buffer);
for (count = 0; count < (ARRAY_SIZE(flags) - 1); count++) flags[count] = ' ';
found = TRUE;
}
} while (FindNextFileW(hff, &fd) != 0);
}
FindClose (hff);
return found;
}
int __cdecl wmain(int argc, WCHAR *argv[])
{
WCHAR name[MAX_PATH];
WCHAR *namepart;
WCHAR curdir[MAX_PATH];
WCHAR originalname[MAX_PATH];
DWORD attrib_set = 0;
DWORD attrib_clear = 0;
BOOL attrib_recurse = FALSE;
BOOL attrib_includedirs = FALSE;
static const WCHAR help_option[] = {'/','?','\0'};
static const WCHAR wildcardsW[] = {'*','?','\0'};
int i = 1;
BOOL found = FALSE;
if ((argc >= 2) && !lstrcmpW(argv[1], help_option)) {
ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_HELP));
return 0;
}
/* By default all files from current directory are taken into account */
lstrcpyW(name, starW);
while (i < argc) {
WCHAR *param = argv[i++];
WINE_TRACE("Processing arg: '%s'\n", wine_dbgstr_w(param));
if ((param[0] == '+') || (param[0] == '-')) {
DWORD attrib = 0;
switch (param[1]) {
case 'H': case 'h': attrib |= FILE_ATTRIBUTE_HIDDEN; break;
case 'S': case 's': attrib |= FILE_ATTRIBUTE_SYSTEM; break;
case 'R': case 'r': attrib |= FILE_ATTRIBUTE_READONLY; break;
case 'A': case 'a': attrib |= FILE_ATTRIBUTE_ARCHIVE; break;
default:
ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_NYI));
return 0;
}
switch (param[0]) {
case '+': attrib_set = attrib; break;
case '-': attrib_clear = attrib; break;
}
} else if (param[0] == '/') {
if (((param[1] == 'D') || (param[1] == 'd')) && !param[2]) {
attrib_includedirs = TRUE;
} else if (((param[1] == 'S') || (param[1] == 's')) && !param[2]) {
attrib_recurse = TRUE;
} else {
WINE_FIXME("Unknown option %s\n", debugstr_w(param));
}
} else if (param[0]) {
lstrcpyW(originalname, param);
}
}
/* Name may be a relative or explicit path, so calculate curdir based on
current locations, stripping off the filename */
WINE_TRACE("Supplied name: '%s'\n", wine_dbgstr_w(originalname));
GetFullPathNameW(originalname, ARRAY_SIZE(curdir), curdir, &namepart);
WINE_TRACE("Result: '%s'\n", wine_dbgstr_w(curdir));
if (namepart) {
lstrcpyW(name, namepart);
*namepart = 0;
} else {
name[0] = 0;
}
/* If a directory is explicitly supplied on the command line, and no
wildcards are in the name, then allow it to be changed/displayed */
if (wcspbrk(originalname, wildcardsW) == NULL) attrib_includedirs = TRUE;
/* Do all the processing based on the filename arg */
found = ATTRIB_processdirectory(curdir, name, attrib_recurse,
attrib_includedirs, attrib_set, attrib_clear);
if (!found) {
ATTRIB_wprintf(ATTRIB_LoadMessage(STRING_FILENOTFOUND), originalname);
}
return 0;
}
|