summaryrefslogtreecommitdiffstats
path: root/toolbox
diff options
context:
space:
mode:
authorGeremy Condra <gcondra@google.com>2013-04-04 23:46:55 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-04-04 23:46:55 -0700
commite5920ffdca9974321fd9cb45c5080867dde194d3 (patch)
tree58d950cd4de615fa30e14d110a2d8662493b43bc /toolbox
parent7d960c961875a5d7ad6d577974c0e3e2b17cf51e (diff)
parenta7096004f220dd7578d7b2e26a3513fa5efc0fc1 (diff)
downloadcore-e5920ffdca9974321fd9cb45c5080867dde194d3.tar.gz
core-e5920ffdca9974321fd9cb45c5080867dde194d3.tar.bz2
core-e5920ffdca9974321fd9cb45c5080867dde194d3.zip
am a7096004: am 911ba269: Merge "Add -i option to toolbox cmd ls"
* commit 'a7096004f220dd7578d7b2e26a3513fa5efc0fc1': Add -i option to toolbox cmd ls
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/ls.c80
1 files changed, 44 insertions, 36 deletions
diff --git a/toolbox/ls.c b/toolbox/ls.c
index e5305212b..532451113 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -28,6 +28,7 @@
#define LIST_LONG_NUMERIC (1 << 5)
#define LIST_CLASSIFY (1 << 6)
#define LIST_MACLABEL (1 << 7)
+#define LIST_INODE (1 << 8)
// fwd
static int listpath(const char *name, int flags);
@@ -127,22 +128,20 @@ static int show_total_size(const char *dirname, DIR *d, int flags)
return 0;
}
-static int listfile_size(const char *path, const char *filename, int flags)
+static int listfile_size(const char *path, const char *filename, struct stat *s,
+ int flags)
{
- struct stat s;
-
- if (lstat(path, &s) < 0) {
- fprintf(stderr, "lstat '%s' failed: %s\n", path, strerror(errno));
+ if(!s || !path) {
return -1;
}
/* blocks are 512 bytes, we want output to be KB */
if ((flags & LIST_SIZE) != 0) {
- printf("%lld ", s.st_blocks / 2);
+ printf("%lld ", s->st_blocks / 2);
}
if ((flags & LIST_CLASSIFY) != 0) {
- char filetype = mode2kind(s.st_mode);
+ char filetype = mode2kind(s->st_mode);
if (filetype != 'l') {
printf("%c ", filetype);
} else {
@@ -161,15 +160,18 @@ static int listfile_size(const char *path, const char *filename, int flags)
return 0;
}
-static int listfile_long(const char *path, int flags)
+static int listfile_long(const char *path, struct stat *s, int flags)
{
- struct stat s;
char date[32];
char mode[16];
char user[16];
char group[16];
const char *name;
+ if(!s || !path) {
+ return -1;
+ }
+
/* name is anything after the final '/', or the whole path if none*/
name = strrchr(path, '/');
if(name == 0) {
@@ -178,36 +180,32 @@ static int listfile_long(const char *path, int flags)
name++;
}
- if(lstat(path, &s) < 0) {
- return -1;
- }
-
- mode2str(s.st_mode, mode);
+ mode2str(s->st_mode, mode);
if (flags & LIST_LONG_NUMERIC) {
- sprintf(user, "%ld", s.st_uid);
- sprintf(group, "%ld", s.st_gid);
+ sprintf(user, "%ld", s->st_uid);
+ sprintf(group, "%ld", s->st_gid);
} else {
- user2str(s.st_uid, user);
- group2str(s.st_gid, group);
+ user2str(s->st_uid, user);
+ group2str(s->st_gid, group);
}
- strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime));
+ strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s->st_mtime));
date[31] = 0;
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
// MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK)
- switch(s.st_mode & S_IFMT) {
+ switch(s->st_mode & S_IFMT) {
case S_IFBLK:
case S_IFCHR:
printf("%s %-8s %-8s %3d, %3d %s %s\n",
mode, user, group,
- (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev),
+ (int) MAJOR(s->st_rdev), (int) MINOR(s->st_rdev),
date, name);
break;
case S_IFREG:
printf("%s %-8s %-8s %8lld %s %s\n",
- mode, user, group, s.st_size, date, name);
+ mode, user, group, s->st_size, date, name);
break;
case S_IFLNK: {
char linkto[256];
@@ -237,15 +235,18 @@ static int listfile_long(const char *path, int flags)
return 0;
}
-static int listfile_maclabel(const char *path, int flags)
+static int listfile_maclabel(const char *path, struct stat *s, int flags)
{
- struct stat s;
char mode[16];
char user[16];
char group[16];
char *maclabel = NULL;
const char *name;
+ if(!s || !path) {
+ return -1;
+ }
+
/* name is anything after the final '/', or the whole path if none*/
name = strrchr(path, '/');
if(name == 0) {
@@ -254,20 +255,16 @@ static int listfile_maclabel(const char *path, int flags)
name++;
}
- if(lstat(path, &s) < 0) {
- return -1;
- }
-
lgetfilecon(path, &maclabel);
if (!maclabel) {
return -1;
}
- mode2str(s.st_mode, mode);
- user2str(s.st_uid, user);
- group2str(s.st_gid, group);
+ mode2str(s->st_mode, mode);
+ user2str(s->st_uid, user);
+ group2str(s->st_gid, group);
- switch(s.st_mode & S_IFMT) {
+ switch(s->st_mode & S_IFMT) {
case S_IFLNK: {
char linkto[256];
ssize_t len;
@@ -301,7 +298,9 @@ static int listfile_maclabel(const char *path, int flags)
static int listfile(const char *dirname, const char *filename, int flags)
{
- if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL)) == 0) {
+ struct stat s;
+
+ if ((flags & (LIST_LONG | LIST_SIZE | LIST_CLASSIFY | LIST_MACLABEL | LIST_INODE)) == 0) {
printf("%s\n", filename);
return 0;
}
@@ -316,12 +315,20 @@ static int listfile(const char *dirname, const char *filename, int flags)
pathname = filename;
}
+ if(lstat(pathname, &s) < 0) {
+ return -1;
+ }
+
+ if(flags & LIST_INODE) {
+ printf("%8llu ", s.st_ino);
+ }
+
if ((flags & LIST_MACLABEL) != 0) {
- return listfile_maclabel(pathname, flags);
+ return listfile_maclabel(pathname, &s, flags);
} else if ((flags & LIST_LONG) != 0) {
- return listfile_long(pathname, flags);
+ return listfile_long(pathname, &s, flags);
} else /*((flags & LIST_SIZE) != 0)*/ {
- return listfile_size(pathname, filename, flags);
+ return listfile_size(pathname, filename, &s, flags);
}
}
@@ -456,6 +463,7 @@ int ls_main(int argc, char **argv)
case 'Z': flags |= LIST_MACLABEL; break;
case 'a': flags |= LIST_ALL; break;
case 'F': flags |= LIST_CLASSIFY; break;
+ case 'i': flags |= LIST_INODE; break;
default:
fprintf(stderr, "%s: Unknown option '-%c'. Aborting.\n", "ls", arg[0]);
exit(1);