/*
display by column original ideas from ls applet,
very optimize by my :)
*/
static void showfiles(char **matches, int nfiles)
{
int ncols, row;
int column_width = 0;
int nrows = nfiles;
/* find the longest file name- use that as the column width */
for (row = 0; row < nrows; row++) {
int l = strlen(matches[row]);
if (column_width < l)
column_width = l;
}
column_width += 2; /* min space for columns */
ncols = cmdedit_termw / column_width;
if (ncols > 1) {
nrows /= ncols;
if(nfiles % ncols)
nrows++; /* round up fractionals */
column_width = -column_width; /* for printf("%-Ns", ...); */
} else {
ncols = 1;
}
for (row = 0; row < nrows; row++) {
int n = row;
int nc;
#ifdef CONFIG_FEATURE_AUTOWIDTH
/* Obtain the terminal width. */
get_terminal_width_height(STDOUT_FILENO, &terminal_width, NULL);
/* Go one less... */
terminal_width--;
#endif
#ifdef CONFIG_FEATURE_LS_COLOR
if (isatty(STDOUT_FILENO))
show_color = 1;
#endif
/* process options */
#ifdef CONFIG_FEATURE_AUTOWIDTH
opt = bb_getopt_ulflags(argc, argv, ls_options, &tabstops_str, &terminal_width_str);
if (tabstops_str) {
tabstops = atoi(tabstops_str);
}
if (terminal_width_str) {
terminal_width = atoi(terminal_width_str);
}
#else
opt = bb_getopt_ulflags(argc, argv, ls_options);
#endif
for (i = 0; opt_flags[i] != (1U<<31); i++) {
if (opt & (1 << i)) {
unsigned int flags = opt_flags[i];
if (flags & LIST_MASK_TRIGGER) {
all_fmt &= ~LIST_MASK;
}
if (flags & STYLE_MASK_TRIGGER) {
all_fmt &= ~STYLE_MASK;
}
#ifdef CONFIG_FEATURE_LS_SORTFILES
if (flags & SORT_MASK_TRIGGER) {
all_fmt &= ~SORT_MASK;
}
#endif
if (flags & DISP_MASK_TRIGGER) {
all_fmt &= ~DISP_MASK;
}
#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
if (flags & TIME_MASK_TRIGGER) {
all_fmt &= ~TIME_MASK;
}
#endif
if (flags & LIST_CONTEXT) {
all_fmt |= STYLE_SINGLE;
}
#ifdef CONFIG_FEATURE_HUMAN_READABLE
if (opt == 'l') {
all_fmt &= ~LS_DISP_HR;
}
#endif
all_fmt |= flags;
}
}
/* sort out which command line options take precedence */
#ifdef CONFIG_FEATURE_LS_RECURSIVE
if (all_fmt & DISP_NOLIST)
all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
#endif
#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
if (all_fmt & TIME_CHANGE)
all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
if (all_fmt & TIME_ACCESS)
all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
#endif
if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
#ifdef CONFIG_FEATURE_LS_USERNAME
if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
#endif
/*
* when there are no cmd line args we have to supply a default "." arg.
* we will create a second argv array, "av" that will hold either
* our created "." arg, or the real cmd line args. The av array
* just holds the pointers- we don't move the date the pointers
* point to.
*/
ac = argc - optind; /* how many cmd line args are left */
if (ac < 1) {
av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *)));
av[0] = bb_xstrdup(".");
ac = 1;
} else {
av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *)));
for (oi = 0; oi < ac; oi++) {
av[oi] = argv[optind++]; /* copy pointer to real cmd line arg */
}
}
/* now, everything is in the av array */
if (ac > 1)
all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
/* stuff the command line file names into an dnode array */
dn = NULL;
for (oi = 0; oi < ac; oi++) {
char *fullname = bb_xstrdup(av[oi]);
cur = my_stat(fullname, fullname);
if (!cur)
continue;
cur->next = dn;
dn = cur;
nfiles++;
}
/* now that we know how many files there are
** allocate memory for an array to hold dnode pointers
*/
dnp = dnalloc(nfiles);
for (i = 0, cur = dn; i < nfiles; i++) {
dnp[i] = cur; /* save pointer to node in array */
cur = cur->next;
}