Date: Mon, 17 Sep 2001 21:46:16 -0500 From: Chris Costello <chris@FreeBSD.org> To: freebsd-binup@FreeBSD.org Subject: common/wrappers.c: Updated getArgs(). Message-ID: <20010917214616.C17889@holly.calldei.com>
next in thread | raw e-mail | index | archive | help
--Dxnq1zWXvFF0Q93v Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Just skimming over this, I figure you may need this. I adapted it from some code I've been working on for FreeBSD (a command interpreter of sorts, if you're interested). -- +-------------------+--------------------------------------+ | Chris Costello | Computer modelers simulate it first. | | chris@FreeBSD.org | | +-------------------+--------------------------------------+ --Dxnq1zWXvFF0Q93v Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="wrappers.c.diff" --- wrappers.c.orig Mon Sep 17 12:36:26 2001 +++ wrappers.c Mon Sep 17 12:37:50 2001 @@ -43,30 +43,23 @@ */ 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; + + n = 0; + + ret = (char **)malloc((nalloc = 1) * sizeof(char *)); + + for (c = strtok(s, " \t\n"), n = 0; c; c = strtok(NULL, " \t\n"), n++) { + if (n == nalloc) + ret = realloc(ret, nalloc++); + 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. */ --Dxnq1zWXvFF0Q93v-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-binup" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010917214616.C17889>