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