---
description: 450 points
---

# A4: Diff

{% hint style="info" %}
**CMake target**: `a4`&#x20;

```bash
cmake --build .build --target a4
```
{% endhint %}

How does one check whether the copied homework is modified enough not to arouse suspicion? Well simple, check every line and see if it is different in some way. Now we are among CS people here so clearly any task that can be automated will be automated (because it clearly is [the more time-efficient approach](https://xkcd.com/1319/), right?).

***

## Assignment

Implement a simplified `my_diff` function for performing a line-based comparison of two files. The Unix `diff` utility is a very powerful and complex tool for comparing files (and directories), your program will only cover a small subset of the functionality.

Given two files:

{% tabs %}
{% tab title="fileA.txt" %}
{% code lineNumbers="true" %}
```
Hi, this is a test file.
Test file 1 to be precise.
```
{% endcode %}
{% endtab %}

{% tab title="fileB.txt" %}
{% code lineNumbers="true" %}
```
Hi, this is a test file.
Test file 2 to be precise.
```
{% endcode %}
{% endtab %}
{% endtabs %}

the output of your program should look like the following:

```
2c2
< Test file 1 to be precise.
---
> Test file 2 to be precise.
```

The output shows that line 2 differs between the files (`2c2` means that line 2 in the original has been changed to become line 2 in the new file). For more examples, have a look at the test files and the expected outputs given in the framework.

{% hint style="warning" %}
The exact way this `my_diff` should handle certain cases may be up to interpretation. As some agreement is needed to allow for automated testing, please consider the interpretation of the teaching team (as given in the expected outputs) as the "correct" interpretation.

Make sure you **investigate the expected outputs for all given inputs before you start** with your implementation to avoid implementing a version that differs from the expected behavior.
{% endhint %}

### Subroutine

Implement a subroutine with the following signature:

```cpp
int my_diff(const char *a, const char *b, bool flag_i, bool flag_B)
```

`a` and `b` are the filenames of the two files to be compared, while `flag_i` and `flag_B` indicate whether the associated flag is active.

Your subroutine should open the files, read and compare their contents line by line, print the result of the comparison, close the files, and finally return with the proper return value (check the man page of the `diff` utility for information on the return codes). You may assume that lines are not longer than 1,023 characters (excluding the newline character). In the case that one file has more lines than the other, you should treat it as if the shorter file has (multiple) empty lines at the end to match the length of the other.

Your implementation should further support both the `-i` and the `-B` flags (and their combination). For details on the behavior of the flags, consider the `diff` man page and the given inputs/expected outputs in the framework. Make sure that your implementation acts as expected by the tests (which might be different from the Unix `diff` behavior due to the simplification).

{% hint style="success" %}
**Note:** You are allowed to use any C library function listed at the top of the framework file. There are more functions in the list than you will need, so consider carefully which ones are relevant to you (and have a look at their man pages to get an idea of how they work).
{% endhint %}

### Main Program

The `main` program for this assignment is already given in the form of the `a4-diff-main.c` file. Its `main` method takes care of parsing the command line arguments and calling your `my_diff` function with the appropriate arguments.
