liblinux++

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

cat.cpp (501B)


      1 #include "linux.hpp"
      2 
      3 static void cat(file fd)
      4 {
      5 	static char buf[0x1000];
      6 
      7 	for (;;)
      8 	{
      9 		auto res = read(fd, buf, sizeof(buf));
     10 		if (!res)
     11 			exit(2);
     12 		auto n = *res;
     13 		if (n == 0)
     14 			return;
     15 		res = write(stdout, buf, n);
     16 		if (!res or *res != n)
     17 			exit(3);
     18 	}
     19 }
     20 
     21 int main(int argc, char* argv[])
     22 {
     23 	if (argc <= 1)
     24 		cat(stdin);
     25 	else for (int i = 1; i < argc; ++i)
     26 	{
     27 		auto res = openat(AT_FDCWD, argv[i], 0, O_RDONLY);
     28 		if (!res)
     29 			return 1;
     30 		auto fd = *res;
     31 		cat(fd);
     32 		*close(fd);
     33 	}
     34 }