int modified_since(time_t * mtime, char *if_modified_since) { struct tm *file_gmt; char *ims_info; char monthname[10 + 1]; int day, month, year, hour, minute, second; int comp; ims_info = if_modified_since; while (*ims_info != ' ' && *ims_info != '\0') ++ims_info; if (*ims_info != ' ') return -1; /* the pre-space in the third scanf skips whitespace for the string */ if (sscanf(ims_info, "%d %3s %d %d:%d:%d GMT", /* RFC 1123 */ &day, monthname, &year, &hour, &minute, &second) == 6); else if (sscanf(ims_info, "%d-%3s-%d %d:%d:%d GMT", /* RFC 1036 */ &day, monthname, &year, &hour, &minute, &second) == 6) year += 1900; else if (sscanf(ims_info, " %3s %d %d:%d:%d %d", /* asctime() format */ monthname, &day, &hour, &minute, &second, &year) == 6); /* allow this non-standard date format: 31 September 2000 23:59:59 GMT */ /* NOTE: Use if_modified_since here, because the date *starts* * with the day, versus a throwaway item */ else if (sscanf(if_modified_since, "%d %10s %d %d:%d:%d GMT", &day, monthname, &year, &hour, &minute, &second) == 6); else { log_error_time(); fprintf(stderr, "Error in %s, line %d: Unable to sscanf \"%s\"\n", __FILE__, __LINE__, ims_info); return -1; /* error */ } file_gmt = gmtime(mtime); month = month2int(monthname); /* Go through from years to seconds -- if they are ever unequal, we know which one is newer and can return */ if ((comp = 1900 + file_gmt->tm_year - year)) return (comp > 0); if ((comp = file_gmt->tm_mon - month)) return (comp > 0); if ((comp = file_gmt->tm_mday - day)) return (comp > 0); if ((comp = file_gmt->tm_hour - hour)) return (comp > 0); if ((comp = file_gmt->tm_min - minute)) return (comp > 0); if ((comp = file_gmt->tm_sec - second)) return (comp > 0); return 0; /* this person must really be into the latest/greatest */ }
int modified_since(time_t * mtime, char *if_modified_since) { struct tm *file_gmt; char *ims_info; char monthname[10 + 1]; int day, month, year, hour, minute, second; int comp; ims_info = if_modified_since; while (*ims_info != ' ' && *ims_info != '\0') ++ims_info; if (*ims_info != ' ') return -1; if (sscanf(ims_info, "%d %3s %d %d:%d:%d GMT", &day, monthname, &year, &hour, &minute, &second) == 6); else if (sscanf(ims_info, "%d-%3s-%d %d:%d:%d GMT", &day, monthname, &year, &hour, &minute, &second) == 6) year += 1900; else if (sscanf(ims_info, " %3s %d %d:%d:%d %d", monthname, &day, &hour, &minute, &second, &year) == 6); /* NOTE: Use if_modified_since here, because the date *starts* * with the day, versus a throwaway item */ else if (sscanf(if_modified_since, "%d %10s %d %d:%d:%d GMT", &day, monthname, &year, &hour, &minute, &second) == 6); else { log_error_time(); fprintf(stderr, "Error in %s, line %d: Unable to sscanf \"%s\"\n", __FILE__, __LINE__, ims_info); return -1; } file_gmt = gmtime(mtime); month = month2int(monthname); /* Go through from years to seconds -- if they are ever unequal, we know which one is newer and can return */ if ((comp = 1900 + file_gmt->tm_year - year)) return (comp > 0); if ((comp = file_gmt->tm_mon - month)) return (comp > 0); if ((comp = file_gmt->tm_mday - day)) return (comp > 0); if ((comp = file_gmt->tm_hour - hour)) return (comp > 0); if ((comp = file_gmt->tm_min - minute)) return (comp > 0); if ((comp = file_gmt->tm_sec - second)) return (comp > 0); return 0; }