ls: code shrink

function                                             old     new   delta
list_single                                          990     961     -29

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-05-13 17:27:15 +02:00
parent 3a240212d9
commit d27ac299eb

View File

@ -659,23 +659,19 @@ static unsigned print_name(const char *name)
static NOINLINE unsigned list_single(const struct dnode *dn)
{
unsigned column = 0;
char *lpath = lpath; /* for compiler */
char *lpath;
#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
struct stat info;
char append;
#endif
/* Never happens:
if (dn->fullname == NULL)
return 0;
*/
#if ENABLE_FEATURE_LS_FILETYPES
append = append_char(dn->dstat.st_mode);
#endif
/* Do readlink early, so that if it fails, error message
* does not appear *inside* the "ls -l" line */
lpath = NULL;
if (all_fmt & LIST_SYMLINK)
if (S_ISLNK(dn->dstat.st_mode))
lpath = xmalloc_readlink_or_warn(dn->fullname);
@ -763,7 +759,7 @@ static NOINLINE unsigned list_single(const struct dnode *dn)
#if ENABLE_FEATURE_LS_COLOR
if (show_color) {
info.st_mode = 0; /* for fgcolor() */
info.st_mode = 0;
lstat(dn->fullname, &info);
printf("\033[%u;%um", bold(info.st_mode),
fgcolor(info.st_mode));
@ -774,16 +770,14 @@ static NOINLINE unsigned list_single(const struct dnode *dn)
printf("\033[0m");
}
if (all_fmt & LIST_SYMLINK) {
if (S_ISLNK(dn->dstat.st_mode) && lpath) {
if (lpath) {
printf(" -> ");
#if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
#if ENABLE_FEATURE_LS_COLOR
info.st_mode = 0; /* for fgcolor() */
#endif
if (stat(dn->fullname, &info) == 0) {
info.st_mode = 0;
stat(dn->fullname, &info);
# if ENABLE_FEATURE_LS_FILETYPES
append = append_char(info.st_mode);
}
# endif
#endif
#if ENABLE_FEATURE_LS_COLOR
if (show_color) {
@ -797,7 +791,6 @@ static NOINLINE unsigned list_single(const struct dnode *dn)
}
free(lpath);
}
}
#if ENABLE_FEATURE_LS_FILETYPES
if (all_fmt & LIST_FILETYPE) {
if (append) {
@ -902,9 +895,7 @@ static struct dnode **list_dir(const char *, unsigned *);
static void showdirs(struct dnode **dn, int first)
{
unsigned nfiles;
unsigned dndirs;
struct dnode **subdnp;
struct dnode **dnd;
for (; *dn; dn++) {
if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
@ -925,6 +916,8 @@ static void showdirs(struct dnode **dn, int first)
if (ENABLE_FEATURE_LS_RECURSIVE
&& (all_fmt & DISP_RECURSIVE)
) {
struct dnode **dnd;
unsigned dndirs;
/* recursive - list the sub-dirs */
dnd = splitdnarray(subdnp, SPLIT_SUBDIR);
dndirs = count_dirs(subdnp, SPLIT_SUBDIR);
@ -950,11 +943,6 @@ static struct dnode **list_dir(const char *path, unsigned *nfiles_p)
DIR *dir;
unsigned i, nfiles;
/* Never happens:
if (path == NULL)
return NULL;
*/
*nfiles_p = 0;
dir = warn_opendir(path);
if (dir == NULL) {
@ -1150,6 +1138,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
)
/* ... or if -H: */
|| (option_mask32 & OPT_H)
/* ... or if -L, but my_stat always follows links if -L */
);
argv++;
if (!cur)