liblinux++

A hosted C++ runtime without any libc.
Log | Files | Refs

paste.c (999B)


      1 #include "linux.h"
      2 
      3 struct input
      4 {
      5 	int 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 		int n = read(in->fd, in->buf, 0x1000-8);
     19 		if (n <= 0)
     20 			return 0;
     21 		in->end = n;
     22 	}
     23 	return in->buf[in->start++];
     24 }
     25 
     26 int main(int argc, char* argv[])
     27 {
     28 	for (int i = 1; i < argc; ++i)
     29 	{
     30 		int fd = openat(AT_FDCWD, argv[i], 0, 0);
     31 		if (get_errno(fd))
     32 			return 1;
     33 		intptr_t m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
     34 		if (get_errno(m))
     35 			return 2;
     36 		((struct input*)m)->fd = fd;
     37 	}
     38 
     39 	for (;;)
     40 	{
     41 		for (int i = 0; i < n_inputs; ++i)
     42 		{
     43 			struct input* in = mappable + i;
     44 
     45 			for (;;)
     46 			{
     47 				char c = getc(in);
     48 				if (!c)
     49 					return 0;
     50 				if (c == '\n')
     51 					break;
     52 				write(1, &c, 1);
     53 			}
     54 			write(1, i + 1 == n_inputs ? "\n" : "\t", 1);
     55 		}
     56 	}
     57 }