Write a program to implement the shell. It should display the command prompt "myshell$" Tokenize the command line and execute the given command by creating the child process. Additionally it should interpret the following commands.
myshell$ search f  filename pattern: To display first occurrence of pattern in the file.
myshell$ search c filename pattern: To count the number of occurrence of pattern in the file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

// Function to search for the first occurrence of pattern in the file
void search_f(const char *filename, const char *pattern) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("fopen");
        return;
    }

    char line[1024];
    while (fgets(line, sizeof(line), file)) {
        if (strstr(line, pattern)) {
            printf("First occurrence: %s", line);
            fclose(file);
            return;
        }
    }

    printf("Pattern not found\n");
    fclose(file);
}

// Function to count the number of occurrences of pattern in the file
void search_c(const char *filename, const char *pattern) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("fopen");
        return;
    }

    char line[1024];
    int count = 0;
    while (fgets(line, sizeof(line), file)) {
        char *ptr = line;
        while ((ptr = strstr(ptr, pattern)) != NULL) {
            count++;
            ptr += strlen(pattern);
        }
    }

    printf("Number of occurrences: %d\n", count);
    fclose(file);
}

int main() {
    char input[1024];

    while (1) {
        // Display the command prompt
        printf("myshell$ ");
        fflush(stdout);

        // Read the input command
        if (!fgets(input, sizeof(input), stdin)) {
            perror("fgets");
            exit(EXIT_FAILURE);
        }

        // Remove newline character from input
        input[strcspn(input, "\n")] = '\0';

        // Tokenize the command line
        char *token = strtok(input, " ");
        if (!token) continue; // No command entered

        // Check for 'search' command
        if (strcmp(token, "search") == 0) {
            token = strtok(NULL, " ");
            if (!token) continue; // Missing option

            char *option = token;
            token = strtok(NULL, " ");
            if (!token) continue; // Missing filename

            char *filename = token;
            token = strtok(NULL, " ");
            if (!token) continue; // Missing pattern

            char *pattern = token;

            if (strcmp(option, "f") == 0) {
                search_f(filename, pattern);
            } else if (strcmp(option, "c") == 0) {
                search_c(filename, pattern);
            } else {
                printf("Invalid option. Use 'f' for first occurrence or 'c' for count.\n");
            }
        } else {
            printf("Invalid command. Use 'search' followed by option and arguments.\n");
        }
    }

    return 0;
}