mp-utils/src/cat.c

79 lines
1.1 KiB
C
Raw Permalink Normal View History

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