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