liblinux++

A hosted C++ runtime without any libc.
git clone git://henryandlizzy.uk/liblinux++
Log | Files | Refs

paste.cpp (1034B)


      1 #include "linux.hpp"
      2 
      3 struct input
      4 {
      5 	file fd;
      6 	unsigned short start, end;
      7 	char buf[0x1000 - 8];
      8 };
      9 
     10 extern struct input mappable[];
     11 static int n_inputs;
     12 static char getc(struct input* in)
     13 {
     14 	if (in->start == in->end)
     15 	{
     16 		in->start = 0;
     17 		in->end = 0;
     18 		auto res = read(in->fd, in->buf, 0x1000-8);
     19 		if (!res)
     20 			exit(1);
     21 		auto n = *res;
     22 		if (n <= 0)
     23 			return 0;
     24 		in->end = n;
     25 	}
     26 	return in->buf[in->start++];
     27 }
     28 
     29 int main(int argc, char* argv[])
     30 {
     31 	for (int i = 1; i < argc; ++i)
     32 	{
     33 		auto fd = openat(AT_FDCWD, argv[i], 0, 0);
     34 		if (!fd)
     35 			return 1;
     36 		auto m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, {}, 0);
     37 		if (!m)
     38 			return 2;
     39 		static_cast<input*>(*m)->fd = *fd;
     40 	}
     41 
     42 	for (;;)
     43 	{
     44 		for (int i = 0; i < n_inputs; ++i)
     45 		{
     46 			input* in = mappable + i;
     47 
     48 			for (;;)
     49 			{
     50 				char c = getc(in);
     51 				if (!c)
     52 					return 0;
     53 				if (c == '\n')
     54 					break;
     55 				*write(stdout, &c, 1);
     56 			}
     57 			*write(stdout, i + 1 == n_inputs ? "\n" : "\t", 1);
     58 		}
     59 	}
     60 }