001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019 #include <libc.h>
020 #include <debug.h>
021
022 #include "fstest_utils.h"
023
024
025
026
027 static void cwd_ls(int detailed, int recursive, int reclevel)
028 {
029 char tab[256], *c;
030 int i;
031 struct dirent * dirent;
032 DIR * here;
033
034 here = opendir(".");
035 if (! here)
036 return;
037
038
039 if (recursive)
040 {
041 for (c = tab, i = 0 ; (i < reclevel) && (i < sizeof(tab)/2) ; i++)
042 {
043 *c++ = ' ';
044 *c++ = ' ';
045 }
046 *c++ = '\0';
047 }
048 else
049 *tab = '\0';
050
051 while ((dirent = readdir(here)) != NULL)
052 {
053 char entrychar;
054 char * entrysuffix;
055
056 switch(dirent->type)
057 {
058 case S_IFREG: entrychar='-'; entrysuffix=""; break;
059 case S_IFDIR: entrychar='d'; entrysuffix="/"; break;
060 case S_IFLNK: entrychar='l'; entrysuffix="@"; break;
061 default: entrychar='?'; entrysuffix="?!?"; break;
062 }
063
064 if (detailed)
065 {
066 struct stat stat;
067 char target_name[SOS_FS_DIRENT_NAME_MAXLEN];
068
069 if (lstat(dirent->name, & stat))
070 continue;
071
072 *target_name = '\0';
073 if (stat.st_type==S_IFLNK)
074 {
075 int fd = open(dirent->name, O_RDONLY | O_NOFOLLOW);
076 if (fd >= 0)
077 {
078 int len = read(fd, target_name, sizeof(target_name) - 1);
079 if (len < 0)
080 *target_name='\0';
081 else
082 target_name[len] = '\0';
083 close(fd);
084 }
085 }
086
087 bochs_printf("%s%c%c%c%c %lld %s%s%s%s (location=0x%llx)\n",
088 tab, entrychar,
089 (stat.st_access_rights&S_IRUSR)?'r':'-',
090 (stat.st_access_rights&S_IWUSR)?'w':'-',
091 (stat.st_access_rights&S_IXUSR)?'x':'-',
092 stat.st_size,
093 dirent->name,
094 entrysuffix,
095 (stat.st_type==S_IFLNK)?" -> ":"",
096 target_name,
097 stat.st_storage_location);
098 }
099 else
100 bochs_printf("%s%s%s\n",
101 tab, dirent->name, entrysuffix);
102
103
104 if (recursive)
105 {
106 int fd_here = dirfd(here);
107 if (chdir(dirent->name))
108 continue;
109 cwd_ls(detailed, recursive, reclevel+1);
110 if(fchdir(fd_here))
111 {
112 closedir(here);
113 return;
114 }
115 }
116 }
117 closedir(here);
118 }
119
120
121 void ls(const char * path, int detailed, int recursive)
122 {
123 int fd_here = open(".", O_RDONLY | O_DIRECTORY);
124 if (fd_here < 0)
125 return;
126
127 if (chdir(path))
128 {
129 close(fd_here);
130 return;
131 }
132
133 bochs_printf("----------- Contents of %s:\n", path);
134 cwd_ls(detailed, recursive, 1);
135 bochs_printf("---------------------------\n");
136
137 fchdir(fd_here);
138 close(fd_here);
139 }