Made cat(1) read from stdin if it isn't a tty.
This commit is contained in:
parent
98d1ccfa7d
commit
7b9e15baf2
|
@ -7,6 +7,28 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <error.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 cat(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
@ -20,27 +42,8 @@ int cat(int argc, char* argv[])
|
||||||
result = 1;
|
result = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
do
|
result |= docat(argv[i], fd);
|
||||||
{
|
|
||||||
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 );
|
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,6 +57,8 @@ int main(int argc, char* argv[])
|
||||||
return cat(argc, argv);
|
return cat(argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !isatty(0) ) { return docat("<stdin>", 0); }
|
||||||
|
|
||||||
bool lastwasesc = false;
|
bool lastwasesc = false;
|
||||||
|
|
||||||
// TODO: This is just compatibility with how cat worked in early versions of
|
// TODO: This is just compatibility with how cat worked in early versions of
|
||||||
|
|
Loading…
Reference in New Issue