Improve cat's unbuffered mode.

master
Mid Favila 2 months ago
parent 254e05998e
commit 966113d372
  1. 26
      src/cat.c

@ -79,26 +79,32 @@ 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);
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);
continue;
}
buf = concat(buf, newbuf, bufsize, newbufsize);
bufsize += newbufsize;
free(newbuf);
i++, newbufsize = 0;
}
while((i < argc) || (((argc - optind) == 0) && !c));
if(!pars[U])
{

Loading…
Cancel
Save