( 1.
Implement the shell program that accepts the command at $ prompt displayed by your shell
(myshell$). Implement the „count‟ command by creating child process which works as follows:
myshell$ count c filename: To display the number of characters in given file myshell$ count w
filename: To display the number of words in given file myshell$ count l filename: To display the
number of lines in given file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
void count_file(char mode, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("File '%s' not found.\n", filename);
exit(1);
int ch, char_count = 0, word_count = 0, line_count = 0;
int in_word = 0;
while ((ch = fgetc(fp)) != EOF) {
char_count++;
if (ch == '\n') {
line_count++;
}
if (ch == ' ' || ch == '\n' || ch == '\t') {
in_word = 0;
} else if (!in_word) {
word_count++;
in_word = 1;
fclose(fp);
if (mode == 'c')
printf("Characters: %d\n", char_count);
else if (mode == 'w')
printf("Words: %d\n", word_count);
else if (mode == 'l')
printf("Lines: %d\n", line_count);
else
printf("Invalid mode. Use c (chars), w (words), or l (lines).\n");
int main() {
char input[100];
while (1) {
printf("myshell$ ");
fgets(input, sizeof(input), stdin);
// Remove trailing newline
input[strcspn(input, "\n")] = 0;
// Exit condition
if (strcmp(input, "exit") == 0)
break;
char *cmd = strtok(input, " ");
char *arg1 = strtok(NULL, " ");
char *arg2 = strtok(NULL, " ");
if (cmd && strcmp(cmd, "count") == 0 && arg1 && arg2) {
pid_t pid = fork();
if (pid == 0) {
// Child process
count_file(arg1[0], arg2);
exit(0);
} else if (pid > 0) {
// Parent waits
wait(NULL);
} else {
printf("Fork failed.\n");
}
} else {
printf("Invalid command. Use: count [c|w|l] filename\n");
return 0;
2. Extend the shell to implement the commands „list‟ which works as follows: myshell$ list f
dirname: It will display filenames in a given directory. myshell$ list n dirname: It will count the
number of entries in a given directory. myshell$ list i dirname: It will display filenames and their
inode number for the files in a given directory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
void count_file(char mode, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("File '%s' not found.\n", filename);
exit(1);
int ch, char_count = 0, word_count = 0, line_count = 0;
int in_word = 0;
while ((ch = fgetc(fp)) != EOF) {
char_count++;
if (ch == '\n') line_count++;
if (ch == ' ' || ch == '\n' || ch == '\t') {
in_word = 0;
} else if (!in_word) {
word_count++;
in_word = 1;
fclose(fp);
if (mode == 'c')
printf("Characters: %d\n", char_count);
else if (mode == 'w')
printf("Words: %d\n", word_count);
else if (mode == 'l')
printf("Lines: %d\n", line_count);
else
printf("Invalid mode. Use c (chars), w (words), or l (lines).\n");
}
void list_dir(char mode, char *dirname) {
DIR *dir = opendir(dirname);
if (dir == NULL) {
printf("Directory '%s' not found.\n", dirname);
exit(1);
struct dirent *entry;
int count = 0;
while ((entry = readdir(dir)) != NULL) {
if (mode == 'f') {
printf("%s\n", entry->d_name);
} else if (mode == 'i') {
printf("%s \t Inode: %lu\n", entry->d_name, entry->d_ino);
count++;
if (mode == 'n') {
printf("Total entries: %d\n", count);
closedir(dir);
}
int main() {
char input[100];
while (1) {
printf("myshell$ ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0; // Remove newline
if (strcmp(input, "exit") == 0)
break;
char *cmd = strtok(input, " ");
char *arg1 = strtok(NULL, " ");
char *arg2 = strtok(NULL, " ");
if (cmd && strcmp(cmd, "count") == 0 && arg1 && arg2) {
pid_t pid = fork();
if (pid == 0) {
count_file(arg1[0], arg2);
exit(0);
} else if (pid > 0) {
wait(NULL);
} else {
printf("Fork failed.\n");
}
}
else if (cmd && strcmp(cmd, "list") == 0 && arg1 && arg2) {
pid_t pid = fork();
if (pid == 0) {
list_dir(arg1[0], arg2);
exit(0);
} else if (pid > 0) {
wait(NULL);
} else {
printf("Fork failed.\n");
else {
printf("Invalid command. Try:\n");
printf(" count [c|w|l] filename\n");
printf(" list [f|n|i] dirname\n");
return 0;