diff --git a/usr.bin/top/utils.c b/usr.bin/top/utils.c index 2da52003418..3fccbd45b80 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; + 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; } /* diff --git a/usr.bin/top/utils.h b/usr.bin/top/utils.h index f83174ab0f5..f40f58818d0 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);