ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

attrib.c
Go to the documentation of this file.
00001 /*
00002  *  ATTRIB.C - attrib internal command.
00003  *
00004  *
00005  *  History:
00006  *
00007  *    04-Dec-1998 Eric Kohl
00008  *        started
00009  *
00010  *    09-Dec-1998 Eric Kohl
00011  *        implementation works, except recursion ("attrib /s").
00012  *
00013  *    05-Jan-1999 Eric Kohl
00014  *        major rewrite.
00015  *        fixed recursion ("attrib /s").
00016  *        started directory support ("attrib /s /d").
00017  *        updated help text.
00018  *
00019  *    14-Jan-1999 Eric Kohl
00020  *        Unicode ready!
00021  *
00022  *    19-Jan-1999 Eric Kohl
00023  *        Redirection ready!
00024  *
00025  *    21-Jan-1999 Eric Kohl
00026  *        Added check for invalid filenames.
00027  *
00028  *    23-Jan-1999 Eric Kohl
00029  *        Added handling of multiple filenames.
00030  *
00031  *    02-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
00032  *        Remove all hardcode string to En.rc
00033  */
00034 
00035 #include <precomp.h>
00036 
00037 #ifdef INCLUDE_CMD_ATTRIB
00038 
00039 
00040 static VOID
00041 PrintAttribute (LPTSTR pszPath, LPTSTR pszFile, BOOL bRecurse)
00042 {
00043     WIN32_FIND_DATA findData;
00044     HANDLE hFind;
00045     TCHAR  szFullName[MAX_PATH];
00046     LPTSTR pszFileName;
00047 
00048     /* prepare full file name buffer */
00049     _tcscpy (szFullName, pszPath);
00050     pszFileName = szFullName + _tcslen (szFullName);
00051 
00052     /* display all subdirectories */
00053     if (bRecurse)
00054     {
00055         /* append file name */
00056         _tcscpy (pszFileName, pszFile);
00057 
00058         hFind = FindFirstFile (szFullName, &findData);
00059         if (hFind == INVALID_HANDLE_VALUE)
00060         {
00061             ErrorMessage (GetLastError (), pszFile);
00062             return;
00063         }
00064 
00065         do
00066         {
00067             if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
00068                 continue;
00069 
00070             if (!_tcscmp (findData.cFileName, _T(".")) ||
00071                 !_tcscmp (findData.cFileName, _T("..")))
00072                 continue;
00073 
00074             _tcscpy (pszFileName, findData.cFileName);
00075             _tcscat (pszFileName, _T("\\"));
00076             PrintAttribute (szFullName, pszFile, bRecurse);
00077         }
00078         while (FindNextFile (hFind, &findData));
00079         FindClose (hFind);
00080     }
00081 
00082     /* append file name */
00083     _tcscpy (pszFileName, pszFile);
00084 
00085     /* display current directory */
00086     hFind = FindFirstFile (szFullName, &findData);
00087     if (hFind == INVALID_HANDLE_VALUE)
00088     {
00089         ErrorMessage (GetLastError (), pszFile);
00090         return;
00091     }
00092 
00093     do
00094     {
00095         if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00096             continue;
00097 
00098         _tcscpy (pszFileName, findData.cFileName);
00099 
00100         ConOutPrintf (_T("%c  %c%c%c     %s\n"),
00101                       (findData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) ? _T('A') : _T(' '),
00102                       (findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? _T('S') : _T(' '),
00103                       (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? _T('H') : _T(' '),
00104                       (findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _T('R') : _T(' '),
00105                       szFullName);
00106     }
00107     while (FindNextFile (hFind, &findData));
00108     FindClose (hFind);
00109 }
00110 
00111 
00112 static VOID
00113 ChangeAttribute (LPTSTR pszPath, LPTSTR pszFile, DWORD dwMask,
00114          DWORD dwAttrib, BOOL bRecurse, BOOL bDirectories)
00115 {
00116     WIN32_FIND_DATA findData;
00117     HANDLE hFind;
00118     DWORD  dwAttribute;
00119     TCHAR  szFullName[MAX_PATH];
00120     LPTSTR pszFileName;
00121 
00122 
00123     /* prepare full file name buffer */
00124     _tcscpy (szFullName, pszPath);
00125     pszFileName = szFullName + _tcslen (szFullName);
00126 
00127     /* change all subdirectories */
00128     if (bRecurse)
00129     {
00130         /* append file name */
00131         _tcscpy (pszFileName, _T("*.*"));
00132 
00133         hFind = FindFirstFile (szFullName, &findData);
00134         if (hFind == INVALID_HANDLE_VALUE)
00135         {
00136             ErrorMessage (GetLastError (), pszFile);
00137       nErrorLevel = 1;
00138             return;
00139         }
00140 
00141         do
00142         {
00143             if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00144             {
00145                 if (!_tcscmp (findData.cFileName, _T(".")) ||
00146                     !_tcscmp (findData.cFileName, _T("..")))
00147                     continue;
00148 
00149                 _tcscpy (pszFileName, findData.cFileName);
00150                 _tcscat (pszFileName, _T("\\"));
00151 
00152                 ChangeAttribute (szFullName, pszFile, dwMask,
00153                                  dwAttrib, bRecurse, bDirectories);
00154             }
00155         }
00156         while (FindNextFile (hFind, &findData));
00157         FindClose (hFind);
00158     }
00159 
00160     /* append file name */
00161     _tcscpy (pszFileName, pszFile);
00162 
00163     hFind = FindFirstFile (szFullName, &findData);
00164     if (hFind == INVALID_HANDLE_VALUE)
00165     {
00166         ErrorMessage (GetLastError (), pszFile);
00167     nErrorLevel = 1;
00168         return;
00169     }
00170 
00171     do
00172     {
00173         if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00174             continue;
00175 
00176         _tcscpy (pszFileName, findData.cFileName);
00177 
00178         dwAttribute = GetFileAttributes (szFullName);
00179 
00180         if (dwAttribute != 0xFFFFFFFF)
00181         {
00182             dwAttribute = (dwAttribute & ~dwMask) | dwAttrib;
00183             SetFileAttributes (szFullName, dwAttribute);
00184         }
00185     }
00186     while (FindNextFile (hFind, &findData));
00187     FindClose (hFind);
00188 }
00189 
00190 
00191 INT CommandAttrib (LPTSTR param)
00192 {
00193     LPTSTR *arg;
00194     INT    argc, i;
00195     TCHAR  szPath[MAX_PATH];
00196     TCHAR  szFileName [MAX_PATH];
00197     BOOL   bRecurse = FALSE;
00198     BOOL   bDirectories = FALSE;
00199     DWORD  dwAttrib = 0;
00200     DWORD  dwMask = 0;
00201 
00202     /* initialize strings */
00203     szPath[0] = _T('\0');
00204     szFileName[0] = _T('\0');
00205 
00206     /* print help */
00207     if (!_tcsncmp (param, _T("/?"), 2))
00208     {
00209         ConOutResPaging(TRUE,STRING_ATTRIB_HELP);
00210         return 0;
00211     }
00212 
00213   nErrorLevel = 0;
00214 
00215     /* build parameter array */
00216     arg = split (param, &argc, FALSE, FALSE);
00217 
00218     /* check for options */
00219     for (i = 0; i < argc; i++)
00220     {
00221         if (_tcsicmp (arg[i], _T("/s")) == 0)
00222             bRecurse = TRUE;
00223         else if (_tcsicmp (arg[i], _T("/d")) == 0)
00224             bDirectories = TRUE;
00225     }
00226 
00227     /* create attributes and mask */
00228     for (i = 0; i < argc; i++)
00229     {
00230         if (*arg[i] == _T('+'))
00231         {
00232             if (_tcslen (arg[i]) != 2)
00233             {
00234                 error_invalid_parameter_format (arg[i]);
00235                 freep (arg);
00236                 return -1;
00237             }
00238 
00239             switch ((TCHAR)_totupper (arg[i][1]))
00240             {
00241                 case _T('A'):
00242                     dwMask   |= FILE_ATTRIBUTE_ARCHIVE;
00243                     dwAttrib |= FILE_ATTRIBUTE_ARCHIVE;
00244                     break;
00245 
00246                 case _T('H'):
00247                     dwMask   |= FILE_ATTRIBUTE_HIDDEN;
00248                     dwAttrib |= FILE_ATTRIBUTE_HIDDEN;
00249                     break;
00250 
00251                 case _T('R'):
00252                     dwMask   |= FILE_ATTRIBUTE_READONLY;
00253                     dwAttrib |= FILE_ATTRIBUTE_READONLY;
00254                     break;
00255 
00256                 case _T('S'):
00257                     dwMask   |= FILE_ATTRIBUTE_SYSTEM;
00258                     dwAttrib |= FILE_ATTRIBUTE_SYSTEM;
00259                     break;
00260 
00261                 default:
00262                     error_invalid_parameter_format (arg[i]);
00263                     freep (arg);
00264                     return -1;
00265             }
00266         }
00267         else if (*arg[i] == _T('-'))
00268         {
00269             if (_tcslen (arg[i]) != 2)
00270             {
00271                 error_invalid_parameter_format (arg[i]);
00272                 freep (arg);
00273                 return -1;
00274             }
00275 
00276             switch ((TCHAR)_totupper (arg[i][1]))
00277             {
00278                 case _T('A'):
00279                     dwMask   |= FILE_ATTRIBUTE_ARCHIVE;
00280                     dwAttrib &= ~FILE_ATTRIBUTE_ARCHIVE;
00281                     break;
00282 
00283                 case _T('H'):
00284                     dwMask   |= FILE_ATTRIBUTE_HIDDEN;
00285                     dwAttrib &= ~FILE_ATTRIBUTE_HIDDEN;
00286                     break;
00287 
00288                 case _T('R'):
00289                     dwMask   |= FILE_ATTRIBUTE_READONLY;
00290                     dwAttrib &= ~FILE_ATTRIBUTE_READONLY;
00291                     break;
00292 
00293                 case _T('S'):
00294                     dwMask   |= FILE_ATTRIBUTE_SYSTEM;
00295                     dwAttrib &= ~FILE_ATTRIBUTE_SYSTEM;
00296                     break;
00297 
00298                 default:
00299                     error_invalid_parameter_format (arg[i]);
00300                     freep (arg);
00301                     return -1;
00302             }
00303         }
00304     }
00305 
00306     if (argc == 0)
00307     {
00308         DWORD len;
00309 
00310         len = GetCurrentDirectory (MAX_PATH, szPath);
00311         if (szPath[len-1] != _T('\\'))
00312         {
00313             szPath[len] = _T('\\');
00314             szPath[len + 1] = 0;
00315         }
00316         _tcscpy (szFileName, _T("*.*"));
00317         PrintAttribute (szPath, szFileName, bRecurse);
00318         freep (arg);
00319         return 0;
00320     }
00321 
00322     /* get full file name */
00323     for (i = 0; i < argc; i++)
00324     {
00325         if ((*arg[i] != _T('+')) && (*arg[i] != _T('-')) && (*arg[i] != _T('/')))
00326         {
00327             LPTSTR p;
00328             GetFullPathName (arg[i], MAX_PATH, szPath, NULL);
00329             p = _tcsrchr (szPath, _T('\\')) + 1;
00330             _tcscpy (szFileName, p);
00331             *p = _T('\0');
00332 
00333             if (dwMask == 0)
00334                 PrintAttribute (szPath, szFileName, bRecurse);
00335             else
00336                 ChangeAttribute (szPath, szFileName, dwMask,
00337                          dwAttrib, bRecurse, bDirectories);
00338         }
00339     }
00340 
00341     freep (arg);
00342     return 0;
00343 }
00344 
00345 #endif /* INCLUDE_CMD_ATTRIB */

Generated on Fri May 25 2012 04:16:27 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.