0% found this document useful (0 votes)
15 views5 pages

RTS-2 3

The document describes an experiment to write a program that simulates UNIX commands like ls, grep, etc. It discusses the purpose and syntax of commands like cp, ls, and grep. It then provides an algorithm and implementation in C++ to create files, copy the contents of one file to another, list the contents of a directory using ls, and search for patterns in a file using grep. The main function calls functions to perform each of these commands.

Uploaded by

19bcs1718
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)
15 views5 pages

RTS-2 3

The document describes an experiment to write a program that simulates UNIX commands like ls, grep, etc. It discusses the purpose and syntax of commands like cp, ls, and grep. It then provides an algorithm and implementation in C++ to create files, copy the contents of one file to another, list the contents of a directory using ls, and search for patterns in a file using grep. The main function calls functions to perform each of these commands.

Uploaded by

19bcs1718
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/ 5

Course Name: Real Time System Lab Course Code: CSP-457

Experiment: 2.3

Aim: Write a program to simulate UNIX Commands like ls, grep, etc.

Software Required: C++ Compiler

Description:

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that
pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global
search for regular expression and print out).

Before seeing the program, you have to learn about what is the meaning of the following commands.

cp- copy- it is used to copying contents from one file to another file.

Algorithm:

cp Source Destination

ls-list- it’s used to list all file names in the directory.

Syntax:

ls

grep-The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that
pattern.

Import the necessary libraries for reading the contents of a directory, such as dirent.h and stdio.h.

Create a main function and declare a pointer to a DIR structure, which will be used to access the
contents of the directory.

Use the opendir function to open the current directory (represented by ".").

Check if the directory was successfully opened. If not, return an error.

Use the readdir function in a loop to read the contents of the directory one by one.

Print the name of each directory entry as it is read.

Name: Rishikesh Bhardwaj UID: 19BCS1718


Course Name: Real Time System Lab Course Code: CSP-457
Use the closedir function to close the directory when you are done reading its contents.

Return 0 to indicate that the program has completed successfully.

Syntax:

grep [options] pattern [files]

Options Description

-c : This prints only a count of the lines that match a pattern

-h : Display the matched lines, but do not display the filenames.

-i : Ignores, case for matching

-l : Displays list of a filenames only.

-n : Display the matched lines and their line numbers.

Import the necessary libraries for reading a file and processing its contents, such as stdio.h, string.h, and
stdlib.h.

Create a main function and declare a variable to hold each line of the file as it is read, as well as a
variable to hold the search string passed as a command-line argument.

Check if the required number of command-line arguments have been provided (at least two: the search
string and the name of the file). If not, print an error message and return an error code.

Use the fopen function to open the file with the specified name.

Check if the file was successfully opened. If not, print an error message and return an error code.

Use the fgets function in a loop to read the contents of the file one line at a time.

Use the strstr function to search each line for the search string. If a match is found, print the line
number, the name of the file, and the line itself.

Use the fclose function to close the file when you are done reading its contents.

Return 0 to indicate that the program has completed successfully.

Implementation:
#include<stdio.h>
#include<stdlib.h>

Name: Rishikesh Bhardwaj UID: 19BCS1718


Course Name: Real Time System Lab Course Code: CSP-457
#include<dirent.h>
#define DATA_SIZE 1000
void createf()
{ char data[DATA_SIZE];
char n[100];
FILE * fPtr;
int i;
printf("create 2 files \nfile1: with data \nfile2: without data for copying\n");
for ( i=0;i<2;i++){
printf("enter a file name:");
gets(n);
fPtr = fopen(n,"w");
if(fPtr == NULL)
{ printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
fputs(data, fPtr);
fclose(fPtr);
printf("File created and saved successfully. ?? \n");
}
}
void copyfun(){
char ch, source_file[20], target_file[20];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if (source == NULL)
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}

Name: Rishikesh Bhardwaj UID: 19BCS1718


Course Name: Real Time System Lab Course Code: CSP-457
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if (target == NULL)
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, target);

printf("File copied successfully.\n");


fclose(source);
fclose(target);
}
void lsandgrep(){
char fn[10],pat[10],temp[200];
FILE *fp;
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(0);
}
while(d=readdir(p))
printf("%s\n",d->d_name);

Name: Rishikesh Bhardwaj UID: 19BCS1718


Course Name: Real Time System Lab Course Code: CSP-457
}

int main(){
createf();
copyfun();
lsandgrep();
}

Output:

Name: Rishikesh Bhardwaj UID: 19BCS1718

You might also like