int read_sort_file(char *filename, int source, char *source_path[]) { FILE *fd; char sort_filename[16385]; int priority; if((fd = fopen(filename, "r")) == NULL) { perror("Could not open sort_list file..."); return FALSE; } while(fscanf(fd, "%s %d", sort_filename, &priority) != EOF) if(priority >= -32768 && priority <= 32767) add_sort_list(sort_filename, priority, source, source_path); else ERROR("Sort file %s, priority %d outside range of -32767:32768 - skipping...\n", sort_filename, priority); fclose(fd); return TRUE; }
int read_sort_file(char *filename, int source, char *source_path[]) { FILE *fd; char line_buffer[MAX_LINE + 1]; /* overflow safe */ char sort_filename[MAX_LINE + 1]; /* overflow safe */ char *line, *name; int n, priority, res; if((fd = fopen(filename, "r")) == NULL) { ERROR("Failed to open sort file \"%s\" because %s\n", filename, strerror(errno)); return FALSE; } while(fgets(line = line_buffer, MAX_LINE + 1, fd) != NULL) { int len = strlen(line); if(len == MAX_LINE && line[len - 1] != '\n') { /* line too large */ ERROR("Line too long when reading " "sort file \"%s\", larger than %d " "bytes\n", filename, MAX_LINE); goto failed; } /* * Remove '\n' terminator if it exists (the last line * in the file may not be '\n' terminated) */ if(len && line[len - 1] == '\n') line[len - 1] = '\0'; /* Skip any leading whitespace */ while(isspace(*line)) line ++; /* if comment line, skip */ if(*line == '#') continue; /* * Scan for filename, don't use sscanf() and "%s" because * that can't handle filenames with spaces */ for(name = sort_filename; !isspace(*line) && *line != '\0';) { if(*line == '\\') { line ++; if (*line == '\0') break; } *name ++ = *line ++; } *name = '\0'; /* * if filename empty, then line was empty of anything but * whitespace or a backslash character. Skip empy lines */ if(sort_filename[0] == '\0') continue; /* * Scan the rest of the line, we expect a decimal number * which is the filename priority */ errno = 0; res = sscanf(line, "%d%n", &priority, &n); if((res < 1 || errno) && errno != ERANGE) { if(errno == 0) /* No error, assume EOL or match failure */ ERROR("Sort file \"%s\", can't find priority " "in entry \"%s\", EOL or match " "failure\n", filename, line_buffer); else /* Some other failure not ERANGE */ ERROR("Sscanf failed reading sort file \"%s\" " "because %s\n", filename, strerror(errno)); goto failed; } else if((errno == ERANGE) || (priority < -32768 || priority > 32767)) { ERROR("Sort file \"%s\", entry \"%s\" has priority " "outside range of -32767:32768.\n", filename, line_buffer); goto failed; } /* Skip any trailing whitespace */ line += n; while(isspace(*line)) line ++; if(*line != '\0') { ERROR("Sort file \"%s\", trailing characters after " "priority in entry \"%s\"\n", filename, line_buffer); goto failed; } res = add_sort_list(sort_filename, priority, source, source_path); if(res == FALSE) goto failed; } if(ferror(fd)) { ERROR("Reading sort file \"%s\" failed because %s\n", filename, strerror(errno)); goto failed; } fclose(fd); return TRUE; failed: fclose(fd); return FALSE; }