Commit 92899600 authored by Dustin L. Howett's avatar Dustin L. Howett

Rewrite the argument handler to not use strsep.

Handle string splitting manually, and don't precalculate argc - handle
everything in a single loop. Use strdup to get copies of the argument
strings. Free them later.
parent e712f785
......@@ -57,21 +57,27 @@ _EVENT(PRIVMSG) {
char *commandname = strsep(&msgbuf, " ");
int argc = 0;
char **argv;
int nargmax = 1;
char **argv = (char**)malloc(nargmax * sizeof(char*));
if(msgbuf) {
int argstart = 0;
for(unsigned int i = 0; i <= strlen(msgbuf); ++i) {
if(msgbuf[i] == ' ' || msgbuf[i] == '\0') {
argv[argc] = strndup(msgbuf+argstart, i-argstart);
++argc;
if(argc >= nargmax) {
nargmax += 10;
argv = (char**)realloc(argv, nargmax*sizeof(char*));
}
if(msgbuf[i] == '\0') break;
argstart = i+1;
}
argv = (char**)malloc(argc+1 * sizeof(char*));
int i = 0;
for(i = 0; i < argc; ++i) {
argv[i] = strsep(&msgbuf, " ");
}
argv[i] = NULL;
argv[argc] = NULL;
} else {
argv = (char**)malloc(sizeof(char*));
*argv = NULL;
}
......@@ -84,5 +90,8 @@ _EVENT(PRIVMSG) {
server->printf("PRIVMSG %s :Unknown command: %s", state.channel, commandname);
}
for(unsigned int i = 0; i < argc; ++i) {
free(argv[i]);
}
free(argv);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment