[CMD]: Continue refactoring to lay out the way to using the CONUTILS library in CMD...
[reactos.git] / reactos / base / shell / cmd / dir.c
index 7897e05..093ae8b 100644 (file)
  *    27-Feb-2005 (Konstantinos Paliouras <squarious@gmail.com>)
  *        Implemented all the switches that were missing, and made
  *        the ros dir very similar to windows dir. Major part of
- *        the code is rewritten. /p is removed, to be rewriten in
+ *        the code is rewritten. /p is removed, to be rewritten in
  *        the main cmd code.
  *
  *    1-Jul-2004 (Brandon Turner <turnerb7@msu.edu>)
@@ -165,7 +165,7 @@ enum EOrderBy
 typedef struct _DirSwitchesFlags
 {
     BOOL bBareFormat;   /* Bare Format */
-    BOOL bTSeperator;   /* Thousands seperator */
+    BOOL bTSeparator;   /* Thousands separator */
     BOOL bWideList;     /* Wide list format */
     BOOL bWideListColSort;  /* Wide list format but sorted by column */
     BOOL bLowerCase;    /* Uses lower case */
@@ -215,7 +215,7 @@ typedef BOOL
 (WINAPI *PGETFREEDISKSPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
 
 /* Globally save the # of dirs, files and bytes,
- * probabaly later pass them to functions. Rob Lake  */
+ * probably later pass them to functions. Rob Lake  */
 static ULONG recurse_dir_cnt;
 static ULONG recurse_file_cnt;
 static ULONGLONG recurse_bytes;
@@ -244,8 +244,8 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
              LPDIRSWITCHFLAGS lpFlags)  /* [IN/OUT] The flags after calculating switches */
 {
     TCHAR cCurSwitch;   /* The current switch */
-    TCHAR cCurChar;     /* Current examing character */
-    TCHAR cCurUChar;    /* Current upper examing character */
+    TCHAR cCurChar;     /* Current examined character */
+    TCHAR cCurUChar;    /* Current upper examined character */
     BOOL bNegative;     /* Negative switch */
     BOOL bPNegative;    /* Negative switch parameter */
     BOOL bIntoQuotes;   /* A flag showing if we are in quotes (") */
@@ -326,7 +326,7 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
             else if (cCurUChar == _T('B'))
                 lpFlags->bBareFormat = ! bNegative;
             else if (cCurUChar == _T('C'))
-                lpFlags->bTSeperator = ! bNegative;
+                lpFlags->bTSeparator = ! bNegative;
             else if (cCurUChar == _T('W'))
                 lpFlags->bWideList = ! bNegative;
             else if (cCurUChar == _T('D'))
@@ -389,10 +389,10 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
                 ptrEnd = Line;
 
                 /* Copy it to the entries list */
-                temp = cmd_alloc((ptrEnd - ptrStart + 1) * sizeof (TCHAR));
+                temp = cmd_alloc((ptrEnd - ptrStart + 1) * sizeof(TCHAR));
                 if (!temp)
                     return FALSE;
-                memcpy(temp, ptrStart, (ptrEnd - ptrStart) * sizeof (TCHAR));
+                memcpy(temp, ptrStart, (ptrEnd - ptrStart) * sizeof(TCHAR));
                 temp[ptrEnd - ptrStart] = _T('\0');
                 StripQuotes(temp);
                 if (!add_entry(entries, params, temp))
@@ -414,12 +414,12 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
             /* Check if there are no more switch parameters */
             if ((cCurChar == _T('/')) || _istspace(cCurChar))
             {
-                /* Wrong desicion path, reprocess current character */
+                /* Wrong decision path, reprocess current character */
                 cCurSwitch = _T(' ');
                 continue;
             }
             /* Process parameter switch */
-            switch(cCurSwitch)
+            switch (cCurSwitch)
             {
             case _T('A'):   /* Switch parameters for /A (attributes filter) */
                 if (cCurChar == _T('-'))
@@ -528,7 +528,7 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
 
 
             }
-            /* We check if we calculated the negative value and realese the flag */
+            /* We check if we calculated the negative value and release the flag */
             if ((cCurChar != _T('-')) && bPNegative)
                 bPNegative = FALSE;
         }
@@ -550,18 +550,18 @@ DirReadParam(LPTSTR Line,               /* [IN] The line with the parameters & s
 }
 
 /* Print either with or without paging, depending on /P switch */
-static INT
+static BOOL
 DirPrintf(LPDIRSWITCHFLAGS lpFlags, LPTSTR szFormat, ...)
 {
-    INT iReturn = 0;
+    BOOL Done = TRUE;
     va_list arg_ptr;
     va_start(arg_ptr, szFormat);
     if (lpFlags->bPause)
-        iReturn = ConPrintfPaging(FALSE, szFormat, arg_ptr, STD_OUTPUT_HANDLE);
+        Done = ConPrintfVPaging(STD_OUTPUT_HANDLE, FALSE, szFormat, arg_ptr);
     else
-        ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE);
+        ConPrintfV(STD_OUTPUT_HANDLE, szFormat, arg_ptr);
     va_end(arg_ptr);
-    return iReturn;
+    return Done;
 }
 
 
@@ -583,7 +583,7 @@ PrintDirectoryHeader(LPTSTR szPath, LPDIRSWITCHFLAGS lpFlags)
     if (lpFlags->bBareFormat)
         return TRUE;
 
-    if (GetFullPathName(szPath, sizeof(szFullDir) / sizeof(TCHAR), szFullDir, &pszFilePart) == 0)
+    if (GetFullPathName(szPath, ARRAYSIZE(szFullDir), szFullDir, &pszFilePart) == 0)
     {
         ErrorMessage(GetLastError(), _T("Failed to build full directory path"));
         return FALSE;
@@ -593,7 +593,7 @@ PrintDirectoryHeader(LPTSTR szPath, LPDIRSWITCHFLAGS lpFlags)
     *pszFilePart = _T('\0');
 
     /* get the media ID of the drive */
-    if (!GetVolumePathName(szFullDir, szRootName, sizeof(szRootName) / sizeof(TCHAR)) ||
+    if (!GetVolumePathName(szFullDir, szRootName, ARRAYSIZE(szRootName)) ||
         !GetVolumeInformation(szRootName, szVolName, 80, &dwSerialNr,
                               NULL, NULL, NULL, 0))
     {
@@ -777,10 +777,10 @@ PrintSummary(LPTSTR szPath,
 
     /* Print recursive specific results */
 
-    /* Take this code offline to fix /S does not print duoble info */
+    /* Take this code offline to fix /S does not print double info */
     if (TotalSummary && lpFlags->bRecursive)
     {
-        ConvertULargeInteger(u64Bytes, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
+        ConvertULargeInteger(u64Bytes, szBuffer, ARRAYSIZE(szBuffer), lpFlags->bTSeparator);
         LoadString(CMD_ModuleHandle, STRING_DIR_HELP5, szMsg, ARRAYSIZE(szMsg));
         DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
     }
@@ -789,7 +789,7 @@ PrintSummary(LPTSTR szPath,
         /* Print File Summary */
         /* Condition to print summary is:
         If we are not in bare format and if we have results! */
-        ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeperator);
+        ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeparator);
         LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, ARRAYSIZE(szMsg));
         DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
     }
@@ -798,7 +798,7 @@ PrintSummary(LPTSTR szPath,
     if (!lpFlags->bRecursive || TotalSummary)
     {
         GetUserDiskFreeSpace(szPath, &uliFree);
-        ConvertULargeInteger(uliFree.QuadPart, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
+        ConvertULargeInteger(uliFree.QuadPart, szBuffer, ARRAYSIZE(szBuffer), lpFlags->bTSeparator);
         LoadString(CMD_ModuleHandle, STRING_DIR_HELP6, szMsg, ARRAYSIZE(szMsg));
         DirPrintf(lpFlags, szMsg, ulDirs, szBuffer);
     }
@@ -891,7 +891,7 @@ DirPrintNewList(PDIRFINDINFO ptrFiles[],        /* [IN]Files' Info */
             iSizeFormat = 14;
             u64FileSize.HighPart = ptrFiles[i]->stFindInfo.nFileSizeHigh;
             u64FileSize.LowPart = ptrFiles[i]->stFindInfo.nFileSizeLow;
-            ConvertULargeInteger(u64FileSize.QuadPart, szSize, 20, lpFlags->bTSeperator);
+            ConvertULargeInteger(u64FileSize.QuadPart, szSize, 20, lpFlags->bTSeparator);
         }
 
         /* Calculate short name */
@@ -915,7 +915,7 @@ DirPrintNewList(PDIRFINDINFO ptrFiles[],        /* [IN]Files' Info */
         ptrCurStream = ptrFiles[i]->ptrHead;
         while (ptrCurStream)
         {
-            ConvertULargeInteger(ptrCurStream->stStreamInfo.StreamSize.QuadPart, szSize, 20, lpFlags->bTSeperator);
+            ConvertULargeInteger(ptrCurStream->stStreamInfo.StreamSize.QuadPart, szSize, 20, lpFlags->bTSeparator);
 
             /* Print the line */
             DirPrintf(lpFlags, _T("%10s  %-6s    %*s%s %s%s\n"),
@@ -958,7 +958,7 @@ DirPrintWideList(PDIRFINDINFO ptrFiles[],       /* [IN] Files' Info */
     {
         if (ptrFiles[i]->stFindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
         {
-            /* Directories need 2 additinal characters for brackets */
+            /* Directories need 2 additional characters for brackets */
             if ((_tcslen(ptrFiles[i]->stFindInfo.cFileName) + 2) > iLongestName)
                 iLongestName = _tcslen(ptrFiles[i]->stFindInfo.cFileName) + 2;
         }
@@ -970,7 +970,7 @@ DirPrintWideList(PDIRFINDINFO ptrFiles[],       /* [IN] Files' Info */
     }
 
     /* Count the highest number of columns */
-    GetScreenSize(&iScreenWidth, 0);
+    GetScreenSize(&iScreenWidth, NULL);
     iColumns = (USHORT)(iScreenWidth / iLongestName);
 
     /* Check if there is enough space for spaces between names */
@@ -1062,7 +1062,7 @@ DirPrintOldList(PDIRFINDINFO ptrFiles[],        /* [IN] Files' Info */
             iSizeFormat = 17;
             u64FileSize.HighPart = ptrFiles[i]->stFindInfo.nFileSizeHigh;
             u64FileSize.LowPart = ptrFiles[i]->stFindInfo.nFileSizeLow;
-            ConvertULargeInteger(u64FileSize.QuadPart, szSize, 20, lpFlags->bTSeperator);
+            ConvertULargeInteger(u64FileSize.QuadPart, szSize, 20, lpFlags->bTSeparator);
         }
 
         /* Format date and time */
@@ -1136,10 +1136,10 @@ DirPrintFiles(PDIRFINDINFO ptrFiles[],      /* [IN] Files' Info */
     /* Condition to print header:
        We are not printing in bare format
        and if we are in recursive mode... we must have results */
-    if (!(lpFlags->bBareFormat ) && !((lpFlags->bRecursive) && (dwCount <= 0)))
+    if (!lpFlags->bBareFormat && !(lpFlags->bRecursive && (dwCount <= 0)))
     {
         LoadString(CMD_ModuleHandle, STRING_DIR_HELP7, szMsg, ARRAYSIZE(szMsg));
-        if (DirPrintf(lpFlags, szMsg, szTemp))
+        if (!DirPrintf(lpFlags, szMsg, szTemp))
             return;
     }
 
@@ -1185,12 +1185,12 @@ CompareFiles(PDIRFINDINFO lpFile1,      /* [IN] A pointer to WIN32_FIND_DATA of
   int i;
   long iComp = 0;   /* The comparison result */
 
-    /* Calculate critiries by order given from user */
-    for (i = 0;i < lpFlags->stOrderBy.sCriteriaCount;i++)
+    /* Calculate criteria by order given from user */
+    for (i = 0; i < lpFlags->stOrderBy.sCriteriaCount; i++)
     {
 
         /* Calculate criteria */
-        switch(lpFlags->stOrderBy.eCriteria[i])
+        switch (lpFlags->stOrderBy.eCriteria[i])
         {
         case ORDER_SIZE:        /* Order by size /o:s */
             /* concat the 32bit integers to a 64bit */
@@ -1199,7 +1199,7 @@ CompareFiles(PDIRFINDINFO lpFile1,      /* [IN] A pointer to WIN32_FIND_DATA of
             u64File2.LowPart = lpFile2->stFindInfo.nFileSizeLow;
             u64File2.HighPart = lpFile2->stFindInfo.nFileSizeHigh;
 
-            /* In case that differnce is too big for a long */
+            /* In case that difference is too big for a long */
             if (u64File1.QuadPart < u64File2.QuadPart)
                 iComp = -1;
             else if (u64File1.QuadPart > u64File2.QuadPart)
@@ -1223,7 +1223,7 @@ CompareFiles(PDIRFINDINFO lpFile1,      /* [IN] A pointer to WIN32_FIND_DATA of
 
         case ORDER_TIME:        /* Order by file's time /o:t */
             /* We compare files based on the time field selected by /t */
-            switch(lpFlags->stTimeField.eTimeField)
+            switch (lpFlags->stTimeField.eTimeField)
             {
             case TF_CREATIONDATE:
                 /* concat the 32bit integers to a 64bit */
@@ -1248,7 +1248,7 @@ CompareFiles(PDIRFINDINFO lpFile1,      /* [IN] A pointer to WIN32_FIND_DATA of
                 break;
             }
 
-            /* In case that differnce is too big for a long */
+            /* In case that difference is too big for a long */
             if (u64File1.QuadPart < u64File2.QuadPart)
                 iComp = -1;
             else if (u64File1.QuadPart > u64File2.QuadPart)
@@ -1323,15 +1323,15 @@ static INT
 DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
         LPDIRSWITCHFLAGS lpFlags)   /* [IN] The flags of the listing */
 {
-    BOOL fPoint;                        /* If szPath is a file with extension fPoint will be True*/
+    BOOL fPoint;                        /* If szPath is a file with extension fPoint will be True */
     HANDLE hSearch;                     /* The handle of the search */
-    HANDLE hRecSearch;                  /* The handle for searching recursivly */
+    HANDLE hRecSearch;                  /* The handle for searching recursively */
     HANDLE hStreams;                    /* The handle for alternate streams */
     WIN32_FIND_DATA wfdFileInfo;        /* The info of file that found */
     PDIRFINDINFO * ptrFileArray;        /* An array of pointers with all the files */
     PDIRFINDLISTNODE ptrStartNode;      /* The pointer to the first node */
-    PDIRFINDLISTNODE ptrNextNode;       /* A pointer used for relatives refernces */
-    TCHAR szFullPath[MAX_PATH];         /* The full path that we are listing with trailing \ */
+    PDIRFINDLISTNODE ptrNextNode;       /* A pointer used for relatives references */
+    TCHAR szFullPath[MAX_PATH];         /* The full path that we are listing with trailing '\' */
     TCHAR szSubPath[MAX_PATH];
     LPTSTR pszFilePart;
     DWORD dwCount;                      /* A counter of files found in directory */
@@ -1355,7 +1355,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     fPoint= FALSE;
 
     /* Create szFullPath */
-    if (GetFullPathName(szPath, sizeof(szFullPath) / sizeof(TCHAR), szFullPath, &pszFilePart) == 0)
+    if (GetFullPathName(szPath, ARRAYSIZE(szFullPath), szFullPath, &pszFilePart) == 0)
     {
         _tcscpy (szFullPath, szPath);
         pszFilePart = NULL;
@@ -1381,7 +1381,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     ptrStartNode->stInfo.ptrHead = NULL;
     ptrNextNode = ptrStartNode;
 
-    /*Checking ir szPath is a File with/wout extension*/
+    /* Checking if szPath is a File with/wout extension */
     if (szPath[_tcslen(szPath) - 1] == _T('.'))
         fPoint= TRUE;
 
@@ -1391,7 +1391,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     {
         do
         {
-            /*If retrieved FileName has extension,and szPath doesnt have extension then JUMP the retrieved FileName*/
+            /* If retrieved FileName has extension,and szPath doesnt have extension then JUMP the retrieved FileName */
             if (_tcschr(wfdFileInfo.cFileName,_T('.'))&&(fPoint==TRUE))
             {
                 continue;
@@ -1600,15 +1600,16 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     if (CheckCtrlBreak(BREAK_INPUT))
         return 1;
 
-
-    /* Add statistics to recursive statistics*/
+    /* Add statistics to recursive statistics */
     recurse_dir_cnt += dwCountDirs;
     recurse_file_cnt += dwCountFiles;
     recurse_bytes += u64CountBytes;
 
-    /* Do the recursive job if requested
-       the recursive is be done on ALL(indepent of their attribs)
-       directoried of the current one.*/
+    /*
+     * Do the recursive job if requested.
+     * The recursion is done on ALL (independent of their attributes)
+     * directories of the current one.
+     */
     if (lpFlags->bRecursive)
     {
         /* The new search is involving any *.* file */
@@ -1638,7 +1639,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
                         return 1;
                     }
                 }
-            } while(FindNextFile(hRecSearch, &wfdFileInfo));
+            } while (FindNextFile(hRecSearch, &wfdFileInfo));
         }
         FindClose(hRecSearch);
     }
@@ -1674,7 +1675,7 @@ CommandDir(LPTSTR rest)
     stFlags.bPause = FALSE;
     stFlags.bRecursive = FALSE;
     stFlags.bShortName = FALSE;
-    stFlags.bTSeperator = TRUE;
+    stFlags.bTSeparator = TRUE;
     stFlags.bUser = FALSE;
     stFlags.bWideList = FALSE;
     stFlags.bWideListColSort = FALSE;
@@ -1718,7 +1719,7 @@ CommandDir(LPTSTR rest)
     if (stFlags.bPause)
         ConOutPrintfPaging(TRUE, _T(""));
 
-    for(loop = 0; loop < (UINT)entries; loop++)
+    for (loop = 0; loop < (UINT)entries; loop++)
     {
         if (CheckCtrlBreak(BREAK_INPUT))
         {
@@ -1736,7 +1737,7 @@ CommandDir(LPTSTR rest)
             int i;
             TRACE("Attributes mask/value %x/%x\n",stFlags.stAttribs.dwAttribMask,stFlags.stAttribs.dwAttribVal  );
             TRACE("(B) Bare format : %i\n", stFlags.bBareFormat );
-            TRACE("(C) Thousand : %i\n", stFlags.bTSeperator );
+            TRACE("(C) Thousand : %i\n", stFlags.bTSeparator );
             TRACE("(W) Wide list : %i\n", stFlags.bWideList );
             TRACE("(D) Wide list sort by column : %i\n", stFlags.bWideListColSort );
             TRACE("(L) Lowercase : %i\n", stFlags.bLowerCase );
@@ -1757,14 +1758,14 @@ CommandDir(LPTSTR rest)
         ChangedVolume = TRUE;
 
         if (!stFlags.bBareFormat &&
-            GetVolumePathName(params[loop], path, sizeof(path) / sizeof(TCHAR)))
+            GetVolumePathName(params[loop], path, ARRAYSIZE(path)))
         {
             if (!_tcscmp(path, prev_volume))
                 ChangedVolume = FALSE;
             else
                 _tcscpy(prev_volume, path);
         }
-        else if (GetFullPathName(params[loop], sizeof(path) / sizeof(TCHAR), path, &pszFilePart) != 0)
+        else if (GetFullPathName(params[loop], ARRAYSIZE(path), path, &pszFilePart) != 0)
         {
             if (pszFilePart != NULL)
                 *pszFilePart = _T('\0');
@@ -1784,7 +1785,7 @@ CommandDir(LPTSTR rest)
         }
 
         /* do the actual dir */
-        if (DirList (params[loop], &stFlags))
+        if (DirList(params[loop], &stFlags))
         {
             nErrorLevel = 1;
             goto cleanup;