Made cat(1) read from stdin if it isn't a tty.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-03-02 18:15:55 +01:00
parent 98d1ccfa7d
commit 7b9e15baf2
1 changed files with 26 additions and 21 deletions

View File

@ -7,6 +7,28 @@
#include <errno.h>
#include <error.h>
int docat(const char* inputname, int fd)
{
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 )
{
error(0, errno, "read: %s", inputname);
return 1;
}
if ( writeall(1, buffer, bytesread) )
{
error(0, errno, "write: %s", inputname);
return 1;
}
} while ( true );
return 0;
}
int cat(int argc, char* argv[])
{
int result = 0;
@ -20,27 +42,8 @@ int cat(int argc, char* argv[])
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 )
{
error(0, errno, "read: %s", argv[i]);
result = 1;
break;
}
if ( writeall(1, buffer, bytesread) )
{
error(0, errno, "write: %s", argv[i]);
result = 1;
break;
}
} while ( true );
result |= docat(argv[i], fd);
close(fd);
}
@ -54,6 +57,8 @@ int main(int argc, char* argv[])
return cat(argc, argv);
}
if ( !isatty(0) ) { return docat("<stdin>", 0); }
bool lastwasesc = false;
// TODO: This is just compatibility with how cat worked in early versions of