i edited the code to the following and its working.
Code:
/* concatenate command and get command count */
for(i = 0; i < cmdWords; i++) {
strcat(cmdStr, words[i]);
strcat(cmdStr, " ");
if(strcmp(words[i], "|") == 0 ) {
cmdCount ++;
}
}
/* Remove => */
temp = strtok(cmdStr, "|");
i = 0;
while(temp) {
cmdList[i] = (char*)malloc(1000); /* allocate memory */
cmdListDup[i] = (char*)malloc(1000); /* allocate memory */
strcpy(cmdList[i], temp);
strcpy(cmdListDup[i], temp);
temp = strtok(NULL, "|");
i++;
}
printf("cmd b4 forloop:%s\n", cmdList[0]);
/* get command for each commands */
for(i = 0; i <= cmdCount; i++) {
temp = strtok(cmdListDup[i], " ");
cmd[i] = temp;
printf("cmd:%s\n", cmd[i]);
}
printf("cmd after forloop:%s\n", cmdList[0]);