Write a ‘C’ program that performs some functions of a file preprocessor. This pr
ID: 3672928 • Letter: W
Question
Write a ‘C’ program that performs some functions of a file preprocessor. This program should accept a –f option that specifies a filename to contain the output of the program. If the –f option is not specified, the program should send the output to standard output. You must use freopen(…) to create the stream for the program’s output if the output is going to standard output. The program should accept zero or more arguments. If no arguments are specified, the program should get it’s input from standard input. If one or more arguments are specified, the program should get its input from the files whose names are specified as arguments. If arguments are specified, the program should send to standard error the name of each file followed by the date/time the file was modified in the format YYYY_MM_DD_HH_MM_SS and then a trailing newline. For example, if the file named “file1.txt” was last modified on February 4, 2016 at 5:30 p.m., the program should send to standard error the following string: “file1.txt 2015_02_04_17_30_00 ” You must use getopt() to process the option and arguments. Examples (assumes the program has been built as an executable named “preprocess”): preprocess Accepts input from standard input and sends processed output to standard output preprocess –f outputfile.txt Accepts input from standard input and sends processed output to a file called outputfile.txt preprocess file1.txt file2.txt Accepts input from file1.txt followed by file2.txt and sends the combined processed output to standard output. Also sends to standard error the name of each file and the last modified date/time as specified above. preprocess –f outputfile.txt file1.txt file2.txt Accepts input from file1.txt followed by file2.txt and sends the combined processed output to a file called outputfile.txt. Also sends to standard error the name of each file and the last modified date/time as specified above. Processing As the input is read, it is simply copied to the output, with two exceptions: • If the string “#include” is found at the beginning of a line, it will be followed by a space then a filename in the form of ”. For example, you may see a line that begins with “#include ”. If the input contains that at the beginning of the line, output the contents of file3.txt rather than the “#include ”. The remainder of the line is ignored. If anything follows the on the line it is ignored and processing begins on the next line. • Anytime the string is found in the input, replace with the number of seconds elapsed since February 4, 2016 5:30 p.m.
Explanation / Answer
#include #include #include "syscalls.h" #define PERMS 0666 /* RW for owner, group, others */ void error(char *, ...); /* cp: copy f1 to f2 */ main(int argc, char *argv[]) { int f1, f2, n; char buf[BUFSIZ]; if (argc != 3) error("Usage: cp from to"); if ((f1 = open(argv[1], O_RDONLY, 0)) == -1) error("cp: can't open %s", argv[1]); if ((f2 = creat(argv[2], PERMS)) == -1) error("cp: can't create %s, mode %03o", argv[2], PERMS); while ((n = read(f1, buf, BUFSIZ)) > 0) if (write(f2, buf, n) != n) error("cp: write error on file %s", argv[2]); return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.