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