Improve cat's unbuffered mode.
This commit is contained in:
parent
254e05998e
commit
966113d372
28
src/cat.c
28
src/cat.c
@ -79,26 +79,32 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
|
|
||||||
/* i < argc is obvious; steps through all provided args. */
|
/* i < argc is obvious; steps through all provided args. */
|
||||||
/* !argc-optind && !newbuf is a little less so - it allows this loop to run once if the user provides no paths */
|
/* !argc-optind && !c is a little less so - it allows this loop to run once if the user provides no paths */
|
||||||
for(i = optind; (i < argc) || (((argc - optind) == 0) && !newbuf); i++, newbufsize = 0)
|
do
|
||||||
{
|
{
|
||||||
if( !(fd = file_open(argv[i], "r")))
|
if( !(fd = file_open(argv[i], "r")))
|
||||||
{
|
{
|
||||||
throw(FOPEN_FAIL, mastrcat(argv[0], mastrcat(": could not open file ", argv[i])));
|
throw(FOPEN_FAIL, mastrcat(argv[0], mastrcat(": could not open file ", argv[i])));
|
||||||
}
|
}
|
||||||
|
|
||||||
newbuf = file_read(fd, &newbufsize);
|
|
||||||
if(pars[U])
|
if(pars[U])
|
||||||
{
|
{
|
||||||
fwrite(newbuf, sizeof(char), newbufsize, stdout);
|
for(; (c = fgetc(fd)) != EOF; c = 0)
|
||||||
free(newbuf);
|
{
|
||||||
continue;
|
fputc(c, stdout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
buf = concat(buf, newbuf, bufsize, newbufsize);
|
else
|
||||||
bufsize += newbufsize;
|
{
|
||||||
free(newbuf);
|
newbuf = file_read(fd, &newbufsize);
|
||||||
|
buf = concat(buf, newbuf, bufsize, newbufsize);
|
||||||
|
bufsize += newbufsize;
|
||||||
|
free(newbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
i++, newbufsize = 0;
|
||||||
}
|
}
|
||||||
|
while((i < argc) || (((argc - optind) == 0) && !c));
|
||||||
|
|
||||||
if(!pars[U])
|
if(!pars[U])
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user