Use 9600 bps for Pintos serial, to match the speed used by the loader.
[pintos-anon] / src / examples / ls.c
index 5907a6cedb90ab507e6f4feee4502f385c1bcdd7..fbe27a1e5023312bf0a421969ed8989f9d24b4c6 100644 (file)
@@ -5,27 +5,32 @@
    named.
 
    By default, only the name of each file is printed.  If "-l" is
-   given as the first argument, the type and size of each file is
-   also printed. */
+   given as the first argument, the type, size, and inumber of
+   each file is also printed.  This won't work until project 4. */
 
 #include <syscall.h>
 #include <stdio.h>
 #include <string.h>
 
-static void
+static bool
 list_dir (const char *dir, bool verbose) 
 {
   int dir_fd = open (dir);
   if (dir_fd == -1) 
     {
       printf ("%s: not found\n", dir);
-      return
+      return false;
     }
 
   if (isdir (dir_fd))
     {
       char name[READDIR_MAX_LEN];
-      printf ("%s:\n", dir);
+
+      printf ("%s", dir);
+      if (verbose)
+        printf (" (inumber %d)", inumber (dir_fd));
+      printf (":\n");
+
       while (readdir (dir_fd, name)) 
         {
           printf ("%s", name); 
@@ -34,14 +39,7 @@ list_dir (const char *dir, bool verbose)
               char full_name[128];
               int entry_fd;
 
-              if (strcmp (dir, "."))
-                snprintf (full_name, sizeof full_name, "%s/%s", dir, name);
-              else 
-                {
-                  /* This is a special case for implementations
-                     that don't fully understand . and .. */
-                  strlcpy (full_name, name, sizeof full_name); 
-                }
+              snprintf (full_name, sizeof full_name, "%s/%s", dir, name);
               entry_fd = open (full_name);
 
               printf (": ");
@@ -51,6 +49,7 @@ list_dir (const char *dir, bool verbose)
                     printf ("directory");
                   else
                     printf ("%d-byte file", filesize (entry_fd));
+                  printf (", inumber %d", inumber (entry_fd));
                 }
               else
                 printf ("open failed");
@@ -61,13 +60,16 @@ list_dir (const char *dir, bool verbose)
     }
   else 
     printf ("%s: not a directory\n", dir);
-  close (dir_fd); 
+  close (dir_fd);
+  return true;
 }
 
 int
 main (int argc, char *argv[]) 
 {
+  bool success = true;
   bool verbose = false;
+  
   if (argc > 1 && !strcmp (argv[1], "-l")) 
     {
       verbose = true;
@@ -76,12 +78,13 @@ main (int argc, char *argv[])
     }
   
   if (argc <= 1)
-    list_dir (".", verbose);
+    success = list_dir (".", verbose);
   else 
     {
       int i;
       for (i = 1; i < argc; i++)
-        list_dir (argv[i], verbose);
+        if (!list_dir (argv[i], verbose))
+          success = false;
     }
-  return 0;
+  return success ? EXIT_SUCCESS : EXIT_FAILURE;
 }