Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CMPT 214– Programming Principles and Practice

Assignment 6

2022

General Submission Instructions

• Assignments must be submitted using Canvas.

• Programs must be written in C conforming to the C11 standard. Everything we teach you is compli- ant with the C11 standard. Things we haven’t taught you might not be, so use only the features of C that we have taught. If you try things you find on other websites (where you should definitely not be looking for solutions) they may not be C11 compliant. Everything you need to solve programming problems can be found in the course materials, or in the assignment itself.

• Include the following identification in a comment at the top of all your code files: your name, NSID, student ID, instructor’s name, course name, and course section number.

• Late assignments are accepted with a penalty depending on how late it is received. Assignments are not accepted more than 2 days late (the assignment submission will close 48 hours after the due date passes and you will not be able to submit). See the course syllabus for the full late assignment and assignment extension policies for this class.

• VERY IMPORTANT: Canvas is very fragile when it comes to submitting multiple files. We insist that you package all of the files for all questions for the entire assignment into a single ZIP archive file. This can be done with a feature built into the Windows explorer (Windows), or with the zip terminal command (LINUX and Mac), or by selecting files in the Mac finder, right-clicking, and choosing "Compress ...". We cannot accept any other archive formats other than ZIP files. This means no  tar, no gzip,  no 7zip (.7zip),  and no WinRAR (.rar) files.  Non-ZIP archives will not      be graded.

• Instructions on "how to create zip archives" can be found here:

https://canvas.usask.ca/courses/62306/pages/how-to-zip-slash-compress-your-files


Background

Additional Useful LINUX/UNIX Commands

A very useful command on LINUX/UNIX is the tr command. It is not discussed in the textbook. How- ever, there are many resources on the internet describing tr and its uses, including one at wikipedia. The man page for the tr command is also very helpful.

The hostname command will tell you the identity of the machine that you are currently working on.

 Online Manual Pages

The UNIX/LINUX online manual pages (man pages) are divided into a number of sections. Section 1, for example, describes user commands like ls or vi. Section 3 of the man pages describes C library functions.

,All the sections and what they cover is in the output from ,

z man man r

on tuxworld.usask.ca. In addition, Section 17.2 of the textbook describes the man command, the sections of the man pages, and much more.

Sometimes man pages with the same name will exist in different sections of the online manual pages. An example of this are the pages for printf. There is a page for printf in Section 1 as well as in Section 3. To disambiguate this situation, when discussing a topic like “printf” authors will often include the section of the man pages that is relevant. For example, when an author is talking about the printf command they might indicate this by referring to “printf(1)”. On the other hand, if the author is talking about the printf library function, they might indicate this by referring to “printf(3)”. Further, even when it is not the case that there are multiple pages with the same name in the man pages, authors will include the section number to indicate whether they are talking about a user command, a system administrator command, a system call, a library function, etc. Hence, in the term “who(1)” the “(1)” indicates that who is a user command. Similarly, in the term “strncpy(3)” the “(3)” indicates that strncpy is a C library function.

 Use of Outside Shell Commands and Syntax Forbidden

Even moreso than C, there are many ways to perform the same tasks using the BASH shell and LINUX commands. But it is also an important skill in computer science to learn to work within a set of resources that you are given. To solve the problems on this assignment you must make use of only LINUX shell commands and command syntax that have appeared in the course materials. Use of any other commands or command syntax that you might find on the internet is forbidden and will result in a grade of zero on a question.



Question 1 (10 points):

Purpose: To practice using git to manage software and file versions

In this question you will modify the source for a UNIX command and then create a Makefile for building the executable program from the modified source. All the time, you will be using git to manage version control and track changes. In the end you will have source code that successfully compiles to a working version of rs(1), a Makefile to build the software, and a revision history from git.

The rs command exists on BSD variants of UNIX, including MacOS. For example, those students who are using an Apple computer can type “man  rs” to the shell in a terminal window and read about  the rs command. The man page is also available online. Unfortunately (or perhaps fortunately), the command does not exist on LINUX.

You  will be using git to manage and track files as you  progress through this question.  Make sure    to review Sections 16.5.1 and 16.5.2 of the textbook and to have gone through the Topic 16 Exercise materials before starting.

Your Tasks

You must do these tasks on tuxworld.usask.ca or we cannot guarantee that the results will be as expected.

1. Find the source code for rs(1) in a file called rs.c in the starter pack for this assignment. This source code, as provided, will not compile successfully on LINUX.

2. Create a subdirectory for working on this question. In that directory, initialize an empty git repository. Copy or move the provided rs.c to your working directory. Add rs.c to the master branch in your repository. Commit the change, providing an appropriate and informative commit message.

While performing the steps in this task, feel free to check the status of your repository with “git status”, to confirm what branch you are working on with “git branch”, or to try compiling rs.c.

3. There are the workflows described in Section 16.5.2 of the textbook. For this assignment you must follow the second workflow, the one that involves using a develop branch to work on and test new features before merging the changes into the master branch. Therefore, at this point create a develop branch, and then “checkout” that branch.

4. The changes that you need to make to rs.c are provided for you in a text file named rs.diff in the starter pack. This file was produced via “git diff” where the original version of rs.c was in the programmer’s repository and a modified version was present in the working directory but had not yet been committed to the repository. Recall that interpretation of output from a “git diff” command was discussed in part 3a of the Topic 16 walk-through.

5. Using a text editor and the content of rs.diff as a guide, modify the copy of rs.c in your working directory so that it compiles without warnings or errors. You will know that you have

,made all the necessary changes when the output of ,

z git diff r

indicates the same changes as are specified in the provided rs.diff file.

Should you  need to restart the editing process, it is easy to get back to the original rs.c thanks  to git. Assuming that step 2 above was completed successfully, git will have safely stored away the original version of that file in the repository. All you need is the command


, git checkout  -- rs. c ,

zto get it back. r

6. Once you have  made the necessary changes to rs.c, confirm that it compiles with out warning  or errors using the

“-Wall -Wextra” options, and executes correctly. You could try, for example, the commands

who

z  who  | ./ rs -h r

and verify that the output from rs is correct given the output from who(1). (You can read about the functionality of who(1) in its man page. The meaning of the “-h” option to rs(1) is described in its manual page on the internet as well.)

7. Commit your modified rs.c to your repository. Provide a meaningful, informative commit mes- sage.

8. Create a Makefile to build rs from rs.c. The Makefile is also to have rules for the target clean. Executing the command “make clean” should remove any .o files or pre-existing rs file.

9. Once your Makefile is working correctly, add it to the set of files being managed by git and commit it to your repository. Provide a meaningful, informative message.

10. As described in the second workflow in Section 16.5.2, at this point “checkout” the master branch once again and merge your changes (modifications to rs.c and addition of Makefile) from the develop branch into the master branch.

11. With your current branch being master, execute the following command

git   log   -- decorate= full   >   asn6q1 . log

You will need to submit asn6q1.log. The content of asn6q1.log should the same as what you see on the terminal from a “git log” command, except without any use of coloured text.

Testing

• Make sure to remove any existing rs or .o files before testing your Makefile in step 8. Also remember to test the rs file generated by your Makefile.


Question 2 (6 points):

Purpose: To practice using I/O redirection in LINUX/UNIX commands

In this question you will be making use of the command bc(1), an interpreter for an arbitrary pre- cision calculator language. (“Arbitrary precision” means that the user can set any number [up to an extremely high maximum] of digits of precision after the decimal point for the results of calculations – read about the scale variable in the man page for bc.) As well, you will be making use of a command called echo that is used extensively in shell scripts and is described in Section 17.1.5 of the textbook. To find out more about bc(1) or echo(1), read the man page for it.

Sample Output

The man page for bc(1) on tuxworld says “This version truncates results from divide and multiply operations” and “Unless specifically mentioned the scale of the result is the maximum scale of the expressions involved.” This means, for example, that bc(1) will behave as follows (green indicates user input, black indicates computer output):

bash -5.1 $   bc bc   1.07.1

Copyright   1991 -1994 ,   1997 ,   1998 ,   2000 ,   2004 ,   2006 ,   2008 ,   2012 -2017   Free   Software   Fo

This   is   free   software   with   ABSOLUTELY   NO   WARRANTY . For   details   type   ‘ warranty ’.

3.3 * 4.4

14.5

even though the result should have been 14.52. If you would like bc(1) to produce more precise results, read about the setting of the scale variable.

Your Tasks

1. Create five different arithmetic expressions in the language supported by bc(1). Each must involve at least one addition, one subtraction, and one multiplication operation and at least 5 different numbers. The expression may also involve division if you wish. It must be possible for bc(1) to successfully evaluate your five expressions. Note that because of how bc(1) handles the scale of results from divide and multiply operations (discussed above), the value output might be different from the true value.

2. Have bc(1) evaluate each of your five expressions. You can end your interactive session with bc by typing quit or by providing an end-of-file (usually the Control-D character). Create a file asn6q2.log that is a log of running bc(1) and successfully evaluating each of your five expressions.

3. Let <expression n> represent your n-th arithmetic expression from step 1. Place your five ex- pressions in a text file named as6q2.in by issuing the following commands:

echo "< expression 1 >" > asn6q2 . in echo "< expression 2 >" >> asn6q2 . in echo "< expression 3 >" >> asn6q2 . in echo "< expression 4 >" >> asn6q2 . in

z  echo  "< expression  5 >"  >> asn6q2 . in r

where each <expression n> is replaced by an actual arithmetic expression. Make sure to place quotes around your expression if it contains any white space characters (see Section 21.2.2 of thetextbook).  Also make sure to use the correct type of redirection.  Add to asn6q2.log a log of  doing the above 5 echo commands.



4. Run bc(1) with its standard input redirected to/from as6q2.in. Add to asn6q2.log a log of doing this.

5. Run bc(1) with its input redirected to/from as6q2.in and its standard output redirected to

asn6q2.out. Add to asn6q2.log a log of doing this.

 

Implementation Notes

 

,

r

,

r

of the shell option noclobber in the documentation for the LINUX/UNIX shell.



Question 3 (9 points):

Purpose: to gain some experience with the LINUX/UNIX file system, practice working with files in LINUX/UNIX, and develop skills with compound shell commands by writing a simple pipeline.

There is no option to ls(1) to say  “only output (information about) directories".   In this question   you will create a solution to this shortcoming. To do so you will be making use of tr(1) (see the Background section of this assignment specification),  the “-p” option to ls(1), the grep command  (see Table 19.4 and Sections 21.1.1 and 21.1.2 of the textbook), and the rs command that you built in Question 1. You will also be creating a shell pipeline. Such constructions are discussed in Section

21.1.2 of the textbook.

Also in this question you will explore some of the important LINUX/UNIX system directories men- tioned in Table 19.2 in the textbook.

 

Preparing your Environment

This question must be done on tuxworld.usask.ca.

1. On tuxworld, student accounts have an automatically set up shell alias ls command that prevents completing this question correctly. Before you begin, run the command:

unalias ls

This will remove any shell aliases that have been set up for you by the default system configura- tion files. You will need to run this command every time you log into tuxworld with ssh as the unalias command only takes effect for the current shell session.

2. Make sure that the binary executable file rs that you created in question 1 is in your path. To do this, enter the command:

export   PATH = $PATH :< path   to   rs >

where <path to rs> is the path to your compiled rs program. For example, if your rs program is in a directory of your home directory called asn6, you would give the command as:

export PATH = $PATH :~/ asn6

This will allow you to execute the command rs from within any current directory which will avoid you having to specify the path to rs each time you use it in a command. To check whether you’ve got the PATH set up correctly, use cd / to change to the root directory, then enter the command rs. If nothing happens, that means rs is correctly in your path — press CTRL-C to get your terminal prompt back. If you see bash: rs: command not found... it means you haven’t set up your PATH correctly.

If you mess up your PATH such that no commands work any more, just log out of tuxworld and and log back in which will reset your PATH and you can try again.



Program Requirements

 

,

r

,

r

,

r

,

r

use of grep(1) and tr(1) with appropriate options or arguments. Any slash (‘/’) characters appended to directory names by the “-p” option of ls must be removed within your pipeline. You can assume that no directory names actually contain a slash (‘/’) character.

Testing

Make sure that the names being produced by your pipeline are the names of all the subdirectories in the current working directory, and only the names of subdirectories.

Once you have your pipeline working, test it as follows on tuxworld.usask.ca.

1. Change your current working directory to /usr. Run your pipeline, but with the arguments “0 4” given to the final rs (in the pipeline).

2. Change your current working directory to /usr/lib. Run your pipeline, but with the arguments “0 4” given to the final rs.

3. Change your current working directory to /usr/include. Run your pipeline, but with the argu- ments “0 3” given to the final rs.

Remember that all this testing must be conducted on tuxworld.usask.ca.

Submission Preparation

Create a log file named asn6q3.log.  Then add to asn6q3.log a log of executing the hostname com-  mand. Finally, add to asn6q3.log a log of completing testing steps 1, 2, 3 above. You will need to  submit asn6q3.log as your response to this question.

Files Provided

A .zip file archive named asn6_starter_pack.zip containing

rs.c: C source code for Question 1.

rs.diff: Output from “git diff” showing the changes that need to be made to rs.c

What to Hand In

Hand in a .zip file archive that contains the following files:

asn6q1.log: Log of the output called for in Question 1.

Makefile:  Makefile called for in Question 1.

rs.c:  Modified source code from Question 1.

asn6q2.log: Log of the output called for in Question 2. asn6q2.in and asn6q2.out: files produced in Question 2. asn6q3.log: Log of the output called for in Question 3.