I’m trying to parse a string manually by spaces without using strtok() or anything from string.h. Does this look like the right approach? When I try this, I keep skipping right past the end of the string.
char cmd[1024];
int ret = read(STDIN, cmd, 1023);
cmd[ret-1] = '\0';
char * args[128];
int length = 0;
char * startptr = cmd;
char * endptr = cmd;
while(1){
if(*startptr == '\n' || *startptr == '\0'){
break;
}
if(*startptr == ' '){
startptr ++;
endptr ++;
continue;
}
// startptr is placed
if(*endptr != '\0' || *endptr != '\n' || *endptr != ' '){
endptr ++;
continue;
}
// both pointers placed
char * i = startptr;
for(i = startptr; i != endptr; i++){
args[length][i-startptr] = *i;
}
length ++;
startptr = endptr;
if(*endptr == '\0' || *endptr == '\n'){
break;
}
}
No is not right.Logical expressions must be ‘and’ not ‘or’.
checking for limitations would be good, some times will may interesting effect like ‘change ARG_MAX_LEN to 2’ you get the first character of every word.
Probably this code will work.
If you are want to get command line arguments you should look for character escaping as well.