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 ‘list’ commands as 
myshell$ list f dirname : To print names of all the files in current directory.
myshell$ list i dirname : To print the number of all entries in the current directory.


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

// Function to print names of all files in the specified directory
void list_files(const char *dirname) {
    DIR *dir = opendir(dirname);
    if (dir == NULL) {
        perror("Error opening directory");
        return;
    }

    struct dirent *entry;
    printf("Files in directory \"%s\":\n", dirname);
    while ((entry = readdir(dir)) != NULL) {
        // Print only files (not directories)
        if (entry->d_type == DT_REG) {
            printf("%s\n", entry->d_name);
        }
    }
    closedir(dir);
}

// Function to print the number of all entries in the specified directory
void list_entries_count(const char *dirname) {
    DIR *dir = opendir(dirname);
    if (dir == NULL) {
        perror("Error opening directory");
        return;
    }

    struct dirent *entry;
    int count = 0;
    while ((entry = readdir(dir)) != NULL) {
        count++;
    }

    printf("Number of entries in directory \"%s\": %d\n", dirname, count);
    closedir(dir);
}

// Function to execute a command in a child process
void execute_command(char **tokens) {
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork failed");
        exit(EXIT_FAILURE);
    } else if (pid == 0) {
        execvp(tokens[0], tokens);
        perror("execvp failed");
        exit(EXIT_FAILURE);
    } else {
        int status;
        waitpid(pid, &status, 0);
    }
}

int main() {
    char input[1024];

    while (1) {
        printf("myshell$ ");
        if (fgets(input, sizeof(input), stdin) == NULL) {
            perror("fgets failed");
            exit(EXIT_FAILURE);
        }

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

        // Tokenize the input
        char *tokens[100];
        int token_count = 0;
        char *token = strtok(input, " ");
        while (token != NULL && token_count < 99) {
            tokens[token_count++] = token;
            token = strtok(NULL, " ");
        }
        tokens[token_count] = NULL;

        // Check if it's a list command
        if (token_count >= 3 && strcmp(tokens[0], "list") == 0) {
            if (strcmp(tokens[1], "f") == 0 && token_count == 3) {
                // list f dirname
                list_files(tokens[2]);
            } else if (strcmp(tokens[1], "i") == 0 && token_count == 3) {
                // list i dirname
                list_entries_count(tokens[2]);
            } else {
                printf("Usage: list [f|i] dirname\n");
            }
        } else {
            // Execute other commands
            execute_command(tokens);
        }
    }

    return 0;
}