2020-12-12 06:33:20 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
2020-02-12 05:30:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
FILE *in_file;
|
|
|
|
int i, error = 0, eof_reached = 0;
|
|
|
|
char c, c_temp;
|
|
|
|
|
|
|
|
for(i = 1; i < argc; i++) {
|
2022-10-06 07:12:56 +00:00
|
|
|
/* If - is passed as an argument, set the input file to stdin */
|
|
|
|
if(argv[i][0] == '-' && argv[i][1] == '\0') {
|
2020-02-12 05:30:50 +00:00
|
|
|
in_file = stdin;
|
2022-10-06 07:12:56 +00:00
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
else {
|
2022-10-06 07:12:56 +00:00
|
|
|
/* Open the file given */
|
2020-02-12 05:30:50 +00:00
|
|
|
in_file = fopen(argv[i], "r");
|
|
|
|
if(in_file == NULL) {
|
2022-10-06 07:12:56 +00:00
|
|
|
/* If it can't open, set that we encountered an error */
|
|
|
|
/*
|
|
|
|
TODO: this should probably check errno instead of
|
|
|
|
assuming that we can't find the file
|
|
|
|
*/
|
2020-12-13 15:17:40 +00:00
|
|
|
fprintf(stderr, "Cannot find file %s\n", argv[i]);
|
|
|
|
error = 1;
|
|
|
|
continue;
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-06 07:12:56 +00:00
|
|
|
/*
|
|
|
|
Loop through the file line by line, applying transformations based on
|
|
|
|
the first character of the line, as given by the POSIX spec.
|
|
|
|
*/
|
2020-02-12 05:30:50 +00:00
|
|
|
eof_reached = 0;
|
|
|
|
while(!eof_reached) {
|
|
|
|
c = fgetc(in_file);
|
2022-10-06 07:12:56 +00:00
|
|
|
/*
|
|
|
|
Get the first character of the line and print the right thing
|
|
|
|
*/
|
2020-02-12 05:30:50 +00:00
|
|
|
switch(c) {
|
2022-10-06 07:12:56 +00:00
|
|
|
case '1': printf("\f"); break; /* Form feed on 1 */
|
|
|
|
case '0': printf("\n"); break; /* New line on 0 */
|
|
|
|
case '+': printf("\r"); break; /* Carriage return on + */
|
|
|
|
case EOF: eof_reached = 1; continue; /* Leave on EOF */
|
|
|
|
/* Anything else, don't do anything */
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
2022-10-06 07:12:56 +00:00
|
|
|
/* Print out the rest of the line unless we hit an EOF, where we quit. */
|
2020-02-12 05:30:50 +00:00
|
|
|
for(c_temp = fgetc(in_file); c_temp != '\n'; c_temp = fgetc(in_file)) {
|
2020-12-13 15:17:40 +00:00
|
|
|
if(c_temp == EOF) {
|
|
|
|
eof_reached = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("%c", c_temp);
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2022-10-06 07:12:56 +00:00
|
|
|
/* If we ever hit an error, return 1. */
|
|
|
|
if(error) {
|
2020-02-12 05:30:50 +00:00
|
|
|
return 1;
|
2022-10-06 07:12:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-02-12 05:30:50 +00:00
|
|
|
return 0;
|
2022-10-06 07:12:56 +00:00
|
|
|
}
|
2020-02-12 05:30:50 +00:00
|
|
|
}
|