cat(1) now writes to /dev/tty.

Someone really ought to implement stdout.
This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-23 00:18:04 +01:00
parent 46c0cc6a12
commit 7a9afd66fe
1 changed files with 23 additions and 2 deletions

View File

@ -5,10 +5,27 @@
#include <errno.h>
#include <libmaxsi/sortix-keyboard.h>
bool writeall(int fd, const void* buffer, size_t len)
{
const char* buf = (const char*) buffer;
while ( len )
{
ssize_t byteswritten = write(fd, buf, len);
if ( byteswritten < 0 ) { return false; }
buf += byteswritten;
len -= byteswritten;
}
return true;
}
int cat(int argc, char* argv[])
{
int result = 0;
int outfd = open("/dev/tty", O_WRONLY | O_APPEND);
if ( outfd < 0 ) { printf("%s: %s: %s\n", argv[0], "/dev/tty", strerror(errno)); return 1; }
for ( int i = 1; i < argc; i++ )
{
int fd = open(argv[i], O_RDONLY);
@ -31,8 +48,12 @@ int cat(int argc, char* argv[])
result = 1;
break;
}
buffer[bytesread] = 0;
printf("%s", buffer);
if ( !writeall(outfd, buffer, bytesread) )
{
printf("%s: /dev/tty: %s\n", argv[0], strerror(errno));
result = 1;
break;
}
} while ( true );
close(fd);