Hello everyone,
my program was working "somehow" and now it ain't.
Basically, my server is receiving a command and he's printing it well but I need to separate it into an array of strings which has been implemented before by some friends of mine and therefore is working correctly but after it I can't print the arguments inside it.
Take a look at it please
My server code :
Code:
void filemon(char **cmd)
{ NFicheiros aux;
aux=cria();
char *ficheiro = NULL;
printf("%s",cmd[1]);
if(strncmp(cmd[0],"addfile",7)== 0)
{
printf("3");
printf("%s",cmd[0]);
ficheiro = strdup(cmd[2]);
printf("merda de cao \n");
aux = insereFicheiro(cmd[2],aux);
printf("\n\n *** Ficheiro %s adicionado com sucesso! ***\n\n",cmd[2]);
}
}
char** getArgs(const char* s)
{
static wordexp_t words;
static int initialized = 0;
if(s && !initialized)
{
wordexp(s, &words, 0);
initialized = 1;
}
else if(s)
{
wordexp(s, &words, WRDE_REUSE);
}
else
{
wordfree(&words);
initialized == 0;
return NULL;
}
return words.we_wordv;
}
int main(int argc, char *argv[])
{
int pid,status,apipe,rpipe=1,i=0;
int erro, erro1;
char **cmds;
char *comand;
char *buff;
const char *comando;
buff=malloc(100*sizeof(char));
printf("\n\n*** Bem vindo ao FileMonitorizer ***\n\n");
mkfifo("trabfifo",0646);
mkfifo("daemonfifo",0646);
int pidp=99999;
int modo=0;
int apaga=1;
comand = buff;
while(rpipe != -1 && strcmp(comand,"exit ")!=0)
{
printf("\n\n*** A ler do pipe! ***\n\n");
apipe = open("trabfifo",O_RDONLY);
rpipe = read(apipe,comand,BUF_TAM_MAX);
if(rpipe)
{
close(apipe);
comand[rpipe] = '\0';
comando = comand;
cmds = getArgs(comando);
printf("\n\n*** Comando recebido pelo server -> %s ***\n\n",comando);
filemon(cmds);
}
}
if(modo==1)
kill(pidp,SIGTERM);
printf("\n\n*** A sair do FileMonitorizer! ***\n\n");
system("rm trabfifo");
system("rm daemonfifo");
return 1;
}
The error occurs in the function filemon.
Thanks everyone!