[CMD] Create a DirNodeCleanup() helper for cleanup and simplify code (adapted from...
[reactos.git] / base / shell / cmd / dir.c
index 07d14d6..a44f9ac 100644 (file)
@@ -1314,6 +1314,27 @@ QsortFiles(PDIRFINDINFO ptrArray[],         /* [IN/OUT] The array with file info
     }
 }
 
+static VOID
+DirNodeCleanup(PDIRFINDLISTNODE ptrStartNode,
+               PDWORD pdwCount)
+{
+    PDIRFINDLISTNODE ptrNextNode;
+    PDIRFINDSTREAMNODE ptrFreeNode;
+    while (ptrStartNode)
+    {
+        ptrNextNode = ptrStartNode->ptrNext;
+        while (ptrStartNode->stInfo.ptrHead)
+        {
+            ptrFreeNode = ptrStartNode->stInfo.ptrHead;
+            ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext;
+            cmd_free(ptrFreeNode);
+        }
+        cmd_free(ptrStartNode);
+        ptrStartNode = ptrNextNode;
+        --(*pdwCount);
+    }
+}
+
 /*
  * DirList
  *
@@ -1341,7 +1362,6 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     ULARGE_INTEGER u64Temp;             /* A temporary counter */
     WIN32_FIND_STREAM_DATA wfsdStreamInfo;
     PDIRFINDSTREAMNODE * ptrCurNode;    /* The pointer to the first stream */
-    PDIRFINDSTREAMNODE ptrFreeNode;     /* The pointer used during cleanup */
     static HANDLE (WINAPI *pFindFirstStreamW)(LPCWSTR, STREAM_INFO_LEVELS, LPVOID, DWORD);
     static BOOL (WINAPI *pFindNextStreamW)(HANDLE, LPVOID);
 
@@ -1404,19 +1424,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
                 if (ptrNextNode->ptrNext == NULL)
                 {
                     WARN("DEBUG: Cannot allocate memory for ptrNextNode->ptrNext!\n");
-                    while (ptrStartNode)
-                    {
-                        ptrNextNode = ptrStartNode->ptrNext;
-                        while (ptrStartNode->stInfo.ptrHead)
-                        {
-                            ptrFreeNode = ptrStartNode->stInfo.ptrHead;
-                            ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext;
-                            cmd_free(ptrFreeNode);
-                        }
-                        cmd_free(ptrStartNode);
-                        ptrStartNode = ptrNextNode;
-                        dwCount--;
-                    }
+                    DirNodeCleanup(ptrStartNode, &dwCount);
                     FindClose(hSearch);
                     return 1;
                 }
@@ -1466,19 +1474,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
                             if (*ptrCurNode == NULL)
                             {
                                 WARN("DEBUG: Cannot allocate memory for *ptrCurNode!\n");
-                                while (ptrStartNode)
-                                {
-                                    ptrNextNode = ptrStartNode->ptrNext;
-                                    while (ptrStartNode->stInfo.ptrHead)
-                                    {
-                                        ptrFreeNode = ptrStartNode->stInfo.ptrHead;
-                                        ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext;
-                                        cmd_free(ptrFreeNode);
-                                    }
-                                    cmd_free(ptrStartNode);
-                                    ptrStartNode = ptrNextNode;
-                                    dwCount--;
-                                }
+                                DirNodeCleanup(ptrStartNode, &dwCount);
                                 FindClose(hStreams);
                                 FindClose(hSearch);
                                 return 1;
@@ -1532,19 +1528,7 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
     if (ptrFileArray == NULL)
     {
         WARN("DEBUG: Cannot allocate memory for ptrFileArray!\n");
-        while (ptrStartNode)
-        {
-            ptrNextNode = ptrStartNode->ptrNext;
-            while (ptrStartNode->stInfo.ptrHead)
-            {
-                ptrFreeNode = ptrStartNode->stInfo.ptrHead;
-                ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext;
-                cmd_free(ptrFreeNode);
-            }
-            cmd_free(ptrStartNode);
-            ptrStartNode = ptrNextNode;
-            dwCount --;
-        }
+        DirNodeCleanup(ptrStartNode, &dwCount);
         return 1;
     }
 
@@ -1582,20 +1566,9 @@ DirList(LPTSTR szPath,              /* [IN] The path that dir starts */
 
     /* Free array */
     cmd_free(ptrFileArray);
+
     /* Free linked list */
-    while (ptrStartNode)
-    {
-        ptrNextNode = ptrStartNode->ptrNext;
-        while (ptrStartNode->stInfo.ptrHead)
-        {
-            ptrFreeNode = ptrStartNode->stInfo.ptrHead;
-            ptrStartNode->stInfo.ptrHead = ptrFreeNode->ptrNext;
-            cmd_free(ptrFreeNode);
-        }
-        cmd_free(ptrStartNode);
-        ptrStartNode = ptrNextNode;
-        dwCount --;
-    }
+    DirNodeCleanup(ptrStartNode, &dwCount);
 
     if (CheckCtrlBreak(BREAK_INPUT))
         return 1;