here is a code, all aspect should be pationed.
- getting char pointer
- using strdup for using char pointer in strtok_r
- using strtok_r to be threadsafe
- free result strdup when were done cuz it uses malloc inside
give me a hint if i forgot anything
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define WHITE " \t\n" // white space, tab and newline for tokenizing arguments
#define MAXARGC 50 // max number of arguments in buf
void handlecommand(int argc, char *argv[])
{
// do some handle code, in this example, just print the arguments
for(int i = 0; i < argc; i++)
printf("argv[%d]='%s'\n", i, argv[i]);
}
void parsecommand(char * cmdstr)
{
char *cmdstrdup = strdup(cmdstr);
if(cmdstrdup == NULL)
//insuficient memory, do some errorhandling.
return;
char *saveptr;
char *ptr;
char *argv[MAXARGC];
int argc;
if((ptr = strtok_r(cmdstrdup, WHITE, &saveptr)) == NULL)
{
printf("%s\n", "no args given");
return;
}
argv[argc = 0] = cmdstrdup;
while(ptr != NULL) {
ptr = strtok_r(NULL, WHITE, &saveptr);
if(++argc >= MAXARGC-1) // -1 for room for NULL at the end
break;
argv[argc] = ptr;
}
// handle command before free
handlecommand(argc, argv);
// free cmdstrdup, cuz strdup does malloc inside
free(cmdstrdup);
}
int main(int argc, char const *argv[])
{
parsecommand("command arg1 arg2 arg3\targ4\narg5 arg6 arg7");
return 0;
}
Result
argv[0]='command'
argv[1]='arg1'
argv[2]='arg2'
argv[3]='arg3'
argv[4]='arg4'
argv[5]='arg5'
argv[6]='arg6'
argv[7]='arg7'