Question: Assignment Content This assignment should give you experience in using file descriptors, open ( ) , close ( ) , write ( ) , stat

Assignment Content
This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments.
Program
Write a C++ program that will allow you to add messages to a file that has NO permissions for any user.
A Unix system has many files that have sensitive information in them. Permissions help keep these files secure.
Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd).
Other files can't be read at all by a regular user (ex.: /etc/shadow).
The program you develop should take a message given as a command line argument and append it to a file (also specified on the command line). The file should have no permissions, both before and after the message is appended.
Of course, the file should be owned by you.
Your program should also have a -c option that will clear the file before the message is appended.
Algorithm
1. Check to see if the output file exists. If it doesn't, create it. Life is simpler if a newly created file is closed at the end of this step.
2. Check the permissions of the output file. If any permissions exist, print a useful error message and exit.
3. Change the permissions on the file to allow writing by the user.
4. Open the file for output. If the "-c" command line option is present, make sure the file is truncated.
5. Write the message from the command line to the output file. Write an additional newline character so that the output has a nicer format.
6. Clear the permissions and close the file. (These two operations can be performed in either order, but the implementation is slightly different.)
Useful Hints
- Don't use the command line arguments directly. Their position in the argument list may change depending on options. Create meaningful char* variables and fill these with the appropriate entries from the argument list.
- Check for error values after nearly every system function call. It's a pain to set up, but it saves time in the long run.
Input
None, really. Just command line arguments.
Error Checking
If the log file cannot be opened, an appropriate error message should be printed and the program should exit. If the file has any permissions at all, the file should be rejected as insecure, and the program should exit. Example Run
Your program executable is called "z1234567" here.
Other Points
- Make sure that your assignment is contained in a single file called "z1234567.cxx" based on your Z-id.
- Make sure to use Unix system calls for the tasks required. Don't use higher level C or C++ functions or libraries.
- Make sure that your program runs fine on your personal Linux system, turing or hopper.
Assignment Content This assignment should give

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 Programming Questions!