Write a program to implement the toy 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. 
count c filename : To print number of characters in the file. 
count w filename : To print number of words in the file. 
count l filename : To print number of lines in the file. #include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/wait.h>


#define MAX_INPUT_SIZE 1024

#define MAX_ARG_SIZE 100


void count_file(const char *option, const char *filename);

void execute_command(char *command);


int main() {

    char input[MAX_INPUT_SIZE];


    while (1) {

        // Display the prompt

        printf("myshell$ ");

        fflush(stdout);


        // Read the command from the user

        if (fgets(input, sizeof(input), stdin) == NULL) {

            perror("fgets");

            exit(EXIT_FAILURE);

        }


        // Remove newline character from the input

        input[strcspn(input, "\n")] = 0;


        // Tokenize the command

        char *args[MAX_ARG_SIZE];

        char *token = strtok(input, " ");

        int i = 0;

        while (token != NULL && i < MAX_ARG_SIZE - 1) {

            args[i++] = token;

            token = strtok(NULL, " ");

        }

        args[i] = NULL; // Null-terminate the array of arguments


        if (i == 0) continue; // No command entered


        // Special handling for the 'count' command

        if (strcmp(args[0], "count") == 0 && i == 3) {

            count_file(args[1], args[2]);

            continue;

        }


        // Fork and execute the command

        pid_t pid = fork();

        if (pid == -1) {

            perror("fork");

            exit(EXIT_FAILURE);

        } else if (pid == 0) {

            execvp(args[0], args);

            perror("execvp"); // execvp returns only on error

            exit(EXIT_FAILURE);

        } else {

            // In the parent process

            wait(NULL); // Wait for the child process to complete

        }

    }


    return 0;

}


void count_file(const char *option, const char *filename) {

    FILE *file = fopen(filename, "r");

    if (file == NULL) {

        perror("fopen");

        return;

    }


    int characters = 0;

    int words = 0;

    int lines = 0;

    int in_word = 0;

    int ch;


    while ((ch = fgetc(file)) != EOF) {

        characters++;

        if (ch == ' ' || ch == '\t' || ch == '\n') {

            if (in_word) {

                words++;

                in_word = 0;

            }

            if (ch == '\n') {

                lines++;

            }

        } else {

            in_word = 1;

        }

    }

    if (in_word) {

        words++;

    }

    if (ch == EOF && characters > 0 && ch != '\n') {

        lines++;

    }


    fclose(file);


    if (strcmp(option, "c") == 0) {

        printf("Number of characters: %d\n", characters);

    } else if (strcmp(option, "w") == 0) {

        printf("Number of words: %d\n", words);

    } else if (strcmp(option, "l") == 0) {

        printf("Number of lines: %d\n", lines);

    } else {

        fprintf(stderr, "Invalid option. Use 'c' for characters, 'w' for words, or 'l' for lines.\n");

    }

}