To develop a C program to implement the cp, ls, grep commands (with some options) using system calls.
Sample Learning Outcome:
- Implement the various system commands like cp, grep, ls, head, tail, wc using system calls
- Learn to process command line arguments and error handling mechanism
- Understand the relation between the system calls and commands
Best Practices:
- Algorithm design
- Naming convention for file names, variables
- Comment usage at proper places
- Prompt messages during reading input and displaying output
- Error handling mechanisms for failures in system calls
- Incremental program development
- Modularity
- All possible test cases in output
AIM:
To develop a C program to implement the cp, ls, grep commands (with some options) using system calls.
cp command: basic cp, -i To copy a file into another ls command: basic ls, -1, -R
To list all files in the directory
grep command: basic grep, -c, -v, -n
To search the given pattern in the file
Procedure for cp:
- The arguments should be obtained in command line and error messages should be printed if they are not sufficient.
- Use open, read, write, creat ,close system calls to do the following.
mycp sourcefilename destinationfilename
-copies source file to destination file
- The failure messages for opening a file, creating a file should be intimated.Note: mycp is the user programs implementing cp.
Procedure for ls:
- To view the files in a directory include dirent.h that helps for opening, reading, closing a directory.
- Open the user named directory giving specific path using opendir system call. This returnsa pointer to a DIR data structure that represents a directory. 3. Can even use . to represent the current working directory.
- Traverse the directory entries using readdir system call. readdir () returns a pointer to a
dirent structure whose member d_name contains the name of the current file.
- Output the entries of directory.
- Close the directory pointer
NOTE: Use open, read, write, creat ,close, opendir, readdir, closedir system calls wherever necessary.
Procedure for grep:
- Open the command line specified file using the required system call.
- Read the contents iteratively till the end of the file and compare it with the pattern you aresearching for.
- If word found print the line on to the display.
- Count the number of occurrences and display it finally.
- Close the file descriptor.
NOTE: Use open, read, write, creat ,close system calls wherever necessary.
SAMPLE INPUT/OUTPUT: cp:
Source.txt:
SSN COLLEGE OF ENGINEERING
target.txt:
SSN NAGAR
KALAVAKKAM
$ ./mycp source.txt target.txt
FILE COPIED!
ls: $ ./myls lab
OUTPUT:
.
..
diros diros.zip Ex-3-cp-cat.doc
Ex-3-cp-cat.pdf Ex-4-ls-grep.doc fork.pdf grep.doc prgs.doc sys-call prgs.doc grep:
$ ./mygrep pattern filename
OUTPUT:
Display the contents of the file that has the pattern in it

![[Solved] UCS1411 Exercise 2- System calls](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[Solved] UCS1411 Exercise 3- Nonpreemptive and Preemptive](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.