MCPcopy Create free account
hub / github.com/coreutils/coreutils / check_file

Function check_file

src/uniq.c:337–475  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

335 If either is "-", use the standard I/O stream for it instead. */
336
337static void
338check_file (char const *infile, char const *outfile, char delimiter)
339{
340 struct linebuffer lb1, lb2;
341 struct linebuffer *thisline, *prevline;
342
343 if (! (streq (infile, "-") || freopen (infile, "r", stdin)))
344 error (EXIT_FAILURE, errno, "%s", quotef (infile));
345 if (! (streq (outfile, "-") || freopen (outfile, "w", stdout)))
346 error (EXIT_FAILURE, errno, "%s", quotef (outfile));
347
348 fadvise (stdin, FADVISE_SEQUENTIAL);
349
350 thisline = &lb1;
351 prevline = &lb2;
352
353 initbuffer (thisline);
354 initbuffer (prevline);
355
356 /* The duplication in the following 'if' and 'else' blocks is an
357 optimization to distinguish between when we can print input
358 lines immediately (1. & 2.) or not.
359
360 1. --group => all input lines are printed.
361 checking for unique/duplicated lines is used only for printing
362 group separators.
363
364 2. The default case in which none of these options has been specified:
365 --count, --repeated, --all-repeated, --unique
366 In the default case, this optimization lets uniq output each different
367 line right away, without waiting to see if the next one is different.
368
369 3. All other cases.
370 */
371 if (output_unique && output_first_repeated && !count_occurrences)
372 {
373 char *prevfield = NULL;
374 idx_t prevlen;
375 bool first_group_printed = false;
376
377 while (!feof (stdin)
378 && readlinebuffer_delim (thisline, stdin, delimiter))
379 {
380 idx_t thislen;
381 char *thisfield = find_field (thisline, &thislen);
382 bool new_group = (!prevfield
383 || different (thisfield, prevfield,
384 thislen, prevlen));
385
386 if (new_group && grouping != GM_NONE
387 && (grouping == GM_PREPEND || grouping == GM_BOTH
388 || (first_group_printed && (grouping == GM_APPEND
389 || grouping == GM_SEPARATE))))
390 putchar (delimiter);
391
392 if (new_group || grouping != GM_NONE)
393 {
394 if (fwrite (thisline->buffer, sizeof (char), thisline->length,

Callers 1

mainFunction · 0.85

Calls 6

fadviseFunction · 0.85
find_fieldFunction · 0.85
differentFunction · 0.85
write_errorFunction · 0.85
swap_linesFunction · 0.85
writelineFunction · 0.70

Tested by

no test coverage detected