From owner-svn-src-head@freebsd.org Mon Jun 4 03:16:25 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 20EAAFE5371; Mon, 4 Jun 2018 03:16:25 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C25F26D11B; Mon, 4 Jun 2018 03:16:24 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9EFAE279EC; Mon, 4 Jun 2018 03:16:24 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w543GOoL010338; Mon, 4 Jun 2018 03:16:24 GMT (envelope-from eadler@FreeBSD.org) Received: (from eadler@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w543GO4Y010337; Mon, 4 Jun 2018 03:16:24 GMT (envelope-from eadler@FreeBSD.org) Message-Id: <201806040316.w543GO4Y010337@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eadler set sender to eadler@FreeBSD.org using -f From: Eitan Adler Date: Mon, 4 Jun 2018 03:16:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334597 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: eadler X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 334597 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Jun 2018 03:16:25 -0000 Author: eadler Date: Mon Jun 4 03:16:24 2018 New Revision: 334597 URL: https://svnweb.freebsd.org/changeset/base/334597 Log: top(1): Use strsep instead of homegrown alternative This replaces some complex, and not quite correct logic, with a more common strsep pattern. Reviewed by: mmacy (older version) Modified: head/usr.bin/top/utils.c head/usr.bin/top/utils.h Modified: head/usr.bin/top/utils.c ============================================================================== --- head/usr.bin/top/utils.c Mon Jun 4 02:05:48 2018 (r334596) +++ head/usr.bin/top/utils.c Mon Jun 4 03:16:24 2018 (r334597) @@ -166,76 +166,23 @@ string_index(const char *string, const char * const *a */ const char * const * -argparse(const char *line, int *cntp) +argparse(char *line, int *cntp) { - const char *from; - char *to; - int cnt; - int ch; - int length; - int lastch; - char **argv; - const char * const *argarray; - char *args; + const char **ap; + static const char *argv[1024] = {0}; - /* unfortunately, the only real way to do this is to go thru the - input string twice. */ - - /* step thru the string counting the white space sections */ - from = line; - lastch = cnt = length = 0; - while ((ch = *from++) != '\0') - { - length++; - if (ch == ' ' && lastch != ' ') - { - cnt++; - } - lastch = ch; + *cntp = 1; + ap = &argv[1]; + while ((*ap = strsep(&line, " ")) != NULL) { + if (**ap != '\0') { + (*cntp)++; + if (*cntp >= (int)nitems(argv)) { + break; + } + ap++; + } } - - /* add three to the count: one for the initial "dummy" argument, - one for the last argument and one for NULL */ - cnt += 3; - - /* allocate a char * array to hold the pointers */ - argarray = calloc(cnt, sizeof(char *)); - - /* allocate another array to hold the strings themselves */ - args = calloc(length+2, 1); - - /* initialization for main loop */ - from = line; - to = args; - argv = argarray; - lastch = '\0'; - - /* create a dummy argument to keep getopt happy */ - *argv++ = to; - *to++ = '\0'; - cnt = 2; - - /* now build argv while copying characters */ - *argv++ = to; - while ((ch = *from++) != '\0') - { - if (ch != ' ') - { - if (lastch == ' ') - { - *to++ = '\0'; - *argv++ = to; - cnt++; - } - *to++ = ch; - } - lastch = ch; - } - *to++ = '\0'; - - /* set cntp and return the allocated array */ - *cntp = cnt; - return(argarray); + return argv; } /* Modified: head/usr.bin/top/utils.h ============================================================================== --- head/usr.bin/top/utils.h Mon Jun 4 02:05:48 2018 (r334596) +++ head/usr.bin/top/utils.h Mon Jun 4 03:16:24 2018 (r334597) @@ -16,7 +16,7 @@ int atoiwi(const char *); char *itoa(unsigned int); char *itoa7(int); int digits(int); -const char * const *argparse(const char *, int *); +const char * const *argparse(char *, int *); long percentages(int, int *, long *, long *, long *); char *format_time(long); char *format_k(int);