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