mp-utils/src/cat.c

79 lines
1.1 KiB
C

/* cat -- third revision of a program to concatenate files and/or stdin onto */
/* stdout */
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include "support.h"
/* Params */
enum
{
U
};
int main(int argc, char **argv)
{
int i, i2;
char param_state[1] = {0};
FILE *fd;
i = i2 = 0;
fd = 0;
for(; (i = getopt(argc, argv, "u")) != -1;)
{
switch(i)
{
case 'u':
param_state[U] = 1;
break;
default:
throw(NEEDARG_FAIL, mastrcat(argv[0], " [-u] [file ...]"));
}
}
i = optind;
if(param_state[U])
{
setvbuf(stdout, NULL, _IONBF, 0);
}
do
{
if(argv[i] == NULL || !strcmp("-", argv[i]))
{
fd = fopen("/dev/stdin", "r");
}
else
{
if( !(fd = fopen(argv[i], "r")))
{
throw(FOPEN_FAIL, mastrcat(argv[0], mastrcat(": could not open file ", argv[i])));
}
}
for(i2 = 0; (i2 = fgetc(fd)) != EOF; i2 = 0)
{
fputc(i2, stdout);
}
i++;
}
while(i < argc);
exit(EXIT_SUCCESS);
}