Write a C program that behaves like a shell which displays the command prompt ‘myshell$’.
It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘count’ as myshell$ count c filename: It will display the
number of characters in given file myshell$ count w filename: It will display the number of words
in given file myshell$ count l filename: It will display the number of lines in given file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_CMD_LEN 1024 //Maximum length of input command line (1024 characters)
#define MAX_ARGS 100 //Maximum number of arguments in a command (100)
// Function to count characters, words, or lines in a file
void count_file(const char *option, const char *filename) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("File opening failed");
return;
int ch, characters = 0, words = 0, lines = 0;
int in_word = 0; //used to detect transition between words
while ((ch = fgetc(file)) != EOF) //Reads one character at a time from the file until End Of File (EOF)
characters++;
if (ch == '\n') lines++;
if (ch == ' ' || ch == '\n' || ch == '\t') {
in_word = 0;
} else if (!in_word) {
in_word = 1;
words++;
fclose(file);
if (strcmp(option, "c") == 0) {
printf("Characters: %d\n", characters);
} else if (strcmp(option, "w") == 0) {
printf("Words: %d\n", words);
} else if (strcmp(option, "l") == 0) {
printf("Lines: %d\n", lines);
} else {
printf("Invalid count option. Use c, w, or l.\n");
int main() {
char input[MAX_CMD_LEN]; //to store the full command typed by the user
char *args[MAX_ARGS]; //array to store command and its arguments (tokens)
while (1) {
printf("myshell$ ");
if (fgets(input, sizeof(input), stdin) == NULL) //Read the user input line using fgets()
break; //If user presses Ctrl+D or error occurs, break the loop (exit shell)
// Remove newline character from input
input[strcspn(input, "\n")] = '\0';
// Tokenize the input
int arg_count = 0;
char *token = strtok(input, " ");
while (token != NULL && arg_count < MAX_ARGS - 1) {
args[arg_count++] = token;
token = strtok(NULL, " ");
args[arg_count] = NULL; // Null-terminate the arguments
if (arg_count == 0) continue; // If user just presses Enter (no command), skip the loop
// Handle 'exit' command
if (strcmp(args[0], "exit") == 0) //If user types exit, break the loop and quit the shell
break;
}
// Handle custom 'count' command
if (strcmp(args[0], "count") == 0) {
if (arg_count != 3) {
printf("Usage: count [c|w|l] filename\n");
} else {
count_file(args[1], args[2]);
continue;
// Fork and execute other commands
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
} else if (pid == 0) {
execvp(args[0], args);
perror("Execution failed");
exit(1);
} else {
wait(NULL); // Parent waits for child
return 0;
}
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘list’ as 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/wait.h>
void list_command(char *opt, char *dir_name) {
DIR *dir = opendir(dir_name);
if (!dir) {
perror("Cannot open directory");
return;
struct dirent *entry; //used to read each file in the directory
int count = 0; //to store number of files (used for list n)
while ((entry = readdir(dir)) != NULL) //Read files one by one using readdir()
if (entry->d_name[0] == '.') continue; // skip hidden files
// If user chose list f, print filenames
if (strcmp(opt, "f") == 0)
printf("%s\n", entry->d_name);
// If option is i, print filename with its inode number
else if (strcmp(opt, "i") == 0)
printf("%s - inode: %lu\n", entry->d_name, entry->d_ino);
//If option is n, just increment the counter for each file
else if (strcmp(opt, "n") == 0)
count++;
// After loop if option was n, display total count
if (strcmp(opt, "n") == 0)
printf("Number of entries: %d\n", count);
closedir(dir);
int main() {
char input[100], *args[10];
while (1) {
printf("myshell$ ");
//Read input from user
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = 0; // remove newline
// Tokenize input
int i = 0;
args[i] = strtok(input, " ");
while (args[i] != NULL)
args[++i] = strtok(NULL, " ");
// If user hits only Enter, skip and show prompt again
if (args[0] == NULL) continue;
//If user types exit, break the loop and terminate shell
if (strcmp(args[0], "exit") == 0)
break;
// If first word is list and has valid arguments: Call list_command() with option and
directory name
if (strcmp(args[0], "list") == 0 && args[1] && args[2]) {
list_command(args[1], args[2]);
continue;
// Execute other system commands
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("Command failed");
exit(1);
} else {
wait(NULL);
return 0;
}
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘typeline’ as myshell$ typeline n filename: It will display
first n lines of the file. myshell$ typeline -n filename: It will display last n lines of the file. myshell$
typeline a filename: It will display all the lines of the file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_CMD_LEN 1024
#define MAX_ARGS 100
#define MAX_LINE_LEN 1024
// Function to display first n lines of a file
void display_first_n_lines(int n, const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("File open failed");
return;
char line[MAX_LINE_LEN];
int count = 0;
while (fgets(line, sizeof(line), fp) != NULL && count < n) {
printf("%s", line);
count++;
fclose(fp);
// Function to display last n lines of a file
void display_last_n_lines(int n, const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("File open failed");
return;
char **lines = malloc(n * sizeof(char *));
for (int i = 0; i < n; i++) {
lines[i] = malloc(MAX_LINE_LEN);
lines[i][0] = '\0';
int index = 0, total = 0;
while (fgets(lines[index], MAX_LINE_LEN, fp) != NULL) {
index = (index + 1) % n;
total++;
}
int start = total >= n ? index : 0;
int count = total >= n ? n : total;
for (int i = 0; i < count; i++) {
printf("%s", lines[(start + i) % n]);
for (int i = 0; i < n; i++) {
free(lines[i]);
free(lines);
fclose(fp);
// Function to display all lines of a file
void display_all_lines(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("File open failed");
return;
char line[MAX_LINE_LEN];
while (fgets(line, sizeof(line), fp) != NULL) {
printf("%s", line);
}
fclose(fp);
// Function to handle the typeline command
void handle_typeline(char *args[]) {
if (args[1] == NULL || args[2] == NULL) {
fprintf(stderr, "Usage: typeline [n/-n/a] filename\n");
return;
if (strcmp(args[1], "a") == 0) {
display_all_lines(args[2]);
} else if (args[1][0] == '-') {
int n = atoi(args[1] + 1);
display_last_n_lines(n, args[2]);
} else {
int n = atoi(args[1]);
display_first_n_lines(n, args[2]);
int main() {
char cmdline[MAX_CMD_LEN];
char *args[MAX_ARGS];
while (1) {
printf("myshell$ ");
fflush(stdout);
if (fgets(cmdline, sizeof(cmdline), stdin) == NULL) {
break; // Ctrl+D (EOF)
// Remove trailing newline
cmdline[strcspn(cmdline, "\n")] = '\0';
// Tokenize input
int argc = 0;
char *token = strtok(cmdline, " ");
while (token != NULL && argc < MAX_ARGS - 1) {
args[argc++] = token;
token = strtok(NULL, " ");
args[argc] = NULL;
if (argc == 0) continue;
// Exit shell
if (strcmp(args[0], "exit") == 0) {
break;
}
// Handle typeline
if (strcmp(args[0], "typeline") == 0) {
handle_typeline(args);
continue;
// Handle regular commands
pid_t pid = fork();
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) {
perror("Command execution failed");
exit(EXIT_FAILURE);
} else if (pid > 0) {
// Parent process
wait(NULL);
} else {
perror("Fork failed");
return 0;
Output: myshell$ typeline 2 file.txt //print first 2 line in the file
myshell$ typeline -2 file.txt //print last 2 line in the file
myshell$ typeline a file.txt //print all file
Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘search’ as myshell$ search f filename pattern : It will
search the first occurrence of pattern in the given file myshell$ search a filename pattern : It will
search all the occurrence of pattern in the given file myshell$ search c filename pattern : It will
count the number of occurrence of pattern in the given file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_CMD_LEN 1024
#define MAX_ARGS 100
#define MAX_LINE_LEN 1024
// Function to search for the first occurrence
void search_first_occurrence(const char *filename, const char *pattern) {
FILE *fp = fopen(filename, "r");
if (!fp) {
perror("File open failed");
return;
}
char line[MAX_LINE_LEN];
int found = 0, lineno = 1;
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, pattern)) {
printf("Line %d: %s", lineno, line);
found = 1;
break;
lineno++;
if (!found) {
printf("Pattern not found.\n");
fclose(fp);
// Function to search all occurrences
void search_all_occurrences(const char *filename, const char *pattern) {
FILE *fp = fopen(filename, "r");
if (!fp) {
perror("File open failed");
return;
}
char line[MAX_LINE_LEN];
int found = 0, lineno = 1;
while (fgets(line, sizeof(line), fp)) {
if (strstr(line, pattern)) {
printf("Line %d: %s", lineno, line);
found = 1;
lineno++;
if (!found) {
printf("Pattern not found.\n");
fclose(fp);
// Function to count occurrences
void search_count_occurrences(const char *filename, const char *pattern) {
FILE *fp = fopen(filename, "r");
if (!fp) {
perror("File open failed");
return;
char line[MAX_LINE_LEN];
int count = 0;
while (fgets(line, sizeof(line), fp)) {
char *ptr = line;
while ((ptr = strstr(ptr, pattern)) != NULL) {
count++;
ptr += strlen(pattern); // Move forward
printf("Total occurrences: %d\n", count);
fclose(fp);
// Handle search command
void handle_search(char *args[]) {
if (!args[1] || !args[2] || !args[3]) {
fprintf(stderr, "Usage: search [f|a|c] filename pattern\n");
return;
char mode = args[1][0];
const char *filename = args[2];
const char *pattern = args[3];
switch (mode) {
case 'f':
search_first_occurrence(filename, pattern);
break;
case 'a':
search_all_occurrences(filename, pattern);
break;
case 'c':
search_count_occurrences(filename, pattern);
break;
default:
fprintf(stderr, "Invalid option. Use f, a, or c.\n");
int main() {
char cmdline[MAX_CMD_LEN];
char *args[MAX_ARGS];
while (1) {
printf("myshell$ ");
fflush(stdout);
if (!fgets(cmdline, sizeof(cmdline), stdin)) break;
// Remove newline
cmdline[strcspn(cmdline, "\n")] = '\0';
// Tokenize input
int argc = 0;
char *token = strtok(cmdline, " ");
while (token && argc < MAX_ARGS - 1) {
args[argc++] = token;
token = strtok(NULL, " ");
args[argc] = NULL;
if (argc == 0) continue;
// Exit
if (strcmp(args[0], "exit") == 0) break;
// Handle search
if (strcmp(args[0], "search") == 0) {
handle_search(args);
continue;
// Execute normal shell command
pid_t pid = fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("Execution failed");
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
} else {
perror("Fork failed");
return 0;
Output: search f file.txt hello //print first occurence
search a file.txt hello //print all occurence
search c file.txt hello //print count