You are on page 1of 4

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.
TOOLS: Dev C++
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:

 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.
 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.

Name: Sahil Saurabh UID:19BCS2855


CODE:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

void ls(const string& path) {


DIR* dir;
struct dirent* ent;
if ((dir = opendir(path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
cout << ent->d_name << endl;
}
closedir(dir);
} else {
cout << "Unable to open directory: " << path << endl;
}
}

void cat(const string& filename) {


ifstream file(filename);
if (file.is_open()) {
string line;
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file: " << filename << endl;
}
}

void echo(const string& message) {


cout << message << endl;

Name: Sahil Saurabh UID:19BCS2855


}

void pwd() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
cout << cwd << endl;
} else {
cout << "Unable to get current working directory" << endl;
}
}

void tokenize(const string& input, vector<string>& tokens) {


stringstream ss(input);
string token;
while (ss >> token) {
tokens.push_back(token);
}
}

int main() {
while (true) {
cout << "> ";
string input;
getline(cin, input);
vector<string> tokens;
tokenize(input, tokens);
if (tokens.empty()) {
continue;
}
if (tokens[0] == "ls") {
string path = ".";
if (tokens.size() > 1) {
path = tokens[1];
}
ls(path);
} else if (tokens[0] == "cat") {
if (tokens.size() > 1) {
cat(tokens[1]);
} else {
cout << "Usage: cat <filename>" << endl;

Name: Sahil Saurabh UID:19BCS2855


}
} else if (tokens[0] == "echo") {
if (tokens.size() > 1) {
string message = input.substr(5);
echo(message);
} else {
cout << "Usage: echo <message>" << endl;
}
} else if (tokens[0] == "pwd") {
pwd();
} else if (tokens[0] == "exit") {
break;
} else {
cout << "Unknown command: " << tokens[0] << endl;
}
}
return 0;
}

OUTPUT:

Name: Sahil Saurabh UID:19BCS2855

You might also like