Changeset 415c7e0d in mainline for uspace/app/bdsh/cmds/modules/ls/ls.c
- Timestamp:
- 2009-06-28T21:41:13Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- bfd247f
- Parents:
- 75160a6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/ls/ls.c
r75160a6 r415c7e0d 51 51 static char *cmdname = "ls"; 52 52 53 static inline off_t flen(const char *f)54 {55 int fd;56 off_t size;57 58 fd = open(f, O_RDONLY);59 if (fd == -1)60 return 0;61 62 size = lseek(fd, 0, SEEK_END);63 close(fd);64 65 if (size < 0)66 size = 0;67 68 return size;69 }70 71 static unsigned int ls_scope(const char *path)72 {73 int fd;74 DIR *dirp;75 76 dirp = opendir(path);77 if (dirp) {78 closedir(dirp);79 return LS_DIR;80 }81 82 fd = open(path, O_RDONLY);83 if (fd > 0) {84 close(fd);85 return LS_FILE;86 }87 88 return LS_BOGUS;89 }90 91 53 static void ls_scan_dir(const char *d, DIR *dirp) 92 54 { 93 55 struct dirent *dp; 94 unsigned int scope;95 56 char *buff; 96 57 … … 109 70 * absolutize() later with subsequent calls to open() or readdir() */ 110 71 snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); 111 scope = ls_scope(buff); 112 switch (scope) { 113 case LS_DIR: 114 ls_print_dir(dp->d_name); 115 break; 116 case LS_FILE: 117 ls_print_file(dp->d_name, buff); 118 break; 119 case LS_BOGUS: 120 /* Odd chance it was deleted from the time readdir() found 121 * it and the time that it was scoped */ 122 printf("ls: skipping bogus node %s\n", dp->d_name); 123 break; 124 } 72 ls_print(dp->d_name, buff); 125 73 } 126 74 … … 130 78 } 131 79 132 /* ls_print _*currently does nothing more than print the entry.80 /* ls_print currently does nothing more than print the entry. 133 81 * in the future, we will likely pass the absolute path, and 134 82 * some sort of ls_options structure that controls how each … … 137 85 * Now we just print basic DOS style lists */ 138 86 139 static void ls_print _dir(const char *d)87 static void ls_print(const char *name, const char *pathname) 140 88 { 141 printf("%-40s\t<dir>\n", d); 89 struct stat s; 90 int rc; 142 91 143 return; 144 } 145 146 static void ls_print_file(const char *name, const char *pathname) 147 { 148 printf("%-40s\t%llu\n", name, (long long) flen(pathname)); 92 if (rc = stat(pathname, &s)) { 93 /* Odd chance it was deleted from the time readdir() found it */ 94 printf("ls: skipping bogus node %s\n", pathname); 95 printf("rc=%d\n", rc); 96 return; 97 } 98 99 if (s.is_file) 100 printf("%-40s\t%llu\n", name, (long long) s.size); 101 else 102 printf("%-40s\n", name); 149 103 150 104 return; … … 167 121 { 168 122 unsigned int argc; 169 unsigned int scope;123 struct stat s; 170 124 char *buff; 171 125 DIR *dirp; … … 185 139 str_cpy(buff, PATH_MAX, argv[1]); 186 140 187 scope = ls_scope(buff); 188 189 switch (scope) { 190 case LS_BOGUS: 141 if (stat(buff, &s)) { 191 142 cli_error(CL_ENOENT, buff); 192 143 free(buff); 193 144 return CMD_FAILURE; 194 case LS_FILE: 195 ls_print_file(buff, buff); 196 break; 197 case LS_DIR: 145 } 146 147 if (s.is_file) { 148 ls_print(buff, buff); 149 } else { 198 150 dirp = opendir(buff); 199 if (! 151 if (!dirp) { 200 152 /* May have been deleted between scoping it and opening it */ 201 153 cli_error(CL_EFAIL, "Could not stat %s", buff); … … 205 157 ls_scan_dir(buff, dirp); 206 158 closedir(dirp); 207 break;208 159 } 209 160
Note:
See TracChangeset
for help on using the changeset viewer.