aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEitan Adler <eadler@FreeBSD.org>2018-06-04 03:16:24 +0000
committerEitan Adler <eadler@FreeBSD.org>2018-06-04 03:16:24 +0000
commita9a99372d959895d98db720a3fbbdc8e3420c87f (patch)
tree4c40e7c9b8089312ede66da1c6379ac8aa6dded2
parent07d80fd8dc996a84b1f135dc11c6a4b07de6b12b (diff)
downloadsrc-a9a99372d959895d98db720a3fbbdc8e3420c87f.tar.gz
src-a9a99372d959895d98db720a3fbbdc8e3420c87f.zip
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)
Notes
Notes: svn path=/head/; revision=334597
-rw-r--r--usr.bin/top/utils.c83
-rw-r--r--usr.bin/top/utils.h2
2 files changed, 16 insertions, 69 deletions
diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c
index 2da520034186..3fccbd45b803 100644
--- a/usr.bin/top/utils.c
+++ b/usr.bin/top/utils.c
@@ -166,76 +166,23 @@ string_index(const char *string, const char * const *array)
*/
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;
-
- /* 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;
- }
-
- /* 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;
+ const char **ap;
+ static const char *argv[1024] = {0};
+
+ *cntp = 1;
+ ap = &argv[1];
+ while ((*ap = strsep(&line, " ")) != NULL) {
+ if (**ap != '\0') {
+ (*cntp)++;
+ if (*cntp >= (int)nitems(argv)) {
+ break;
+ }
+ ap++;
+ }
}
- *to++ = '\0';
-
- /* set cntp and return the allocated array */
- *cntp = cnt;
- return(argarray);
+ return argv;
}
/*
diff --git a/usr.bin/top/utils.h b/usr.bin/top/utils.h
index f83174ab0f5e..f40f58818d0b 100644
--- a/usr.bin/top/utils.h
+++ b/usr.bin/top/utils.h
@@ -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);