title: Segmentation fault (core dumped)
body: <p>I'm writing a program in c that basically copies files, but I'm getting this error: Segmentation fault (core dumped). From what I'm reading I think it's because I'm trying to access memory that hasn't been allocated yet. I'm a newbie when it comes to c and I suck at pointers, so I was wondering if you guys could tell me which pointer is causing this and how to fix it if possible. Btw, this program is supposed to be a daemon, but I haven't put anything inside the infinite while loop at the bottom.
  Here is my code: </p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;syslog.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dirent.h&gt;
  
  int main(int c, char *argv[]) {
  char *source, *destination;
  char *list1[30], *list2[30], *listDif[30];
  unsigned char buffer[4096];
  int i=0, x=0, sizeSource=0, sizeDest=0, sizeDif=0;
  int outft, inft,fileread;
  int sleeper;
  struct dirent *ent, *ent1;
  
  //Check number of arguments
  if(c&lt;3) 
  {
      printf("Daemon wrongly called\n");
      printf("How to use: &lt;daemon name&gt; &lt;orginDirectory&gt; &lt;destinationDirectory&gt; \n");
      printf("or : &lt;daemon name&gt; &lt;orginDirectory&gt; &lt;destinationDirectory&gt; &lt;sleeperTime(seconds)&gt;");
      return 0;
  }
  
  //Checks if sleeper time is given or will be the default 5minutes
  /*if(c=4)
  {
      char *p;
      errno = 0;
      long conv = strtol(argv[3], &amp;p, 10);
      if(errno != 0 || *p != '\0') 
      {
          printf("Number given for sleeper incorrect, it has to be an integer value.\n");
          return(0);
      } else 
      {
      sleeper = conv;
      }
  } else
  {
      sleeper = 300;
  }*/
  
  //Get path of directories from arguments
  source = argv[1];
  destination = argv[2];
  
  //Check if directories exist
  DIR* dirSource = opendir(source);
  if (!dirSource)
  {
      printf("Source directory incorrect\n");
      return 0;
  }
  DIR* dirDest = opendir(destination);
  if (!dirDest)
  {
      printf("Destination directory incorrect\n");
      return 0;
  }
  
  /* save all the files and directories within directory */
  while ((ent = readdir (dirSource)) != NULL) {
      list1[sizeSource] = strdup(ent-&gt;d_name);    
      sizeSource++;
      if(sizeSource&gt;=30){break;}
  }
  closedir(dirSource);
  while((ent1 = readdir (dirDest)) != NULL) {
      list2[sizeDest] = strdup(ent1-&gt;d_name);
      sizeDest++;
      if(sizeDest&gt;=30){break;}
  }
  closedir(dirDest);
  /* Verify the diferences between the directories and save them */
  int z;
  int dif = 0; //0 - False | 1 - True
  printf("Diferenças:\n");
  for(i=0;i&lt;sizeSource;i++){
      dif = 0;
      for(z=0;z&lt;sizeDest;z++){
          if(strcmp(list1[i],list2[z])==0){ //If there is no match, it saves the name of the file to listDif[]
              dif = 1; 
              break;
          }   
      }
      if(dif==0) {
          printf("%s\n",list1[i]);
          listDif[sizeDif] = list1[i];
          sizeDif++;
      }
  }
  
  /* This code will copy the files */
  z=0;
  while(z!=sizeDif){
      // output file opened or created
      char *pathSource, *pathDest;        
  
      strcpy(pathSource, source);
      strcat(pathSource, "/");
      strcat(pathSource, listDif[z]);
  
      strcpy(pathDest, destination);
      strcat(pathDest, "/");
      strcat(pathDest, listDif[z]);
  
      // output file opened or created
      if((outft = open(pathDest, O_CREAT | O_APPEND | O_RDWR))==-1){
          perror("open");
      }
      // lets open the input file
      inft = open(pathSource, O_RDONLY);
      if(inft &gt;0){ // there are things to read from the input
          fileread = read(inft, buffer, sizeof(buffer));
          printf("%s\n", buffer);
          write(outft, buffer, fileread);
          close(inft);
      }
      close(outft);
  }
  
  
  
  /* Our process ID and Session ID */
  pid_t pid, sid;
  
  /* Fork off the parent process */
  pid = fork();
  if (pid &lt; 0) {
          exit(EXIT_FAILURE);
  }
  /* If we got a good PID, then
     we can exit the parent process. */
  if (pid &gt; 0) {
          exit(EXIT_SUCCESS);
  }
  
  /* Change the file mode mask */
  umask(0);
  
  /* Open any logs here */        
  
  /* Create a new SID for the child process */
  sid = setsid();
  if (sid &lt; 0) {
          /* Log the failure */
          exit(EXIT_FAILURE);
  }
  
  
  
  /* Change the current working directory */
  if ((chdir("/")) &lt; 0) {
          /* Log the failure */
          exit(EXIT_FAILURE);
  }
  
  /* Close out the standard file descriptors */
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  
  /* Daemon-specific initialization goes here */
  
  /* The Big Loop */
  while (1) {
  
     //sleep(5); /* wait 5 seconds */
  }
  exit(EXIT_SUCCESS); 
  </code></pre>
  
  <p>}
  The result of <code>ls</code> is:</p>
  
  <pre><code>ubuntu@ubuntu:~/Desktop$ ls
  Concatenar_Strings.c   core  D2      daemon.c   examples.desktop
  Concatenar_Strings.c~  D1    daemon  daemon.c~  ubiquity.desktop
  </code></pre>
  
  <p>D1 and D2 are folders, and in D1 are three text documents that I want to copy into D2.
  One other question, is this a delayed error or an immediate one? Because I doubt this message would appear on a code line that with two integers.
  Thanks in advance guys. </p>