From 96d94d959715a2f4aa20547a5b0d23b8470d64e9 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 18 Nov 2011 19:29:52 +0100 Subject: [PATCH] cat(1) can now cat files. --- utils/cat.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/utils/cat.cpp b/utils/cat.cpp index c50d8ae2..b055afc1 100644 --- a/utils/cat.cpp +++ b/utils/cat.cpp @@ -1,8 +1,41 @@ #include +#include +#include #include +int cat(int argc, char* argv[]) +{ + int result = 0; + + for ( int i = 1; i < argc; i++ ) + { + int fd = open(argv[i], O_RDONLY); + if ( fd < 0 ) { printf("%s: unable to open: %s\n", argv[0], argv[i]); result = 1; continue; } + + do + { + const size_t BUFFER_SIZE = 255; + char buffer[BUFFER_SIZE+1]; + ssize_t bytesread = read(fd, buffer, BUFFER_SIZE); + if ( bytesread == 0 ) { break; } + if ( bytesread < 0 ) { printf("%s: read failed: %s\n", argv[0], argv[i]); result = 1; break; } + buffer[bytesread] = 0; + printf("%s", buffer); + } while ( true ); + + close(fd); + } + + return result; +} + int main(int argc, char* argv[]) { + if ( 1 < argc ) + { + return cat(argc, argv); + } + bool lastwasesc = false; while (true)