0

I'm writing a program to check if a file in the current directory has been modified after a certain time in Unix. I use:

  struct tm* clock;                     // create a time structure
  struct stat attrib;                   // create a file attribute structure
  DIR * directory = NULL;               // create directory file
  directory = opendir(cwd);
  if(directory == NULL)
    return -1;
  struct dirent * ent;
  time_t tim=time(NULL);                // acquire time information
  struct tm * now=localtime(&tim);

Later on I have:

while((ent = readdir (directory)) != NULL)
{                                         

      stat(ent->d_name, &attrib);           // get the attributes of file
      clock = gmtime(&(attrib.st_mtime));   // Get the last modified time
      {...//printf's to print all tm_ measurements and iyr,imonth,etc}
      if(iyr<=clock->tm_year+1900 && imonth<=clock->tm_mon+1 && iday <= clock->tm_mday &&   ihour<=clock->tm_hour && imin <= clock ->tm_min)
      { ...}

Excusing the absurd amount of conditions, why would clock->tm_mon cause no seg fault, while clock->tm_mon+1 leads to a seg fault crash? With the code that I have above, the seg fault happened after one print command in the loop. If I create an integer b, set it equal to clock->tm_mon and add one, using it in place of clock->tm_mon+1, I still get a seg fault but several prints later.

The iyr,imonth, etc are integers representing recent years,months,etc properly and I've confirmed that the seg fault happens in the loop no matter what I try to do to account for the +1 in the month. I've also confirmed that nothing makes it past the if condition and nothing happens after that if condition in the remainder of the loop.

countofmontecristo
  • 381
  • 3
  • 5
  • 15

0 Answers0