df: fix multiple bugs
authorAlan Cox <alan@linux.intel.com>
Sat, 16 May 2015 15:44:26 +0000 (16:44 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 16 May 2015 15:44:26 +0000 (16:44 +0100)
- sizeof(number) is not number but 1 or 2
- check strtok return rather than trying to play with NULL

Not clear with is the root cause of the trs80 df crash but at least it works
now

Applications/util/df.c

index 074b605..b2830af 100644 (file)
@@ -125,13 +125,14 @@ void df_all(void)
     
     f = fopen("/etc/mtab", "r");
     if (f) {
-        while (fgets(tmp, sizeof(MTAB_LINE), f)) {
-                       dev = strtok(tmp, " ");
-                       mntpt = strtok(NULL, " ");
-            df_path(mntpt);
+        while (fgets(tmp, MTAB_LINE, f)) {
+           dev = strtok(tmp, " ");
+           mntpt = strtok(NULL, " ");
+            if (mntpt)
+                df_path(mntpt);
         }
         fclose(f);
-    }else{
+    } else {
         fprintf(stderr, "df: cannot open /etc/mtab: %s\n", strerror(errno));
     }
 }
@@ -156,9 +157,11 @@ const char *devname(dev_t devno)
                 continue;
             if (fstat.st_rdev != devno)
                 continue;
+            closedir(dp);
             return namebuf;
         }
     }
+    closedir(dp);
 
     sprintf(namebuf, "%d", devno);
     return namebuf;