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

Function fillbuf

src/sort.c:1871–1983  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1869 Return true if some input was read. */
1870
1871static bool
1872fillbuf (struct buffer *buf, FILE *fp, char const *file)
1873{
1874 struct keyfield const *key = keylist;
1875 char eol = eolchar;
1876 size_t line_bytes = buf->line_bytes;
1877 size_t mergesize = merge_buffer_size - MIN_MERGE_BUFFER_SIZE;
1878
1879 if (buf->eof)
1880 return false;
1881
1882 if (buf->used != buf->left)
1883 {
1884 memmove (buf->buf, buf->buf + buf->used - buf->left, buf->left);
1885 buf->used = buf->left;
1886 buf->nlines = 0;
1887 }
1888
1889 while (true)
1890 {
1891 char *ptr = buf->buf + buf->used;
1892 struct line *linelim = buffer_linelim (buf);
1893 struct line *line = linelim - buf->nlines;
1894 size_t avail = (char *) linelim - buf->nlines * line_bytes - ptr;
1895 char *line_start = buf->nlines ? line->text + line->length : buf->buf;
1896
1897 while (line_bytes + 1 < avail)
1898 {
1899 /* Read as many bytes as possible, but do not read so many
1900 bytes that there might not be enough room for the
1901 corresponding line array. The worst case is when the
1902 rest of the input file consists entirely of newlines,
1903 except that the last byte is not a newline. */
1904 size_t readsize = (avail - 1) / (line_bytes + 1);
1905 size_t bytes_read = fread (ptr, 1, readsize, fp);
1906 char *ptrlim = ptr + bytes_read;
1907 char *p;
1908 avail -= bytes_read;
1909
1910 if (bytes_read != readsize)
1911 {
1912 if (ferror (fp))
1913 sort_die (_("read failed"), file);
1914 if (feof (fp))
1915 {
1916 buf->eof = true;
1917 if (buf->buf == ptrlim)
1918 return false;
1919 if (line_start != ptrlim && ptrlim[-1] != eol)
1920 *ptrlim++ = eol;
1921 }
1922 }
1923
1924 /* Find and record each line in the just-read input. */
1925 while ((p = memchr (ptr, eol, ptrlim - ptr)))
1926 {
1927 /* Delimit the line with NUL. This eliminates the need to
1928 temporarily replace the last byte with NUL when calling

Callers 3

checkFunction · 0.85
mergefpsFunction · 0.85
sortFunction · 0.85

Calls 5

buffer_linelimFunction · 0.85
sort_dieFunction · 0.85
limfieldFunction · 0.85
begfieldFunction · 0.85
to_ucharFunction · 0.85

Tested by

no test coverage detected