Date: Mon, 17 Sep 2001 21:53:45 -0500 From: Chris Costello <chris@FreeBSD.ORG> To: freebsd-binup@FreeBSD.ORG Subject: Re: common/wrappers.c: Updated getArgs(). Message-ID: <20010917215345.D17889@holly.calldei.com> In-Reply-To: <20010917214616.C17889@holly.calldei.com>; from chris@FreeBSD.ORG on Mon, Sep 17, 2001 at 09:46:16PM -0500 References: <20010917214616.C17889@holly.calldei.com>
index | next in thread | previous in thread | raw e-mail
[-- Attachment #1 --]
Er, that's an out-of-date diff with a few pieces of
useless/ugly cruft (where the code incremented the number of
allocated pointers by three, rather than one). A more
thought-out patch is attached.
--
+-------------------+-----------------------+
| Chris Costello | Old mail has arrived. |
| chris@FreeBSD.org | |
+-------------------+-----------------------+
[-- Attachment #2 --]
--- wrappers.c.orig Mon Sep 17 12:36:26 2001
+++ wrappers.c Mon Sep 17 21:51:39 2001
@@ -42,31 +42,24 @@
* Will return an array with 'arg1', 'arg2', 'arg3', and n will contain '3'
*/
+#define ALLOC_INCREMENT 1
+
char **
-getArgs(char *command, int *n) {
- /* XXX evil constant */
-#define MAXARGS 10
- char **args = Malloc(sizeof(char *) * MAXARGS);
- int num=0,i;
- if (command == NULL) {
- *n=num;
- return NULL;
+getArgs(char *s, int *args)
+{
+ char **ret;
+ char *c;
+ int n, nalloc;
+
+ ret = (char **)malloc((nalloc = ALLOC_INCREMENT) * sizeof(char *));
+
+ for (c = strtok(s, " \t\n"), n = 0; c; c = strtok(NULL, " \t\n"), n++) {
+ if (n == nalloc)
+ ret = realloc(ret, (nalloc += ALLOC_INCREMENT));
+ ret[n] = c;
}
- /* Skip leading whitespace */
- for (i=0;i<MAXARGS;i++) {
- if (command == NULL) {
- *n=num;
- return args;
- }
- while (isspace(command[0])) command++;
- if (command == NULL) {
- *n=num;
- return args;
- }
- args[i] = strsep(&command, " ");
- num++;
- }
- return (args);
+ *args = n;
+ return (ret);
}
/* This function removes the end-of-line characters from a string. */
home |
help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010917215345.D17889>
