Starting from:

$25

CSE344 -  System Programming - Homework 1 - v2 - File I/O - Solved

The goal is to develop a simple text editor accomplishing a common task: string replacement. It must support the following:

a)   ./hw1 ‘/str1/str2/‘ inputFilePath

This will replace all occurrences of str1 with str2 in the file inputFilePath (relative or absolute)

b)  ./hw1 ‘/str1/str2/i‘ inputFilePath

This will perform the same as before, but will be case insensitive.

c)   ./hw1 ‘/str1/str2/i;/str3/str4/‘ inputFilePath It must be able to support multiple replacement operations.

d)  ./hw1 ‘/[zs]tr1/str2/‘ inputFilePath

It must support multiple character matching; e.g. this will match both ztr1 and str1

e)   ./hw1 ‘/^str1/str2/‘ inputFilePath

It must support matching at line starts; e.g. this will only match lines starting with str1

f)   ./hw1 ‘/str1$/str2/‘ inputFilePath

It must support matching at line ends; e.g. this will only match lines ending with str1

g)  ./hw1 ‘/st*r1/str2/‘ inputFilePath

It must support zero or more repetitions of characters; e.g. this will match sr1, str1, sttr1, sttttr1, etc

And of course it must support arbitrary combinations of the above;

e.g. ./hw1 ‘/^Window[sz]*/Linux/i;/close[dD]$/open/‘ inputFilePath

Assume that the user will try to match/replace only letters and digits, but no symbols or space. Hence you will not need escape characters.

Output

The output will be written to the input file. If you need temporary files, you must use mkstemp().

Is this all?

No. The program might be executed as multiple instances with different replacement commands, on the same file. This will result in multiple processes manipulating the same input file with potentially wrong results.

Imagine for instance one process replacing str1 with str2, and another replacing str2 with str3 at the same time. Depending on which process writes/reads what, the end result might vary. You cannot stop the user from invoking your program multiple times, so instead you must protect file access (via the locks seen during our lectures) by making sure than no second instance of the program runs on the same file, before the first one has finished its task with it.

For all file I/O operations, use the low-level system calls presented during our lecture and not C library functions. Make sure you control each system call’s return value and print informative messages via perror/strerror in case of errors. Make sure you print your program’s usage in case the commandline arguments are wrong.


More products