On Tue, 24 Feb 2015, Karel Zak wrote: > On Sun, Feb 22, 2015 at 02:41:42PM +0000, Sami Kerola wrote: > > When last lines happen to be greater than string buffer size for fgets() > > the number of printed lines resulted to too few. > > Maybe add a note about mmap() to commit message ?! Thanks Karel, Done. https://github.com/kerolasa/lelux-utiliteetit/commit/537f3b197f3c6af307997ebd103becbf44f529c1 > > - if (!(str = fopen(filename, "r"))) > > + if (!(fd = open(filename, O_RDONLY))) > > err(EXIT_FAILURE, _("cannot open %s"), filename); > > - > > - buf = xmalloc((lines ? lines : 1) * BUFSIZ); > > - p = buf; > > - while (fgets(p, BUFSIZ, str)) { > > - if (++tail >= lines) { > > - tail = 0; > > - head = 1; > > + data = mmap(0, old->st_size, PROT_READ, MAP_SHARED, fd, 0); > > It would be nice to compare this it with original implementation on > large files. I guess mmap will be more effective and faster. My motivation to do this change had less to do with speed than correcness. Earlier version did not print requested number or lines when tail read buffer was insufficient. That probably did not happen too often as 8K is a lot of text. The reason I chose mmap() is simple. It provides easy interface to read a file backwards. And a potential speed gain is a nice side effect. -- Sami Kerola http://www.iki.fi/kerolasa/ -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html