tac.c (969B)
1 #include "linux.h" 2 3 extern char mappable[]; 4 5 static int rd(int const fd, char* const start, char const* const end) 6 { 7 char* pos = start; 8 while (pos != end) 9 { 10 int n = read(fd, pos, end - pos); 11 if (get_errno(n)) 12 exit(2); 13 if (n == 0) 14 break; 15 pos += n; 16 } 17 return pos - start; 18 } 19 20 static void wr(int const fd, char const* start, char const* const end) 21 { 22 while (start != end) 23 { 24 int n = write(fd, start, end - start); 25 if (get_errno(n)) 26 exit(3); 27 start += n; 28 } 29 } 30 31 int main() 32 { 33 char* buf = mappable; 34 size_t cap = 0; 35 size_t len = 0; 36 37 while (len == cap) 38 { 39 intptr_t m = mmap(buf + cap, cap ?: 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 40 if (get_errno(m)) 41 return 1; 42 cap += cap ?: 0x1000; 43 44 len += rd(0, buf + len, buf + cap); 45 } 46 47 reverse: 48 char const* end = buf + len; 49 char const* str = end; 50 51 while (--str > buf) 52 { 53 while (str > buf && str[-1] != '\n') 54 --str; 55 wr(1, str, end); 56 end = str; 57 } 58 }