ReactOS 0.4.15-dev-6073-g59e7458
attrib.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <windef.h>
#include <winbase.h>
#include <wincon.h>
#include <winuser.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for attrib.c:

Go to the source code of this file.

Functions

static VOID ErrorMessage (_In_ DWORD dwErrorCode, _In_opt_ PCWSTR pszMsg,...)
 
static BOOL PrintAttribute (LPWSTR pszPath, LPWSTR pszFile, BOOL bRecurse, BOOL bDirectories)
 
static BOOL ChangeAttribute (LPWSTR pszPath, LPWSTR pszFile, BOOL bRecurse, BOOL bDirectories, DWORD dwMask, DWORD dwAttrib)
 
int wmain (int argc, WCHAR *argv[])
 

Variables

CON_SCREEN StdOutScreen = INIT_CON_SCREEN(StdOut)
 

Function Documentation

◆ ChangeAttribute()

static BOOL ChangeAttribute ( LPWSTR  pszPath,
LPWSTR  pszFile,
BOOL  bRecurse,
BOOL  bDirectories,
DWORD  dwMask,
DWORD  dwAttrib 
)
static

Definition at line 184 of file attrib.c.

191{
192 WIN32_FIND_DATAW findData;
193 HANDLE hFind;
194 WCHAR szFullName[MAX_PATH];
196 BOOL bFound = FALSE;
197 BOOL bIsDir;
198 BOOL bExactMatch;
199 DWORD dwAttribute;
200 DWORD Error;
201
202 /* prepare full file name buffer */
203 wcscpy(szFullName, pszPath);
204 pszFileName = szFullName + wcslen(szFullName);
205
206 /* display all subdirectories */
207 if (bRecurse)
208 {
209 /* append *.* */
210 wcscpy(pszFileName, L"*.*");
211
212 hFind = FindFirstFileW(szFullName, &findData);
213 if (hFind == INVALID_HANDLE_VALUE)
214 {
218 {
219 ErrorMessage(Error, pszFile);
220 }
221 return FALSE;
222 }
223
224 do
225 {
226 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
227 continue;
228
229 if (!wcscmp(findData.cFileName, L".") ||
230 !wcscmp(findData.cFileName, L".."))
231 {
232 continue;
233 }
234
235 wcscpy(pszFileName, findData.cFileName);
236 wcscat(pszFileName, L"\\");
237 bFound |= ChangeAttribute(szFullName, pszFile, bRecurse, bDirectories,
238 dwMask, dwAttrib);
239 }
240 while (FindNextFileW(hFind, &findData));
241 FindClose(hFind);
242 }
243
244 /* append file name */
245 wcscpy(pszFileName, pszFile);
246
247 /* search current directory */
248 hFind = FindFirstFileW(szFullName, &findData);
249 if (hFind == INVALID_HANDLE_VALUE)
250 {
251 return bFound;
252 }
253
254 do
255 {
256 bIsDir = findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
257 bExactMatch = wcsicmp(findData.cFileName, pszFile) == 0;
258
259 if (bIsDir && !bDirectories && !bExactMatch)
260 continue;
261
262 if (!wcscmp(findData.cFileName, L".") ||
263 !wcscmp(findData.cFileName, L".."))
264 {
265 continue;
266 }
267
268 if (bRecurse && bIsDir && !bDirectories)
269 continue;
270
271 wcscpy(pszFileName, findData.cFileName);
272
273 dwAttribute = (findData.dwFileAttributes & ~dwMask) | dwAttrib;
274
275 SetFileAttributes(szFullName, dwAttribute);
276 bFound = TRUE;
277 }
278 while (FindNextFileW(hFind, &findData));
279 FindClose(hFind);
280
281 return bFound;
282}
static VOID ErrorMessage(_In_ DWORD dwErrorCode, _In_opt_ PCWSTR pszMsg,...)
Definition: attrib.c:51
static BOOL ChangeAttribute(LPWSTR pszPath, LPWSTR pszFile, BOOL bRecurse, BOOL bDirectories, DWORD dwMask, DWORD dwAttrib)
Definition: attrib.c:184
BOOL Error
Definition: chkdsk.c:66
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
#define wcsicmp
Definition: compat.h:15
HANDLE WINAPI FindFirstFileW(IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:320
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
BOOL WINAPI FindNextFileW(IN HANDLE hFindFile, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:382
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define L(x)
Definition: ntvdm.h:50
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
WORD WORD PSZ PSZ pszFileName
Definition: vdmdbg.h:44
DWORD WINAPI GetLastError(void)
Definition: except.c:1040
#define SetFileAttributes
Definition: winbase.h:3780
#define ERROR_SHARING_VIOLATION
Definition: winerror.h:135
#define ERROR_DIRECTORY
Definition: winerror.h:295
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184

Referenced by ChangeAttribute(), and wmain().

◆ ErrorMessage()

static VOID ErrorMessage ( _In_ DWORD  dwErrorCode,
_In_opt_ PCWSTR  pszMsg,
  ... 
)
static

Definition at line 51 of file attrib.c.

55{
56 INT Len;
57 va_list arg_ptr;
58
59 if (dwErrorCode == ERROR_SUCCESS)
60 return;
61
62 va_start(arg_ptr, pszMsg);
65 NULL,
66 dwErrorCode,
68 &arg_ptr);
69 va_end(arg_ptr);
70
71 /* Fall back just in case the error is not defined */
72 if (Len <= 0)
74
75 /* Display the extra optional message if necessary */
76 if (pszMsg)
77 ConPrintf(StdErr, L" %s\n", pszMsg);
78}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define STRING_CONSOLE_ERROR
Definition: resource.h:6
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define StdErr
Definition: fc.c:15
#define Len
Definition: deflate.h:82
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
INT ConMsgPrintfV(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId, IN va_list *Arguments OPTIONAL)
Definition: outstream.c:1030
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
int32_t INT
Definition: typedefs.h:58
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:423

Referenced by AddAssociation(), ChangeAttribute(), cmd_mkdir(), cmd_mklink(), cmd_move(), cmd_rmdir(), cmd_start(), DeleteFiles(), DeleteFolder(), ExecuteAsync(), PrintAllAssociations(), PrintAssociation(), PrintAssociationEx(), PrintAttribute(), PrintVolumeHeader(), ProcessPortMessage(), RemoveAssociation(), ShowItemError(), and ShowStepError().

◆ PrintAttribute()

static BOOL PrintAttribute ( LPWSTR  pszPath,
LPWSTR  pszFile,
BOOL  bRecurse,
BOOL  bDirectories 
)
static

Definition at line 83 of file attrib.c.

88{
89 WIN32_FIND_DATAW findData;
90 HANDLE hFind;
91 WCHAR szFullName[MAX_PATH];
93 BOOL bFound = FALSE;
94 BOOL bIsDir;
95 BOOL bExactMatch;
97
98 /* prepare full file name buffer */
99 wcscpy(szFullName, pszPath);
100 pszFileName = szFullName + wcslen(szFullName);
101
102 /* display all subdirectories */
103 if (bRecurse)
104 {
105 /* append *.* */
106 wcscpy(pszFileName, L"*.*");
107
108 hFind = FindFirstFileW(szFullName, &findData);
109 if (hFind == INVALID_HANDLE_VALUE)
110 {
114 {
115 ErrorMessage(Error, pszFile);
116 }
117 return FALSE;
118 }
119
120 do
121 {
122 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
123 continue;
124
125 if (!wcscmp(findData.cFileName, L".") ||
126 !wcscmp(findData.cFileName, L".."))
127 {
128 continue;
129 }
130
131 wcscpy(pszFileName, findData.cFileName);
132 wcscat(pszFileName, L"\\");
133 bFound |= PrintAttribute(szFullName, pszFile, bRecurse, bDirectories);
134 }
135 while (FindNextFileW(hFind, &findData));
136 FindClose(hFind);
137 }
138
139 /* append file name */
140 wcscpy(pszFileName, pszFile);
141
142 /* search current directory */
143 hFind = FindFirstFileW(szFullName, &findData);
144 if (hFind == INVALID_HANDLE_VALUE)
145 {
146 return bFound;
147 }
148
149 do
150 {
151 bIsDir = findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
152 bExactMatch = wcsicmp(findData.cFileName, pszFile) == 0;
153
154 if (bIsDir && !bDirectories && !bExactMatch)
155 continue;
156
157 if (!wcscmp(findData.cFileName, L".") ||
158 !wcscmp(findData.cFileName, L".."))
159 {
160 continue;
161 }
162
163 wcscpy(pszFileName, findData.cFileName);
164
166 L"%c %c%c%c %s\n",
167 (findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? L'A' : L' ',
168 (findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? L'S' : L' ',
169 (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? L'H' : L' ',
170 (findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? L'R' : L' ',
171 szFullName);
172 bFound = TRUE;
173 }
174 while (FindNextFileW(hFind, &findData));
175 FindClose(hFind);
176
177 return bFound;
178}
static BOOL PrintAttribute(LPWSTR pszPath, LPWSTR pszFile, BOOL bRecurse, BOOL bDirectories)
Definition: attrib.c:83
#define StdOut
Definition: fc.c:14
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
#define FILE_ATTRIBUTE_HIDDEN
Definition: nt_native.h:703
#define FILE_ATTRIBUTE_SYSTEM
Definition: nt_native.h:704
#define FILE_ATTRIBUTE_ARCHIVE
Definition: nt_native.h:706

Referenced by PrintAttribute(), and wmain().

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 284 of file attrib.c.

285{
286 INT i;
287 BOOL bRecurse = FALSE;
288 BOOL bDirectories = FALSE;
289 DWORD dwAttrib = 0;
290 DWORD dwMask = 0;
291 BOOL bFound = FALSE;
293 WCHAR szFilePath[MAX_PATH + 2] = L""; // + 2 to reserve an extra path separator and a NULL-terminator.
294
295 /* Initialize the Console Standard Streams */
297
298 /* Check for options and file specifications */
299 for (i = 1; i < argc; i++)
300 {
301 if (*argv[i] == L'/')
302 {
303 /* Print help and bail out if needed */
304 if (wcscmp(argv[i], L"/?") == 0)
305 {
307 return 0;
308 }
309 else
310 /* Retrieve the enumeration modes */
311 if (wcsicmp(argv[i], L"/s") == 0)
312 bRecurse = TRUE;
313 else if (wcsicmp(argv[i], L"/d") == 0)
314 bDirectories = TRUE;
315 else
316 {
317 /* Unknown option */
319 return -1;
320 }
321 }
322 else
323 /* Build attributes and mask */
324 if ((*argv[i] == L'+') || (*argv[i] == L'-'))
325 {
326 BOOL bAdd = (*argv[i] == L'+');
327
328 if (wcslen(argv[i]) != 2)
329 {
331 return -1;
332 }
333
334 switch (towupper(argv[i][1]))
335 {
336 case L'A':
337 dwMask |= FILE_ATTRIBUTE_ARCHIVE;
338 if (bAdd)
339 dwAttrib |= FILE_ATTRIBUTE_ARCHIVE;
340 else
341 dwAttrib &= ~FILE_ATTRIBUTE_ARCHIVE;
342 break;
343
344 case L'S':
345 dwMask |= FILE_ATTRIBUTE_SYSTEM;
346 if (bAdd)
347 dwAttrib |= FILE_ATTRIBUTE_SYSTEM;
348 else
349 dwAttrib &= ~FILE_ATTRIBUTE_SYSTEM;
350 break;
351
352 case L'H':
353 dwMask |= FILE_ATTRIBUTE_HIDDEN;
354 if (bAdd)
355 dwAttrib |= FILE_ATTRIBUTE_HIDDEN;
356 else
357 dwAttrib &= ~FILE_ATTRIBUTE_HIDDEN;
358 break;
359
360 case L'R':
361 dwMask |= FILE_ATTRIBUTE_READONLY;
362 if (bAdd)
363 dwAttrib |= FILE_ATTRIBUTE_READONLY;
364 else
365 dwAttrib &= ~FILE_ATTRIBUTE_READONLY;
366 break;
367
368 default:
370 return -1;
371 }
372 }
373 else
374 {
375 /* At least one file specification found */
376 bFound = TRUE;
377 }
378 }
379
380 /* If no file specification was found, operate on all files of the current directory */
381 if (!bFound)
382 {
384 if (szFilePath[len - 1] != L'\\')
385 {
386 szFilePath[len] = L'\\';
388 }
389 pszFileName = L"*.*";
390
391 if (dwMask == 0)
392 bFound = PrintAttribute(szFilePath, pszFileName, bRecurse, bDirectories);
393 else
394 bFound = ChangeAttribute(szFilePath, pszFileName, bRecurse, bDirectories, dwMask, dwAttrib);
395
396 if (!bFound)
398
399 return 0;
400 }
401
402 /* Operate on each file specification */
403 for (i = 1; i < argc; i++)
404 {
405 /* Skip options */
406 if (*argv[i] == L'/' || *argv[i] == L'+' || *argv[i] == L'-')
407 continue;
408
410 if (pszFileName)
411 {
412 /* Move the file part so as to separate and NULL-terminate the directory */
414 sizeof(szFilePath) - (pszFileName -szFilePath + 1) * sizeof(*szFilePath));
416 }
417 else
418 {
419 pszFileName = L"";
420 }
421
422 if (dwMask == 0)
423 bFound = PrintAttribute(szFilePath, pszFileName, bRecurse, bDirectories);
424 else
425 bFound = ChangeAttribute(szFilePath, pszFileName, bRecurse, bDirectories, dwMask, dwAttrib);
426
427 if (!bFound)
429 }
430
431 return 0;
432}
static int argc
Definition: ServiceArgs.c:12
#define STRING_ATTRIB_HELP
Definition: resource.h:7
#define STRING_ERROR_INVALID_PARAM_FORMAT
Definition: resource.h:5
#define STRING_FILE_NOT_FOUND
Definition: resource.h:8
#define ConInitStdStreams()
Definition: fc.c:13
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
#define GetCurrentDirectoryW(x, y)
Definition: compat.h:756
DWORD WINAPI GetFullPathNameW(IN LPCWSTR lpFileName, IN DWORD nBufferLength, OUT LPWSTR lpBuffer, OUT LPWSTR *lpFilePart)
Definition: path.c:1106
GLenum GLsizei len
Definition: glext.h:6722
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define argv
Definition: mplay32.c:18
#define UNICODE_NULL
static WCHAR szFilePath[]
Definition: qotd.c:14
#define _countof(array)
Definition: sndvol32.h:68
#define towupper(c)
Definition: wctype.h:99
uint16_t * PWSTR
Definition: typedefs.h:56
#define MoveMemory
Definition: winbase.h:1667

Variable Documentation

◆ StdOutScreen

Definition at line 47 of file attrib.c.

Referenced by cmd_cls().