From owner-dev-commits-src-main@freebsd.org Sun Dec 27 21:32:36 2020 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id CEBE94C7A5E; Sun, 27 Dec 2020 21:32:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4D3v6D5Rv4z3hsp; Sun, 27 Dec 2020 21:32:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ACEC4631B; Sun, 27 Dec 2020 21:32:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 0BRLWalq010562; Sun, 27 Dec 2020 21:32:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 0BRLWadl010561; Sun, 27 Dec 2020 21:32:36 GMT (envelope-from git) Date: Sun, 27 Dec 2020 21:32:36 GMT Message-Id: <202012272132.0BRLWadl010561@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Stefan Eßer Subject: git: 3fee777ec5a2 - main - Simplify LS_COLWIDTHS processing MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: se X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 3fee777ec5a2e91ffbd9b396e99edae386eb150e Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "Commit messages for the main branch of the src repository." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 27 Dec 2020 21:32:36 -0000 The branch main has been updated by se: URL: https://cgit.FreeBSD.org/src/commit/?id=3fee777ec5a2e91ffbd9b396e99edae386eb150e commit 3fee777ec5a2e91ffbd9b396e99edae386eb150e Author: Stefan Eßer AuthorDate: 2020-12-26 21:10:54 +0000 Commit: Stefan Eßer CommitDate: 2020-12-27 21:32:22 +0000 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). --- bin/ls/ls.c | 115 +++++++++++++++++++++++------------------------------------- 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/bin/ls/ls.c b/bin/ls/ls.c index b0fe8d79e3ea..338b3d1d2a26 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -88,12 +89,12 @@ __FBSDID("$FreeBSD$"); */ #define MAKENINES(n) \ do { \ - intmax_t i; \ + intmax_t __i; \ \ /* 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 = i - 1; \ + n = __i - 1; \ } while(0) static void display(const FTSENT *, FTSENT *, int); @@ -715,88 +716,58 @@ display(const FTSENT *p, FTSENT *list, int options) char *flags, *labelstr = NULL; char ngroup[STRBUF_SIZEOF(uid_t) + 1]; char nuser[STRBUF_SIZEOF(gid_t) + 1]; + u_long width[9]; + int i; needstats = f_inode || f_longform || f_size; flen = 0; btotal = 0; + +#define LS_COLWIDTHS_FIELDS 9 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. */ - jinitmax = malloc(strlen(initmax) * 2 + 2); - if (jinitmax == NULL) - err(1, "malloc"); - initmax2 = jinitmax; - if (*initmax == ':') - strcpy(initmax2, "0:"), initmax2 += 2; - else - *initmax2++ = *initmax, *initmax2 = '\0'; - for (initmax++; *initmax != '\0'; initmax++) { - if (initmax[-1] == ':' && initmax[0] == ':') { - *initmax2++ = '0'; - *initmax2++ = initmax[0]; - initmax2[1] = '\0'; + + for (i = 0 ; i < LS_COLWIDTHS_FIELDS; i++) + width[i] = 0; + + if (initmax != NULL) { + char *endp; + + for (i = 0; i < LS_COLWIDTHS_FIELDS && *initmax != '\0'; i++) { + if (*initmax == ':') { + width[i] = 0; } else { - *initmax2++ = initmax[0]; - initmax2[1] = '\0'; + width[i] = strtoul(initmax, &endp, 10); + initmax = endp; + while (isspace(*initmax)) + initmax++; + if (*initmax != ':') + break; + initmax++; } } - if (initmax2[-1] == ':') - 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 */ + if (i < LS_COLWIDTHS_FIELDS) #ifdef COLORLS if (!f_color) #endif 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; sizelen = 0; flags = NULL;