00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <syscall.h>
00013 #include <stdio.h>
00014 #include <string.h>
00015
00016 static bool
00017 list_dir (const char *dir, bool verbose)
00018 {
00019 int dir_fd = open (dir);
00020 if (dir_fd == -1)
00021 {
00022 printf ("%s: not found\n", dir);
00023 return false;
00024 }
00025
00026 if (isdir (dir_fd))
00027 {
00028 char name[READDIR_MAX_LEN];
00029
00030 printf ("%s", dir);
00031 if (verbose)
00032 printf (" (inumber %d)", inumber (dir_fd));
00033 printf (":\n");
00034
00035 while (readdir (dir_fd, name))
00036 {
00037 printf ("%s", name);
00038 if (verbose)
00039 {
00040 char full_name[128];
00041 int entry_fd;
00042
00043 snprintf (full_name, sizeof full_name, "%s/%s", dir, name);
00044 entry_fd = open (full_name);
00045
00046 printf (": ");
00047 if (entry_fd != -1)
00048 {
00049 if (isdir (entry_fd))
00050 printf ("directory");
00051 else
00052 printf ("%d-byte file", filesize (entry_fd));
00053 printf (", inumber %d", inumber (entry_fd));
00054 }
00055 else
00056 printf ("open failed");
00057 close (entry_fd);
00058 }
00059 printf ("\n");
00060 }
00061 }
00062 else
00063 printf ("%s: not a directory\n", dir);
00064 close (dir_fd);
00065 return true;
00066 }
00067
00068 int
00069 main (int argc, char *argv[])
00070 {
00071 bool success = true;
00072 bool verbose = false;
00073
00074 if (argc > 1 && !strcmp (argv[1], "-l"))
00075 {
00076 verbose = true;
00077 argv++;
00078 argc--;
00079 }
00080
00081 if (argc <= 1)
00082 success = list_dir (".", verbose);
00083 else
00084 {
00085 int i;
00086 for (i = 1; i < argc; i++)
00087 if (!list_dir (argv[i], verbose))
00088 success = false;
00089 }
00090 return success ? EXIT_SUCCESS : EXIT_FAILURE;
00091 }