mp-utils/old/cat2.c

73 lines
1.2 KiB
C

/* cat.c -- program to concatenate named files and/or stdin onto stdout */
/* version 2 */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "support.h"
/* parameters */
enum
{
U
};
int main(int argc, char **argv)
{
int i, c, bufsize, newbufsize;
char *buf, *newbuf;
char pars[1] = {0};
FILE *fd;
i = c = bufsize = newbufsize = 0;
buf = newbuf = 0;
fd = 0;
/* i < argc is obvious; steps through all provided args. */
/* !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])));
}
if(pars[U])
{
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);
}
exit(EXIT_SUCCESS);
}