Mail Archives: djgpp/1997/04/05/12:35:46
In article <01bc409f$afbb59c0$97c3b8cd AT scully>
scully AT ix DOT netcom DOT com "Cephaler" writes:
> what I need is a simple parser, one that would work with gets(input) (input
*Never* use gets()! Always use fgets(stdin) gets can quite easily overrun
your buffer and destroy your computer.
> = char *input) or scanf("%s",input) and divide each "chunk" of characters
> into seperate strings and put them all into one big array... kind of
> like....
>
> Prog output: Enter a command: _
> User Input: /showfilestats math.c
>
> then, after that, the program parses the user input and puts it into char
> *UserCommand[10] like this:
>
> UserCommand[0] = /showfilestats
> UserCommand[1] = math.c
char buf[BUFSIZE];
char *p;
char *argv[];
int argc=1;
fgets(buf, BUFSIZE, stdin);
argv=malloc(sizeof (char *));
argv[0]=buf;
for(p=buf;*p;p++) {
if (*p==' ') {
*p='\0';
argv=realloc(argv, ++argc * sizeof (char *));
argv[argc-1]=p+1;
}
}
It's *very* simplistic...
--
:sb)
- Raw text -