소스 검색

Update listdir.c

1.close dir before return;
2.fixed indentation.
David Lin 4 년 전
부모
커밋
adc5dab5b2
1개의 변경된 파일37개의 추가작업 그리고 36개의 파일을 삭제
  1. 37 36
      examples/file/listdir.c

+ 37 - 36
examples/file/listdir.c

@@ -14,49 +14,50 @@
 void list_dir(const char* path)
 {
     char * fullpath;
-	DIR *dir;
+    DIR *dir;
 
-	dir = opendir(path);
-	if (dir != RT_NULL)
-	{
-		struct dirent* dirent;
-		struct stat s;
+    dir = opendir(path);
+    if (dir != RT_NULL)
+    {
+        struct dirent* dirent;
+        struct stat s;
 
-	    fullpath = rt_malloc(256);
-	    if (fullpath == RT_NULL)
-	    {
-	        rt_kprintf("no memory\n");
-	        return;
-	    }
+        fullpath = rt_malloc(256);
+        if (fullpath == RT_NULL)
+        {
+            closedir(dir);
+            rt_kprintf("no memory\n");
+            return;
+        }
 
-		do
-		{
-			dirent = readdir(dir);
-			if (dirent == RT_NULL) break;
-			rt_memset(&s, 0, sizeof(struct stat));
+        do
+        {
+            dirent = readdir(dir);
+            if (dirent == RT_NULL) break;
+            rt_memset(&s, 0, sizeof(struct stat));
 
-			/* build full path for each file */
-			rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
+            /* build full path for each file */
+            rt_sprintf(fullpath, "%s/%s", path, dirent->d_name);
 
-			stat(fullpath, &s);
-			if ( s.st_mode & S_IFDIR )
-			{
-				rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
-			}
-			else
-			{
-				rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
-			}
-		} while (dirent != RT_NULL);
+            stat(fullpath, &s);
+            if ( s.st_mode & S_IFDIR )
+            {
+                rt_kprintf("%s\t\t<DIR>\n", dirent->d_name);
+            }
+            else
+            {
+                rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size);
+            }
+        } while (dirent != RT_NULL);
 
-		closedir(dir);
-	}
-	else
-	{
-	    rt_kprintf("open %s directory failed\n", path);
-	}
+        closedir(dir);
+    }
+    else
+    {
+        rt_kprintf("open %s directory failed\n", path);
+    }
 
-	rt_free(fullpath);
+    rt_free(fullpath);
 }
 
 #ifdef RT_USING_FINSH