diff options
author | Andy McFadden <fadden@android.com> | 2009-09-27 15:13:56 -0700 |
---|---|---|
committer | Andy McFadden <fadden@android.com> | 2009-09-27 15:13:56 -0700 |
commit | 9feee025a09ace19b09dbb41d6ba87198c77cdf1 (patch) | |
tree | f101761e91ea1f6f09cdcb087a43a2294a7e158f /toolbox/ls.c | |
parent | 8a0ba1b1e4f5117da63b8e530ef7c6ba3dc8838c (diff) | |
download | system_core-9feee025a09ace19b09dbb41d6ba87198c77cdf1.zip system_core-9feee025a09ace19b09dbb41d6ba87198c77cdf1.tar.gz system_core-9feee025a09ace19b09dbb41d6ba87198c77cdf1.tar.bz2 |
Fix up ls -s.
Added the "total" line, which makes it easy to answer questions like,
"how the hell much space am I wasting on /data/dalvik-cache?".
Also fixed the -s filename output, which was showing mostly-full paths
when a simple filename was called for.
Diffstat (limited to 'toolbox/ls.c')
-rw-r--r-- | toolbox/ls.c | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/toolbox/ls.c b/toolbox/ls.c index b221074..f340a83 100644 --- a/toolbox/ls.c +++ b/toolbox/ls.c @@ -86,15 +86,50 @@ static void group2str(unsigned gid, char *out) } } -static int listfile_size(const char *path, int flags) +static int show_total_size(const char *dirname, DIR *d, int flags) { + struct dirent *de; + char tmp[1024]; struct stat s; + int sum = 0; + + /* run through the directory and sum up the file block sizes */ + while ((de = readdir(d)) != 0) { + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + if (de->d_name[0] == '.' && (flags & LIST_ALL) == 0) + continue; + + if (strcmp(dirname, "/") == 0) + snprintf(tmp, sizeof(tmp), "/%s", de->d_name); + else + snprintf(tmp, sizeof(tmp), "%s/%s", dirname, de->d_name); + + if (lstat(tmp, &s) < 0) { + fprintf(stderr, "stat failed on %s: %s\n", tmp, strerror(errno)); + rewinddir(d); + return -1; + } + + sum += s.st_blocks / 2; + } - if (lstat(path, &s) < 0) + printf("total %d\n", sum); + rewinddir(d); + return 0; +} + +static int listfile_size(const char *path, const char *filename, int flags) +{ + struct stat s; + + if (lstat(path, &s) < 0) { + fprintf(stderr, "lstat '%s' failed: %s\n", path, strerror(errno)); return -1; + } /* blocks are 512 bytes, we want output to be KB */ - printf("%lld %s\n", s.st_blocks / 2, path); + printf("%lld %s\n", s.st_blocks / 2, filename); return 0; } @@ -189,7 +224,7 @@ static int listfile(const char *dirname, const char *filename, int flags) if ((flags & LIST_LONG) != 0) { return listfile_long(pathname, flags); } else /*((flags & LIST_SIZE) != 0)*/ { - return listfile_size(pathname, flags); + return listfile_size(pathname, filename, flags); } } @@ -198,13 +233,17 @@ static int listdir(const char *name, int flags) char tmp[4096]; DIR *d; struct dirent *de; - + d = opendir(name); if(d == 0) { fprintf(stderr, "opendir failed, %s\n", strerror(errno)); return -1; } + if ((flags & LIST_SIZE) != 0) { + show_total_size(name, d, flags); + } + while((de = readdir(d)) != 0){ if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; if(de->d_name[0] == '.' && (flags & LIST_ALL) == 0) continue; |