Simplify LS_COLWIDTHS processing
The previous version normalized the width list (replaced empty fields with "0") just to be able to use sscanf() on the string. It is much simpler to just parse the string as-is. The clearing of f_notabs is preserved for the case that less than 9 width values have been defined, but I do not understand the rationale for this particular condition. E.g., LS_COLWIDTHS="::::::::" will be counted as 9 defined fields (may clear f_notabs) but is no different fron LS_COLWIDTHS="" with regard to the field width (and that does not clear f_notabs, since there are less than 9 fields).
This commit is contained in:
parent
dbb25cbe55
commit
3fee777ec5
115
bin/ls/ls.c
115
bin/ls/ls.c
@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/mac.h>
|
#include <sys/mac.h>
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -88,12 +89,12 @@ __FBSDID("$FreeBSD$");
|
|||||||
*/
|
*/
|
||||||
#define MAKENINES(n) \
|
#define MAKENINES(n) \
|
||||||
do { \
|
do { \
|
||||||
intmax_t i; \
|
intmax_t __i; \
|
||||||
\
|
\
|
||||||
/* Use a loop as all values of n are small. */ \
|
/* Use a loop as all values of n are small. */ \
|
||||||
for (i = 1; n > 0; i *= 10) \
|
for (__i = 1; n > 0; __i *= 10) \
|
||||||
n--; \
|
n--; \
|
||||||
n = i - 1; \
|
n = __i - 1; \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
static void display(const FTSENT *, FTSENT *, int);
|
static void display(const FTSENT *, FTSENT *, int);
|
||||||
@ -715,88 +716,58 @@ display(const FTSENT *p, FTSENT *list, int options)
|
|||||||
char *flags, *labelstr = NULL;
|
char *flags, *labelstr = NULL;
|
||||||
char ngroup[STRBUF_SIZEOF(uid_t) + 1];
|
char ngroup[STRBUF_SIZEOF(uid_t) + 1];
|
||||||
char nuser[STRBUF_SIZEOF(gid_t) + 1];
|
char nuser[STRBUF_SIZEOF(gid_t) + 1];
|
||||||
|
u_long width[9];
|
||||||
|
int i;
|
||||||
|
|
||||||
needstats = f_inode || f_longform || f_size;
|
needstats = f_inode || f_longform || f_size;
|
||||||
flen = 0;
|
flen = 0;
|
||||||
btotal = 0;
|
btotal = 0;
|
||||||
initmax = getenv("LS_COLWIDTHS");
|
|
||||||
/* Fields match -lios order. New ones should be added at the end. */
|
|
||||||
maxlabelstr = maxblock = maxlen = maxnlink = 0;
|
|
||||||
maxuser = maxgroup = maxflags = maxsize = 0;
|
|
||||||
maxinode = 0;
|
|
||||||
if (initmax != NULL && *initmax != '\0') {
|
|
||||||
char *initmax2, *jinitmax;
|
|
||||||
int ninitmax;
|
|
||||||
|
|
||||||
/* Fill-in "::" as "0:0:0" for the sake of scanf. */
|
#define LS_COLWIDTHS_FIELDS 9
|
||||||
jinitmax = malloc(strlen(initmax) * 2 + 2);
|
initmax = getenv("LS_COLWIDTHS");
|
||||||
if (jinitmax == NULL)
|
|
||||||
err(1, "malloc");
|
for (i = 0 ; i < LS_COLWIDTHS_FIELDS; i++)
|
||||||
initmax2 = jinitmax;
|
width[i] = 0;
|
||||||
if (*initmax == ':')
|
|
||||||
strcpy(initmax2, "0:"), initmax2 += 2;
|
if (initmax != NULL) {
|
||||||
else
|
char *endp;
|
||||||
*initmax2++ = *initmax, *initmax2 = '\0';
|
|
||||||
for (initmax++; *initmax != '\0'; initmax++) {
|
for (i = 0; i < LS_COLWIDTHS_FIELDS && *initmax != '\0'; i++) {
|
||||||
if (initmax[-1] == ':' && initmax[0] == ':') {
|
if (*initmax == ':') {
|
||||||
*initmax2++ = '0';
|
width[i] = 0;
|
||||||
*initmax2++ = initmax[0];
|
|
||||||
initmax2[1] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
*initmax2++ = initmax[0];
|
width[i] = strtoul(initmax, &endp, 10);
|
||||||
initmax2[1] = '\0';
|
initmax = endp;
|
||||||
|
while (isspace(*initmax))
|
||||||
|
initmax++;
|
||||||
|
if (*initmax != ':')
|
||||||
|
break;
|
||||||
|
initmax++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (initmax2[-1] == ':')
|
if (i < LS_COLWIDTHS_FIELDS)
|
||||||
strcpy(initmax2, "0");
|
|
||||||
|
|
||||||
ninitmax = sscanf(jinitmax,
|
|
||||||
" %ju : %ld : %lu : %u : %u : %i : %jd : %lu : %lu ",
|
|
||||||
&maxinode, &maxblock, &maxnlink, &maxuser,
|
|
||||||
&maxgroup, &maxflags, &maxsize, &maxlen, &maxlabelstr);
|
|
||||||
f_notabs = 1;
|
|
||||||
switch (ninitmax) {
|
|
||||||
case 0:
|
|
||||||
maxinode = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 1:
|
|
||||||
maxblock = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 2:
|
|
||||||
maxnlink = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 3:
|
|
||||||
maxuser = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 4:
|
|
||||||
maxgroup = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 5:
|
|
||||||
maxflags = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 6:
|
|
||||||
maxsize = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 7:
|
|
||||||
maxlen = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
case 8:
|
|
||||||
maxlabelstr = 0;
|
|
||||||
/* FALLTHROUGH */
|
|
||||||
#ifdef COLORLS
|
#ifdef COLORLS
|
||||||
if (!f_color)
|
if (!f_color)
|
||||||
#endif
|
#endif
|
||||||
f_notabs = 0;
|
f_notabs = 0;
|
||||||
/* FALLTHROUGH */
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
MAKENINES(maxinode);
|
|
||||||
MAKENINES(maxblock);
|
|
||||||
MAKENINES(maxnlink);
|
|
||||||
MAKENINES(maxsize);
|
|
||||||
free(jinitmax);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Fields match -lios order. New ones should be added at the end. */
|
||||||
|
maxinode = width[0];
|
||||||
|
maxblock = width[1];
|
||||||
|
maxnlink = width[2];
|
||||||
|
maxuser = width[3];
|
||||||
|
maxgroup = width[4];
|
||||||
|
maxflags = width[5];
|
||||||
|
maxsize = width[6];
|
||||||
|
maxlen = width[7];
|
||||||
|
maxlabelstr = width[8];
|
||||||
|
|
||||||
|
MAKENINES(maxinode);
|
||||||
|
MAKENINES(maxblock);
|
||||||
|
MAKENINES(maxnlink);
|
||||||
|
MAKENINES(maxsize);
|
||||||
|
|
||||||
d.s_size = 0;
|
d.s_size = 0;
|
||||||
sizelen = 0;
|
sizelen = 0;
|
||||||
flags = NULL;
|
flags = NULL;
|
||||||
|
Loading…
Reference in New Issue
Block a user