0% found this document useful (0 votes)
68 views

Lab Exercise 4 Implementation of Ls and Grep Commands Using System Calls Aim

1. The document describes a lab exercise to implement the ls and grep commands using system calls in C. 2. For ls, it involves opening a directory, reading entries using readdir(), and outputting the file/folder names. For grep, it involves opening a file, reading line by line, and outputting lines that match the search pattern. 3. Various options for both commands are listed like recursive listing for ls (-R) and line number or count output for grep.

Uploaded by

Anirudh Venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Lab Exercise 4 Implementation of Ls and Grep Commands Using System Calls Aim

1. The document describes a lab exercise to implement the ls and grep commands using system calls in C. 2. For ls, it involves opening a directory, reading entries using readdir(), and outputting the file/folder names. For grep, it involves opening a file, reading line by line, and outputting lines that match the search pattern. 3. Various options for both commands are listed like recursive listing for ls (-R) and line number or count output for grep.

Uploaded by

Anirudh Venkat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SSN COLLEGE OF ENGINEERING, KALAVAKKAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 141451 - OPERATING SYSTEM LAB -----------------------------------------------------------------------------------------------------------Lab Exercise

4 Implementation of ls and grep commands using system calls

AIM: Develop a C program to implement the ls and grep commands using system calls. Simulation of grep and ls command 1. The arguments should be obtained in command line and error messages should be printed if they are not sufficient. 2. Using open, read, write, close, opendir, readdir, closedir and other necessary system calls do the operations. 3. Use functions for need. [ pattern checking, Dir contents printing ] 4. Various operations for grep 1. mygrep pattern filename 2. mygrep pattern filename1 filename2 .. filenamen 3. mygrep c pattern filename 4. mygrep v pattern filename 5. mygrep n pattern filename 5. Various operations for ls 1. myls 2. myls dirname 3. myls R dirname 4. myls -1 [ number 1] 5. myls l [ long listing] 6. Using DIR pointer open the directory and check for existence. 7. Using dirent structure read the directory and display its details until you are getting NULL on readdir. 8. For R the subdirectories contents should also be listed. 9. The failure messages for opening a file, directory should be intimated. ALGORITHM for ls command: 1. To view the files in a directory include dirent.h that helps for opening, reading, closing a directory. 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. ALGORITHM for grep command: 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 searching for. 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. SAMPLE INPUT & OUTPUT FOR ls COMMAND: Enter the name of the directory (specify path name): ./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 FOR grep COMMAND: INPUT : programname argument1 argument2 argument1: name of the file name to be opened argument2: pattern to be searched in the argument 1 OUTPUT: Display the contents of the file that has the pattern in it

You might also like