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