I wrote this, works nearly as expected but on the last element extracted from input string it attach various unexpected character, please any suggestion??
In particular look at divCmd function.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 1024
#define STDIN 0
#define STDOUT 1
char ** divCmd(char *);
int main(int argc, char *argv[])
{
char cmd[BUFLEN];
char shellTxt[9] = "myShell$ ";
int nByte;
char **prova;
write(STDOUT,shellTxt,9);
nByte = read(STDIN,cmd,BUFLEN);
//write(STDOUT,buf,nByte);
prova = divCmd(cmd);
puts(prova[2]);
}
char ** divCmd(char *cmd)
{
char *cmdArr[50];
char **arr;
int i;
cmdArr[0] = strtok(cmd," ");
printf("cmdArr[0] = %s\n", cmdArr[0]);
for(i=1; i<50; i++)
{
cmdArr = strtok(NULL," ");
if(cmdArr != NULL)
{
printf("cmdArr[%d] = %s\n", i, cmdArr);
} else break;
}
return cmdArr;
}
In particular look at divCmd function.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 1024
#define STDIN 0
#define STDOUT 1
char ** divCmd(char *);
int main(int argc, char *argv[])
{
char cmd[BUFLEN];
char shellTxt[9] = "myShell$ ";
int nByte;
char **prova;
write(STDOUT,shellTxt,9);
nByte = read(STDIN,cmd,BUFLEN);
//write(STDOUT,buf,nByte);
prova = divCmd(cmd);
puts(prova[2]);
}
char ** divCmd(char *cmd)
{
char *cmdArr[50];
char **arr;
int i;
cmdArr[0] = strtok(cmd," ");
printf("cmdArr[0] = %s\n", cmdArr[0]);
for(i=1; i<50; i++)
{
cmdArr = strtok(NULL," ");
if(cmdArr != NULL)
{
printf("cmdArr[%d] = %s\n", i, cmdArr);
} else break;
}
return cmdArr;
}