Improve cat's unbuffered mode.

This commit is contained in:
Mid Favila 2022-10-02 13:27:00 -04:00
parent 254e05998e
commit 966113d372
1 changed files with 17 additions and 11 deletions

View File

@ -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);