| 3067 | they are not in order. */ |
| 3068 | |
| 3069 | static bool |
| 3070 | check (char const *file_name, char checkonly) |
| 3071 | { |
| 3072 | FILE *fp = xfopen (file_name, "r"); |
| 3073 | struct buffer buf; /* Input buffer. */ |
| 3074 | struct line temp; /* Copy of previous line. */ |
| 3075 | size_t alloc = 0; |
| 3076 | uintmax_t line_number = 0; |
| 3077 | struct keyfield const *key = keylist; |
| 3078 | bool nonunique = ! unique; |
| 3079 | bool ordered = true; |
| 3080 | |
| 3081 | initbuf (&buf, sizeof (struct line), |
| 3082 | MAX (merge_buffer_size, sort_size)); |
| 3083 | temp.text = NULL; |
| 3084 | |
| 3085 | while (fillbuf (&buf, fp, file_name)) |
| 3086 | { |
| 3087 | struct line const *line = buffer_linelim (&buf); |
| 3088 | struct line const *linebase = line - buf.nlines; |
| 3089 | |
| 3090 | /* Make sure the line saved from the old buffer contents is |
| 3091 | less than or equal to the first line of the new buffer. */ |
| 3092 | if (alloc && nonunique <= compare (&temp, line - 1)) |
| 3093 | { |
| 3094 | found_disorder: |
| 3095 | { |
| 3096 | if (checkonly == 'c') |
| 3097 | { |
| 3098 | struct line const *disorder_line = line - 1; |
| 3099 | uintmax_t disorder_line_number = |
| 3100 | buffer_linelim (&buf) - disorder_line + line_number; |
| 3101 | fprintf (stderr, _("%s: %s:%ju: disorder: "), |
| 3102 | program_name, file_name, disorder_line_number); |
| 3103 | write_line (disorder_line, stderr, _("standard error")); |
| 3104 | } |
| 3105 | |
| 3106 | ordered = false; |
| 3107 | break; |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | /* Compare each line in the buffer with its successor. */ |
| 3112 | while (linebase < --line) |
| 3113 | if (nonunique <= compare (line, line - 1)) |
| 3114 | goto found_disorder; |
| 3115 | |
| 3116 | line_number += buf.nlines; |
| 3117 | |
| 3118 | /* Save the last line of the buffer. */ |
| 3119 | if (alloc < line->length) |
| 3120 | { |
| 3121 | do |
| 3122 | { |
| 3123 | alloc *= 2; |
| 3124 | if (! alloc) |
| 3125 | { |
| 3126 | alloc = line->length; |
no test coverage detected