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