From 9c7dc7fbecf015b087e1626335190e8a456f5da9 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Sun, 1 Nov 2015 14:39:13 +0100 Subject: analyze format to save expensive lookups --- lr.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'lr.c') diff --git a/lr.c b/lr.c index 762d622..510f8cc 100644 --- a/lr.c +++ b/lr.c @@ -92,6 +92,9 @@ struct idmap { char *name; }; +static int need_group; +static int need_user; +static int need_fstype; static off_t maxsize; static nlink_t maxlinks; static int uwid, gwid, fwid; @@ -995,6 +998,25 @@ print_noprefix(struct fileinfo *fi) print_shquoted(basenam(fi->fpath)); } +void +analyze_format() +{ + char *s; + for (s = format; *s; s++) { + if (*s == '\\') { + s++; + continue; + } + if (*s != '%') + continue; + switch (*++s) { + case 'g': need_group++; break; + case 'u': need_user++; break; + case 'Y': need_fstype++; break; + } + } +} + void print_format(struct fileinfo *fi) { @@ -1155,11 +1177,13 @@ callback(const char *fpath, const struct stat *sb, int depth, int entries, off_t maxlinks = fi->sb.st_nlink; if (fi->sb.st_size > maxsize) maxsize = fi->sb.st_size; - if (lflag) { - /* prefetch user/group name for correct column widths. */ + /* prefetch user/group/fs name for correct column widths. */ + if (need_user) username(fi->sb.st_uid); + if (need_group) groupname(fi->sb.st_gid); - } + if (need_fstype) + fstype(fi->sb.st_dev); return 0; } @@ -1300,6 +1324,8 @@ main(int argc, char *argv[]) exit(2); } + analyze_format(); + if (optind == argc) { sflag++; traverse("."); -- cgit 1.4.1