The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <dirent.h> |
| 6 | #include <errno.h> |
| 7 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 8 | #include <selinux/selinux.h> |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 9 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <unistd.h> |
| 12 | #include <time.h> |
| 13 | |
| 14 | #include <pwd.h> |
| 15 | #include <grp.h> |
| 16 | |
| 17 | #include <linux/kdev_t.h> |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 18 | #include <limits.h> |
| 19 | |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 20 | #include "dynarray.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | |
| 22 | // bits for flags argument |
Andy McFadden | b33d341 | 2009-04-08 19:25:35 -0700 | [diff] [blame] | 23 | #define LIST_LONG (1 << 0) |
| 24 | #define LIST_ALL (1 << 1) |
| 25 | #define LIST_RECURSIVE (1 << 2) |
| 26 | #define LIST_DIRECTORIES (1 << 3) |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 27 | #define LIST_SIZE (1 << 4) |
Brad Fitzpatrick | e7fe5bf | 2010-10-25 13:29:53 -0700 | [diff] [blame] | 28 | #define LIST_LONG_NUMERIC (1 << 5) |
Kenny Root | 40dac65 | 2011-07-13 09:14:33 -0700 | [diff] [blame] | 29 | #define LIST_CLASSIFY (1 << 6) |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 30 | #define LIST_MACLABEL (1 << 7) |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 31 | #define LIST_INODE (1 << 8) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 32 | |
| 33 | // fwd |
| 34 | static int listpath(const char *name, int flags); |
| 35 | |
Elliott Hughes | fd4c6b0 | 2014-07-22 17:20:15 -0700 | [diff] [blame] | 36 | static char mode2kind(mode_t mode) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | { |
| 38 | switch(mode & S_IFMT){ |
| 39 | case S_IFSOCK: return 's'; |
| 40 | case S_IFLNK: return 'l'; |
| 41 | case S_IFREG: return '-'; |
| 42 | case S_IFDIR: return 'd'; |
| 43 | case S_IFBLK: return 'b'; |
| 44 | case S_IFCHR: return 'c'; |
| 45 | case S_IFIFO: return 'p'; |
| 46 | default: return '?'; |
| 47 | } |
| 48 | } |
| 49 | |
Elliott Hughes | fd4c6b0 | 2014-07-22 17:20:15 -0700 | [diff] [blame] | 50 | void strmode(mode_t mode, char *out) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | { |
| 52 | *out++ = mode2kind(mode); |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 53 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | *out++ = (mode & 0400) ? 'r' : '-'; |
| 55 | *out++ = (mode & 0200) ? 'w' : '-'; |
| 56 | if(mode & 04000) { |
| 57 | *out++ = (mode & 0100) ? 's' : 'S'; |
| 58 | } else { |
| 59 | *out++ = (mode & 0100) ? 'x' : '-'; |
| 60 | } |
| 61 | *out++ = (mode & 040) ? 'r' : '-'; |
| 62 | *out++ = (mode & 020) ? 'w' : '-'; |
| 63 | if(mode & 02000) { |
| 64 | *out++ = (mode & 010) ? 's' : 'S'; |
| 65 | } else { |
| 66 | *out++ = (mode & 010) ? 'x' : '-'; |
| 67 | } |
| 68 | *out++ = (mode & 04) ? 'r' : '-'; |
| 69 | *out++ = (mode & 02) ? 'w' : '-'; |
| 70 | if(mode & 01000) { |
| 71 | *out++ = (mode & 01) ? 't' : 'T'; |
| 72 | } else { |
| 73 | *out++ = (mode & 01) ? 'x' : '-'; |
| 74 | } |
| 75 | *out = 0; |
| 76 | } |
| 77 | |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 78 | static void user2str(uid_t uid, char *out, size_t out_size) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | { |
| 80 | struct passwd *pw = getpwuid(uid); |
| 81 | if(pw) { |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 82 | strlcpy(out, pw->pw_name, out_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | } else { |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 84 | snprintf(out, out_size, "%d", uid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 88 | static void group2str(gid_t gid, char *out, size_t out_size) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | { |
| 90 | struct group *gr = getgrgid(gid); |
| 91 | if(gr) { |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 92 | strlcpy(out, gr->gr_name, out_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | } else { |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 94 | snprintf(out, out_size, "%d", gid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Andy McFadden | 9feee02 | 2009-09-27 15:13:56 -0700 | [diff] [blame] | 98 | static int show_total_size(const char *dirname, DIR *d, int flags) |
| 99 | { |
| 100 | struct dirent *de; |
| 101 | char tmp[1024]; |
| 102 | struct stat s; |
| 103 | int sum = 0; |
| 104 | |
| 105 | /* run through the directory and sum up the file block sizes */ |
| 106 | while ((de = readdir(d)) != 0) { |
| 107 | if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) |
| 108 | continue; |
| 109 | if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0) |
| 110 | continue; |
| 111 | |
| 112 | if (strcmp(dirname, "/") == 0) |
| 113 | snprintf(tmp, sizeof(tmp), "/%s", de->d_name); |
| 114 | else |
| 115 | snprintf(tmp, sizeof(tmp), "%s/%s", dirname, de->d_name); |
| 116 | |
| 117 | if (lstat(tmp, &s) < 0) { |
| 118 | fprintf(stderr, "stat failed on %s: %s\n", tmp, strerror(errno)); |
| 119 | rewinddir(d); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | sum += s.st_blocks / 2; |
| 124 | } |
| 125 | |
| 126 | printf("total %d\n", sum); |
| 127 | rewinddir(d); |
| 128 | return 0; |
| 129 | } |
| 130 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 131 | static int listfile_size(const char *path, const char *filename, struct stat *s, |
| 132 | int flags) |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 133 | { |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 134 | if(!s || !path) { |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 135 | return -1; |
Andy McFadden | 9feee02 | 2009-09-27 15:13:56 -0700 | [diff] [blame] | 136 | } |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 137 | |
| 138 | /* blocks are 512 bytes, we want output to be KB */ |
Kenny Root | 40dac65 | 2011-07-13 09:14:33 -0700 | [diff] [blame] | 139 | if ((flags & LIST_SIZE) != 0) { |
Mark Salyzyn | 4727c77 | 2014-03-14 13:43:10 -0700 | [diff] [blame] | 140 | printf("%lld ", (long long)s->st_blocks / 2); |
Kenny Root | 40dac65 | 2011-07-13 09:14:33 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if ((flags & LIST_CLASSIFY) != 0) { |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 144 | char filetype = mode2kind(s->st_mode); |
Kenny Root | 40dac65 | 2011-07-13 09:14:33 -0700 | [diff] [blame] | 145 | if (filetype != 'l') { |
| 146 | printf("%c ", filetype); |
| 147 | } else { |
| 148 | struct stat link_dest; |
| 149 | if (!stat(path, &link_dest)) { |
| 150 | printf("l%c ", mode2kind(link_dest.st_mode)); |
| 151 | } else { |
| 152 | fprintf(stderr, "stat '%s' failed: %s\n", path, strerror(errno)); |
| 153 | printf("l? "); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | printf("%s\n", filename); |
| 159 | |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 163 | static int listfile_long(const char *path, struct stat *s, int flags) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 164 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | char date[32]; |
| 166 | char mode[16]; |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 167 | char user[32]; |
| 168 | char group[32]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | const char *name; |
| 170 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 171 | if(!s || !path) { |
| 172 | return -1; |
| 173 | } |
| 174 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | /* name is anything after the final '/', or the whole path if none*/ |
| 176 | name = strrchr(path, '/'); |
| 177 | if(name == 0) { |
| 178 | name = path; |
| 179 | } else { |
| 180 | name++; |
| 181 | } |
| 182 | |
Elliott Hughes | fd4c6b0 | 2014-07-22 17:20:15 -0700 | [diff] [blame] | 183 | strmode(s->st_mode, mode); |
Brad Fitzpatrick | e7fe5bf | 2010-10-25 13:29:53 -0700 | [diff] [blame] | 184 | if (flags & LIST_LONG_NUMERIC) { |
Elliott Hughes | ccecf14 | 2014-01-16 10:53:11 -0800 | [diff] [blame] | 185 | snprintf(user, sizeof(user), "%u", s->st_uid); |
| 186 | snprintf(group, sizeof(group), "%u", s->st_gid); |
Brad Fitzpatrick | e7fe5bf | 2010-10-25 13:29:53 -0700 | [diff] [blame] | 187 | } else { |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 188 | user2str(s->st_uid, user, sizeof(user)); |
| 189 | group2str(s->st_gid, group, sizeof(group)); |
Brad Fitzpatrick | e7fe5bf | 2010-10-25 13:29:53 -0700 | [diff] [blame] | 190 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 192 | strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s->st_mtime)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | date[31] = 0; |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 194 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 |
| 196 | // MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK) |
| 197 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 198 | switch(s->st_mode & S_IFMT) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | case S_IFBLK: |
| 200 | case S_IFCHR: |
| 201 | printf("%s %-8s %-8s %3d, %3d %s %s\n", |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 202 | mode, user, group, |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 203 | (int) MAJOR(s->st_rdev), (int) MINOR(s->st_rdev), |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | date, name); |
| 205 | break; |
| 206 | case S_IFREG: |
Kenny Root | eb42170 | 2010-06-25 09:08:05 -0700 | [diff] [blame] | 207 | printf("%s %-8s %-8s %8lld %s %s\n", |
Mark Salyzyn | 4727c77 | 2014-03-14 13:43:10 -0700 | [diff] [blame] | 208 | mode, user, group, (long long)s->st_size, date, name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | break; |
| 210 | case S_IFLNK: { |
| 211 | char linkto[256]; |
Nick Kralevich | 8dfdf65 | 2013-11-27 16:12:48 -0800 | [diff] [blame] | 212 | ssize_t len; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | |
| 214 | len = readlink(path, linkto, 256); |
| 215 | if(len < 0) return -1; |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 216 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | if(len > 255) { |
| 218 | linkto[252] = '.'; |
| 219 | linkto[253] = '.'; |
| 220 | linkto[254] = '.'; |
| 221 | linkto[255] = 0; |
| 222 | } else { |
| 223 | linkto[len] = 0; |
| 224 | } |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 225 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | printf("%s %-8s %-8s %s %s -> %s\n", |
| 227 | mode, user, group, date, name, linkto); |
| 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | printf("%s %-8s %-8s %s %s\n", |
| 232 | mode, user, group, date, name); |
| 233 | |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Nick Kralevich | 8dfdf65 | 2013-11-27 16:12:48 -0800 | [diff] [blame] | 238 | static int listfile_maclabel(const char *path, struct stat *s) |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 239 | { |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 240 | char mode[16]; |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 241 | char user[32]; |
| 242 | char group[32]; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 243 | char *maclabel = NULL; |
| 244 | const char *name; |
| 245 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 246 | if(!s || !path) { |
| 247 | return -1; |
| 248 | } |
| 249 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 250 | /* name is anything after the final '/', or the whole path if none*/ |
| 251 | name = strrchr(path, '/'); |
| 252 | if(name == 0) { |
| 253 | name = path; |
| 254 | } else { |
| 255 | name++; |
| 256 | } |
| 257 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 258 | lgetfilecon(path, &maclabel); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 259 | if (!maclabel) { |
| 260 | return -1; |
| 261 | } |
| 262 | |
Elliott Hughes | fd4c6b0 | 2014-07-22 17:20:15 -0700 | [diff] [blame] | 263 | strmode(s->st_mode, mode); |
Nick Kralevich | 4ec2910 | 2013-11-27 15:09:43 -0800 | [diff] [blame] | 264 | user2str(s->st_uid, user, sizeof(user)); |
| 265 | group2str(s->st_gid, group, sizeof(group)); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 266 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 267 | switch(s->st_mode & S_IFMT) { |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 268 | case S_IFLNK: { |
| 269 | char linkto[256]; |
Kenny Root | ef5d034 | 2012-10-10 11:26:54 -0700 | [diff] [blame] | 270 | ssize_t len; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 271 | |
| 272 | len = readlink(path, linkto, sizeof(linkto)); |
| 273 | if(len < 0) return -1; |
| 274 | |
Kenny Root | ef5d034 | 2012-10-10 11:26:54 -0700 | [diff] [blame] | 275 | if((size_t)len > sizeof(linkto)-1) { |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 276 | linkto[sizeof(linkto)-4] = '.'; |
| 277 | linkto[sizeof(linkto)-3] = '.'; |
| 278 | linkto[sizeof(linkto)-2] = '.'; |
| 279 | linkto[sizeof(linkto)-1] = 0; |
| 280 | } else { |
| 281 | linkto[len] = 0; |
| 282 | } |
| 283 | |
| 284 | printf("%s %-8s %-8s %s %s -> %s\n", |
| 285 | mode, user, group, maclabel, name, linkto); |
| 286 | break; |
| 287 | } |
| 288 | default: |
| 289 | printf("%s %-8s %-8s %s %s\n", |
| 290 | mode, user, group, maclabel, name); |
| 291 | |
| 292 | } |
| 293 | |
| 294 | free(maclabel); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 299 | static int listfile(const char *dirname, const char *filename, int flags) |
| 300 | { |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 301 | struct stat s; |
| 302 | |
| 303 | if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL | LIST_INODE)) == 0) { |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 304 | printf("%s\n", filename); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | char tmp[4096]; |
| 309 | const char* pathname = filename; |
| 310 | |
| 311 | if (dirname != NULL) { |
| 312 | snprintf(tmp, sizeof(tmp), "%s/%s", dirname, filename); |
| 313 | pathname = tmp; |
| 314 | } else { |
| 315 | pathname = filename; |
| 316 | } |
| 317 | |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 318 | if(lstat(pathname, &s) < 0) { |
William Roberts | ded3464 | 2014-01-02 14:34:54 -0800 | [diff] [blame] | 319 | fprintf(stderr, "lstat '%s' failed: %s\n", pathname, strerror(errno)); |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | if(flags & LIST_INODE) { |
Mark Salyzyn | 4727c77 | 2014-03-14 13:43:10 -0700 | [diff] [blame] | 324 | printf("%8llu ", (unsigned long long)s.st_ino); |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 327 | if ((flags & LIST_MACLABEL) != 0) { |
Nick Kralevich | 8dfdf65 | 2013-11-27 16:12:48 -0800 | [diff] [blame] | 328 | return listfile_maclabel(pathname, &s); |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 329 | } else if ((flags & LIST_LONG) != 0) { |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 330 | return listfile_long(pathname, &s, flags); |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 331 | } else /*((flags & LIST_SIZE) != 0)*/ { |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 332 | return listfile_size(pathname, filename, &s, flags); |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | static int listdir(const char *name, int flags) |
| 337 | { |
| 338 | char tmp[4096]; |
| 339 | DIR *d; |
| 340 | struct dirent *de; |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 341 | strlist_t files = STRLIST_INITIALIZER; |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 342 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | d = opendir(name); |
| 344 | if(d == 0) { |
| 345 | fprintf(stderr, "opendir failed, %s\n", strerror(errno)); |
| 346 | return -1; |
| 347 | } |
| 348 | |
Andy McFadden | 9feee02 | 2009-09-27 15:13:56 -0700 | [diff] [blame] | 349 | if ((flags & LIST_SIZE) != 0) { |
| 350 | show_total_size(name, d, flags); |
| 351 | } |
| 352 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 353 | while((de = readdir(d)) != 0){ |
| 354 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; |
| 355 | if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue; |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 356 | |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 357 | strlist_append_dup(&files, de->d_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 358 | } |
| 359 | |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 360 | strlist_sort(&files); |
| 361 | STRLIST_FOREACH(&files, filename, listfile(name, filename, flags)); |
| 362 | strlist_done(&files); |
| 363 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 364 | if (flags & LIST_RECURSIVE) { |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 365 | strlist_t subdirs = STRLIST_INITIALIZER; |
| 366 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 367 | rewinddir(d); |
| 368 | |
| 369 | while ((de = readdir(d)) != 0) { |
| 370 | struct stat s; |
| 371 | int err; |
| 372 | |
| 373 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
| 374 | continue; |
| 375 | if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0) |
| 376 | continue; |
| 377 | |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 378 | if (!strcmp(name, "/")) |
| 379 | snprintf(tmp, sizeof(tmp), "/%s", de->d_name); |
| 380 | else |
| 381 | snprintf(tmp, sizeof(tmp), "%s/%s", name, de->d_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * If the name ends in a '/', use stat() so we treat it like a |
| 385 | * directory even if it's a symlink. |
| 386 | */ |
| 387 | if (tmp[strlen(tmp)-1] == '/') |
| 388 | err = stat(tmp, &s); |
| 389 | else |
| 390 | err = lstat(tmp, &s); |
| 391 | |
| 392 | if (err < 0) { |
| 393 | perror(tmp); |
| 394 | closedir(d); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | if (S_ISDIR(s.st_mode)) { |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 399 | strlist_append_dup(&subdirs, tmp); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 402 | strlist_sort(&subdirs); |
| 403 | STRLIST_FOREACH(&subdirs, path, { |
| 404 | printf("\n%s:\n", path); |
| 405 | listdir(path, flags); |
| 406 | }); |
| 407 | strlist_done(&subdirs); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | closedir(d); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int listpath(const char *name, int flags) |
| 415 | { |
| 416 | struct stat s; |
| 417 | int err; |
| 418 | |
| 419 | /* |
| 420 | * If the name ends in a '/', use stat() so we treat it like a |
| 421 | * directory even if it's a symlink. |
| 422 | */ |
| 423 | if (name[strlen(name)-1] == '/') |
| 424 | err = stat(name, &s); |
| 425 | else |
| 426 | err = lstat(name, &s); |
| 427 | |
| 428 | if (err < 0) { |
| 429 | perror(name); |
| 430 | return -1; |
| 431 | } |
| 432 | |
Andy McFadden | b33d341 | 2009-04-08 19:25:35 -0700 | [diff] [blame] | 433 | if ((flags & LIST_DIRECTORIES) == 0 && S_ISDIR(s.st_mode)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | if (flags & LIST_RECURSIVE) |
| 435 | printf("\n%s:\n", name); |
| 436 | return listdir(name, flags); |
| 437 | } else { |
Andy McFadden | 327e696 | 2009-08-18 11:10:03 -0700 | [diff] [blame] | 438 | /* yeah this calls stat() again*/ |
| 439 | return listfile(NULL, name, flags); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | int ls_main(int argc, char **argv) |
| 444 | { |
| 445 | int flags = 0; |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 446 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 447 | if(argc > 1) { |
| 448 | int i; |
| 449 | int err = 0; |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 450 | strlist_t files = STRLIST_INITIALIZER; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 451 | |
| 452 | for (i = 1; i < argc; i++) { |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 453 | if (argv[i][0] == '-') { |
| 454 | /* an option ? */ |
| 455 | const char *arg = argv[i]+1; |
| 456 | while (arg[0]) { |
| 457 | switch (arg[0]) { |
| 458 | case 'l': flags |= LIST_LONG; break; |
Brad Fitzpatrick | e7fe5bf | 2010-10-25 13:29:53 -0700 | [diff] [blame] | 459 | case 'n': flags |= LIST_LONG | LIST_LONG_NUMERIC; break; |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 460 | case 's': flags |= LIST_SIZE; break; |
| 461 | case 'R': flags |= LIST_RECURSIVE; break; |
| 462 | case 'd': flags |= LIST_DIRECTORIES; break; |
Stephen Smalley | 8290d10 | 2012-01-13 08:53:56 -0500 | [diff] [blame] | 463 | case 'Z': flags |= LIST_MACLABEL; break; |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 464 | case 'a': flags |= LIST_ALL; break; |
Kenny Root | 40dac65 | 2011-07-13 09:14:33 -0700 | [diff] [blame] | 465 | case 'F': flags |= LIST_CLASSIFY; break; |
William Roberts | 403b195 | 2013-04-03 16:01:30 -0700 | [diff] [blame] | 466 | case 'i': flags |= LIST_INODE; break; |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 467 | default: |
| 468 | fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]); |
| 469 | exit(1); |
| 470 | } |
| 471 | arg++; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | } |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 473 | } else { |
| 474 | /* not an option ? */ |
| 475 | strlist_append_dup(&files, argv[i]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
David 'Digit' Turner | aa2106b | 2010-06-04 10:44:56 -0700 | [diff] [blame] | 479 | if (files.count > 0) { |
| 480 | STRLIST_FOREACH(&files, path, { |
| 481 | if (listpath(path, flags) != 0) { |
| 482 | err = EXIT_FAILURE; |
| 483 | } |
| 484 | }); |
| 485 | strlist_done(&files); |
| 486 | return err; |
| 487 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | } |
David 'Digit' Turner | a8d1afb | 2011-01-06 08:39:44 +0100 | [diff] [blame] | 489 | |
| 490 | // list working directory if no files or directories were specified |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | return listpath(".", flags); |
| 492 | } |