Question: I need help with answering Lab Writeup please. Programming Tools Part IV: Using diff and patch Material Covered man diff man patch (a utility that

I need help with answering Lab Writeup please.

Programming Tools Part IV: Using diff and patch

Material Covered

man diff

man patch (a utility that will take the output of diff and make changes accordingly)

Part I: Diff

This lab will cover the UNIX file utility diff. diff compares two or more files for differences. Order is extremely important when using diff; calls should be made as follows:

diff

diff produces output which indicates how the files differ. The format of the output can be changed by giving specific options to diff. The default format is as follows:

The two types of output are line number indicators and line contents. The line number indicator line consists of:

line_list1 type line_list2

where line_list1 are line numbers from the original file, line_list2 are line numbers from the new file and type is one of the following:

a

means that lines will need to be added, or appended, to the original file in order to obtain the results shown in the new file

d

means that lines will need to be deleted in the original file to make it identical to the new file

c

means that lines have been changed between the original file and the new file

The line contents are printed out after the line number indicator. Each line of content is prefaced by one of the following three indicators:

<

refers to lines in the original file that do not appear in the new

>

refers to lines in the new file that do not appear in the original

<>

lines that appear in both files that have been changed

Example:

$ diff date.old date.new 1c1 < Mon Jul 2 13:12:16 PDT 1990 --- > Tue Jun 19 21:41:39 PDT 1990

This means that line 1 in date.old has changed from "Mon Jul 2 13:12:16 PDT 1990" to "Tue Jun 19 21:41:39 PDT 1990" in date.new. All the other lines are the same.

Part II: Patch

diff can also be told to output in unified output format using the -u option:

diff -u

This produces output similar to the following:

$ diff -u memcode1.cpp memcode2.cpp --- memcode1.cpp 2009-11-18 15:27:19.000000000 -0800 +++ memcode2.cpp 2009-11-18 15:27:32.000000000 -0800 @@ -1,4 +1,4 @@ - for(int i = 1; i <= 7; i++) + for(int i = 0; i < 7; i++) { arr[i] += i; }

The --- line gives information about the first file. The +++ line gives information about the second file. Any line surrounded by@@ identifies a chunk of text that differs. The line numbers preceeded by - are the line numbers in the first file. The line numbers preceeding by + are the line numbers in the second file. This is then followed by information about the chunk. Several lines from the chunk are shown. Lines from the first file are marked with -. This indicates lines that need to be removed from the first file to make it identical to the second file. Lines from the second file are marked with +. This indicates lines that must be added to the first file to make it identical to the second file. Lines in both files have a space at the start of the line.

The patch utility can take the output of diff and recreate the second file from the first file. It can take several different output formats from diff, but the unified output format is the most commonly used in coding circles. If you have the unified output from diff in a file, you invoke patch as follows:

patch -p

where filename is NOT the name of the original file but is instead the name of the file containing the diff output. num indicates that the filenames given on the --- and +++ lines need to be trimmed to match the local filenames. A num of 0 would leave the filenames untouched. A num of 1 would remove everything up to the first / in the filename. A num of 2 would remove everything up to the second / in the filename. So num is the number of directories you need to trim off the filenames to get the filenames to match your local directory structure.

Lab Writeup

Explain what the following diff outputs and patch commands would do.

Chunk 1:

1c1 < // this is the original file --- > // this is the new file

Chunk 2:

2a3 > // Additional comments added to the new file

Chunk 3:

7a9,11 > class Time; // forward reference > istream &operator >> (istream &, Time &); >

Chunk 4:

8a13 > friend ostream &operator<<(ostream &, const Time &);

Chunk 5:

13a19 > Time &setTime( const Time & ); // set Time object

Chunk 6:

25d30 < void printMilitary() const; // print military time

Assume you had the following output in a file called diff_file:

--- hw4_2.cpp 2009-11-18 14:54:47.000000000 -0800 +++ hw4_2b.cpp 2009-11-18 14:56:04.000000000 -0800 @@ -14,18 +14,14 @@ int size; // Current number of elements in nums double avg, stdev; - cout << "How many integers do you wish to enter? "; - cin >> size; - // Validate that size is between 2 and MAX_SIZE. We use 2 as the lower bound // since you cannot compute standard deviation with only 1 value. Validation // loops always use the inverse of the desired values as the while statement - while(size < 2 || size > MAX_SIZE) + do { - cout << "Invalid size. Valid sizes are 2 to " << MAX_SIZE; - cout << ". How many integers? "; + cout << "How many integers do you wish to enter (2 to " << MAX_SIZE << ")? "; cin >> size; - } + } while(size < 2 || size > MAX_SIZE); // Now we can prompt the user for each integer for(int i = 0; i < size; i++)

What would the command patch -p0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!