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
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.
Check the permissions of the output file. If any permissions exist, print a useful error message and exit.
Change the permissions on the file to allow writing by the user.
Open the file for output. If the "-c" command line option is present, make sure the file is truncated.
Write the message from the command line to the output file. Write an additional newline character so that the output has a nicer format.
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 "z123456" here.
% rm log
% ./z123456 Usage: seclog [-c] out_file message_string where the message_string is appended to file out_file. The -c option clears the file before the message is appended
% chmod u-w .
% ./z123456 log "Hello" Permission denied
% chmod u+w .
% ./z123456 log "Hello"
% ls -l total 72 ---------- 1 z123456 student 6 Sep 24 18:39 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o
% ./z123456 log "Hello"
% ls -l total 72 ---------- 1 z123456 student 12 Sep 24 18:40 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o
% chmod 400 log
% tail log Hello Hello
% ./z123456 log "Wait, there's more" log is not secure. Ignoring.
% chmod 000 log
% ./z123456 log "Wait, there's more"
% ls -l total 72 ---------- 1 z123456 student 31 Sep 24 18:41 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o
% chmod 400 log
% tail log Hello Hello Wait, there's more
% chmod 000 log
% ./z123456 -c log "Clean start"
% ls -l total 72 ---------- 1 z123456 student 12 Sep 24 18:41 log -rwxr-xr-x 1 z123456 student 26385 Sep 24 18:38 z123456 -rw-r--r-- 1 z123456 student 2204 Sep 24 18:36 z123456.cxx -rw-r--r-- 1 z123456 student 30896 Sep 24 18:38 z123456.o
% chmod 400 log
% tail log Clean start
% chmod 000 log
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ofstream;
using std::ios_base;
using std::ifstream;
using std::cerr;
bool fileexists = false;
int level = 1;
string weapon;
int main()
{
char text[200];
cout << endl << "Checking for save
file..." << endl;
//sleep(2);
ofstream savefile;
savefile.open("Desktop/savefile.txt");
if(savefile.good())
{
fileexists = true;
}
if(fileexists == true)
{
cout << endl
<< "Savefile found, reading..." << endl;
// sleep(1);
}
else if(fileexists == false)
{
cout << endl
<< "Savefile not found, creating one on Desktop" <<
endl;
savefile.open("Desktop/savefile.txt", ios_base::out);
cin.get();
}
cout << "Write text to be written on file."
<< endl;
cin.getline(text, sizeof(text));
savefile << text << endl;
FILE *fp = fopen("Desktop/savefile.txt", "w");
if (fp == NULL) {
//if (errno == EACCES)
cerr <<
"Permission denied" << endl;
else
//cerr << "Something
went wrong: " << endl;
}
savefile.close();
return 0;
}
}
This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(),...
Purpose
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...
write the following code using C language. It will be ran in
Ubuntu running linux OS
2 File Management System Write a basic file managment system with following capabilities. Suppose the name of the program is myFS. When it executes as below: myFS Is - /home Figure 6 : First command format The program should provides the file listing of the given directory. The program should list the filename, file size, and time last modified for each file within the...
(In Linux) 1. Create a file to write to from your script and save it as “.txt”. 2. Write the following information to the file that you created in Step 1: Today's date Your full name Your student ID The name of your course What directory you are in File permissions of the file that you created that allow everyone to read, write, and execute The long list of files in your current directory, providing all items and their process...
Write a program in C using Unix system calls and functions that will change the permissions on a file. The executable shall be called “mychmod” and will be executed by: mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ... The lowercase options will add permissions while the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1), you can review...
do numbers 4-8
4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...
In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...
Question 2 0/1 point (graded) What happens when you remove a directory using the command rm -r? You cannot remove a directory using the rm command. You permanently remove the entire directory, including all files and subdirectories. You move the entire directory to a trash folder, but it can be restored later. You get a warning message asking if you want to proceed, then you delete the directory. incorrect Answer Incorrect: Try again. Unix does not warn you before permanently...
Write c program. Do part 4 and 5
CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...
Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...
Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output: << CS Directory Analysis >> Date: ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files: 0 files Plain text files: 10 files File have read permissions: 3 files File have...