1. To develop a C program to implement the cp, ls, grep commands (with some options) using
Sample Learning Outcome:
1. Implement the various system commands like cp, grep, ls, head, tail, wc using system calls
2. Learn to process command line arguments and error handling mechanism
3. Understand the relation between the system calls and commands
1. Algorithm design
2. Naming convention – for file names, variables
3. Comment usage at proper places
4. Prompt messages during reading input and displaying output
5. Error handling mechanisms for failures in system calls
6. Incremental program development
8. All possible test cases in output
To develop a C program to implement the cp, ls, grep commands (with some options) using
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:
1. The arguments should be obtained in command line and error messages should be printed
if they are not sufficient.
2. Use open, read, write, creat ,close system calls to do the following.
mycp sourcefilename destinationfilename
-copies source file to destination file
3. The failure messages for opening a file, creating a file should be intimated.
Note: mycp is the user programs implementing cp.
Procedure for ls:
1. To view the files in a directory include dirent.h that helps for opening, reading, closing a
2. Open the user named directory giving specific path using opendir system call. This returns
a pointer to a DIR data structure that represents a directory.
3. Can even use “.” to represent the current working directory.
4. 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.
5. Output the entries of directory.
6. Close the directory pointer
NOTE: Use open, read, write, creat ,close, opendir, readdir, closedir system calls wherever
Procedure for grep:
1. Open the command line specified file using the required system call.
2. Read the contents iteratively till the end of the file and compare it with the pattern you are
3. If word found print the line on to the display.
4. Count the number of occurrences and display it finally.
5. Close the file descriptor.
NOTE: Use open, read, write, creat ,close system calls wherever necessary.
SSN COLLEGE OF ENGINEERING
$ ./mycp source.txt target.txt
$ ./myls lab
$ ./mygrep pattern filename
Display the contents of the file that has the pattern in it