subject

A terminal program allows you to communicate with the operating system through a series of commands. In this project, you are going to write a very simple terminal program that is capable of understanding the following commands: - ls : list the files and directories in the current working directory - cd : change the working directory to the directory given in the path argument. - pwd : prints the current working directory - mkdir : creates a new directory inside the current working directory - rm : removes the file given in the path - exit : causes the terminal to terminate. The project will be implemented via functions. But you need to learn about some of C system library functions first. Include the following libraries in your program: , , and Library functions:
1. S_ISDIR() : This function allows you to check whether a given path is a directory or not. It returns either true or false. struct stat buf; //data structure that holds file status lstat("/home/ubuntu/opencv. zip", &buf); // fills buffer about the argument path if(S_ISDIR(buf. st_mode)) // checks if path is a directory cout << "It is a directory" << endl; Similarly S_ISREG() returns true if the path is a file.
2. getcwd() : This function returns the current working directory in the form of a C string which is a char * cout << getcwd(NULL, 0) << endl; 3. Listing files inside a directory can be done via two functions: opendir() and readdir() DIR * dp; //directory data structure struct dirent * dirp; //directory entry data structure dp = opendir("/home/ubuntu"); //opens the directory for the given path while((dirp = readdir(dp)) != NULL) //reads directory entries one by one in dirp data structurecout d_name << endl;
4. c_str() : The functions introduced above accept C-strings as arguments. To convert a regular string variable into a C-string, we can call the c_str() function on the string object. The first sample program can be altered as follows: struct stat buf; //data structure that holds file status string path = "/home/ubuntu/opencv. zip"; lstat(path. c_str(), &buf); // fills the data structure about the argument path if(S_ISDIR(buf. st_mode)) // checks if path is a directory cout << "It is a directory" << endl;
5. mkdir(): Creates a new directory. int status; status = mkdir("newdir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); The function creates a directory named "newdir" under the current working directory using read-write-execute privileges for user and group and read-execute privileges for others.
6. chdir(): changes the current working directory to a different one. if(chdir("/home/ubuntu")<0) cout << "Coud not open path: " << path; 7. remove(): deletes the file or directory given a certain path. remove("/home/ubuntu/sample. txt"); Step1: Define and implement a function named isFile() based on the following prototype:
bool isFile(string path); The function will return true if the given path is a regular file and return false otherwise.
Step2: Define and implement a function named listFiles() based on the following prototype: void listFiles(string dirPath); The function will print all the files and directories under dirPath directory. You may assume that dirPath is a directory.
Step3: Define and implement a function named getCurrentDirectory() based on the following prototype: char * getCurrentDirectory(); This function does not take any parameter but returns the current working directory in the form of a C-string.
Step4. Define and implement a function named removeFile() based on the following prototype: void removeFile(string path); The function must check first whether the path is a file and then remove the file. Otherwise, the function prints a message saying "Cannot remove, it is not a file."
Step5: Write a main function which will call the functions above to implement a simple terminal program. The terminal program will accept commands from the user and act based
on the command type.
Sample output: ~/Spring2021/CS101/project2/ $ ./terminal myterminal>ls samples. cpp samples . terminal terminal. cpp .. myterminal>pwd /home/ubuntu/Spring2021/CS101/proje ct2 myterminal>cd .. /home/ubuntu/Spring2021/CS101 myterminal>ls Lab1 Lecture2 Lecture1 Lecture9 Lab3 . Lecture6 Lab2 Lecture3 Lab4 project2
mockexam1 .. Lecture7 myterminal>rm Lecture9/math myterminal>cd Lecture9/ /home/ubuntu/Spring2021/CS101/Lectu re9 myterminal>ls math. cpp . .. myterminal>mkdir newdir myterminal>ls math. cpp . newdir .. myterminal>cd newdir /home/ubuntu/Spring2021/CS101/Lectu re9/newdir myterminal>ls . .. myterminal>exit

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 19:00
Stacy works as blank. the most important soft skill she needs for this role is blank.
Answers: 3
question
Computers and Technology, 23.06.2019 00:30
Knowing that the central portion of link bd has a uniform cross sectional area of 800 mm2 , determine the magnitude of the load p for which the normal stress in link bd is 50 mpa. (hint: link bd is a two-force member.) ans: p = 62.7 kn
Answers: 2
question
Computers and Technology, 23.06.2019 06:00
Which statement is true of web-based social media? a.they allow consumers to interact with and update content. b.they cannot be updated easily, as compared to print media. c.they are expensive to produce and maintain, as compared to print and television. d.they can exist independent of the internet.
Answers: 1
question
Computers and Technology, 23.06.2019 07:10
If you want to import a picture into a dtp application, what must you do first? draw an image frame. import text. open the folder containing the file. select get image… from the windows menu.
Answers: 2
You know the right answer?
A terminal program allows you to communicate with the operating system through a series of commands....
Questions
question
Mathematics, 04.12.2021 03:10
question
Mathematics, 04.12.2021 03:10
question
Mathematics, 04.12.2021 03:10
question
Mathematics, 04.12.2021 03:10
Questions on the website: 13722363