+ for (i = 0; i < nfiles; i++) {
+ if (fds[i] == -1)
+ continue;
+ seen = 0;
+ if (show_headers || (!hide_headers && nfiles > 1))
+ printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
+ while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
+ if (from_top) {
+#ifndef BB_FEATURE_SIMPLE_TAIL
+ if (units == BYTES) {
+ if (count - 1 <= seen)
+ nwrite = nread;
+ else if (count - 1 <= seen + nread)
+ nwrite = nread + seen - (count - 1);
+ else
+ nwrite = 0;
+ seen += nread;
+ } else {
+#else
+ {
+#endif
+ if (count - 1 <= seen)
+ nwrite = nread;
+ else {
+ nwrite = 0;
+ for (s = memchr(buf, '\n', nread); s != NULL;
+ s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
+ if (count - 1 <= ++seen) {
+ nwrite = nread - (s + 1 - buf);
+ break;
+ }
+ }
+ }
+ }
+ if (full_write(STDOUT_FILENO, buf + nread - nwrite,
+ nwrite) < 0) {
+ perror_msg("write");
+ status = EXIT_FAILURE;
+ break;
+ }
+ } else {
+#ifndef BB_FEATURE_SIMPLE_TAIL
+ if (units == BYTES) {
+ if (nread < count) {
+ memmove(tailbuf, tailbuf + nread, count - nread);
+ memcpy(tailbuf + count - nread, buf, nread);
+ } else {
+ memcpy(tailbuf, buf + nread - count, count);
+ }
+ seen += nread;
+ } else {
+#else
+ {
+#endif
+ for (start = buf, end = memchr(buf, '\n', nread);
+ end != NULL; start = end+1,
+ end = memchr(start, '\n', nread - (start - buf))) {
+ if (newline && count <= seen)
+ tailbuf_trunc();
+ tailbuf_append(start, end - start + 1);
+ seen++;
+ newline = 1;
+ }
+ if (newline && count <= seen && nread - (start - buf) > 0)
+ tailbuf_trunc();
+ tailbuf_append(start, nread - (start - buf));
+ }
+ }
+ }