Thread: Segmentation fault

  1. #1
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187

    Segmentation fault

    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!

  2. #2

  3. #3
    C Beginner
    Join Date
    Dec 2011
    Location
    Portugal
    Posts
    187
    Thanks rags.
    Thing is, I know exactly when the error is happening.
    After doing :
    Code:
     cmds = getArgs(comando);
    I can successfully do printf("%s",cmds[0]) and the printf will work perfectly.
    Despite that, it might sound stupid but after doing :
    Code:
    printf("\n\n*** Comando recebido pelo server -> %s ***\n\n",comando);
    My printf's won't work anymore

    Any help ?
    Thanks!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Spend more than 15 minutes glossing over the links provided, and actually use the debugger!

    It will point you at a line of code, and allow you to examine variables - so you can figure out for yourself why it broke.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By williamsonj in forum C Programming
    Replies: 36
    Last Post: 03-26-2010, 02:01 AM
  2. Segmentation fault
    By sirsmilealot in forum C Programming
    Replies: 12
    Last Post: 02-10-2010, 01:26 PM
  3. Segmentation fault
    By Calavera in forum C Programming
    Replies: 4
    Last Post: 11-18-2005, 08:19 AM
  4. segmentation fault : ?????
    By gemini_shooter in forum C Programming
    Replies: 5
    Last Post: 06-06-2005, 02:18 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM