Exemple #1
0
/*----------------------------------------------------------------------*/
static void showfiles(struct dnode **dn, int nfiles)
{
  //printf("=== ls showfiles\n");
	int i, ncols, nrows, row, nc;
	int column = 0;
	int nexttab = 0;
	int column_width = 0; /* for STYLE_LONG and STYLE_SINGLE not used */

	if (dn == NULL || nfiles < 1)
		return;

	if (all_fmt & STYLE_ONE_RECORD_FLAG) {
		ncols = 1;
	} else {
		/* find the longest file name-  use that as the column width */
		for (i = 0; i < nfiles; i++) {
			int len = strlen(dn[i]->name) +
#ifdef CONFIG_SELINUX
			((all_fmt & LIST_CONTEXT) ? 33 : 0) +
#endif
			((all_fmt & LIST_INO) ? 8 : 0) +
			((all_fmt & LIST_BLOCKS) ? 5 : 0);
			if (column_width < len)
				column_width = len;
		}
		column_width += tabstops;
		ncols = (int) (terminal_width / column_width);
	}

	if (ncols > 1) {
		nrows = nfiles / ncols;
		if ((nrows * ncols) < nfiles)
			nrows++;                /* round up fractionals */
	} else {
		nrows = nfiles;
		ncols = 1;
	}

	for (row = 0; row < nrows; row++) {
		for (nc = 0; nc < ncols; nc++) {
			/* reach into the array based on the column and row */
			i = (nc * nrows) + row;	/* assume display by column */
			if (all_fmt & DISP_ROWS)
				i = (row * ncols) + nc;	/* display across row */
			if (i < nfiles) {
				if (column > 0) {
					nexttab -= column;
					while (nexttab--) {
						putchar(' ');
						column++;
					}
			}
				nexttab = column + column_width;
				column += list_single(dn[i]);
		}
		}
		putchar('\n');
		column = 0;
	}
}
/*----------------------------------------------------------------------*/
static void showfiles(struct dnode **dn, int nfiles)
{
	int i, ncols, nrows, row, nc;
#ifdef BB_FEATURE_AUTOWIDTH
	int len;
#endif

	if(dn==NULL || nfiles < 1) return;

#ifdef BB_FEATURE_AUTOWIDTH
	/* find the longest file name-  use that as the column width */
	column_width= 0;
	for (i=0; i<nfiles; i++) {
		len= strlen(dn[i]->name) +
			((list_fmt & LIST_INO) ? 8 : 0) +
			((list_fmt & LIST_BLOCKS) ? 5 : 0)
			;
		if (column_width < len) 
			column_width= len;
	}
	if (column_width >= 6)
		ncols = (int)(terminal_width / (column_width + COLUMN_GAP));
	else {
		ncols = 1;
		column_width = COLUMN_WIDTH;
	}
#else
	ncols= TERMINAL_WIDTH;
#endif
	switch (style_fmt) {
		case STYLE_LONG:	/* one record per line, extended info */
		case STYLE_SINGLE:	/* one record per line */
			ncols= 1;
			break;
	}

	if (ncols > 1) {
		nrows = nfiles / ncols;
	} else {
		nrows = nfiles;
		ncols = 1;
	}
	if ((nrows * ncols) < nfiles) nrows++; /* round up fractionals */

	if (nrows > nfiles) nrows= nfiles;
	for (row=0; row<nrows; row++) {
		for (nc=0; nc<ncols; nc++) {
			/* reach into the array based on the column and row */
			i= (nc * nrows) + row;		/* assume display by column */
			if (disp_opts & DISP_ROWS)
				i= (row * ncols) + nc;	/* display across row */
			if (i < nfiles) {
				nexttabstop();
				list_single(dn[i]);
			}
		}
		newline();
	}
}