A simple shell program for a UNIX-like interface
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
main.c

README.md

Program: A simple shell

Problem:

Get user input, parse input, and look up command(input) using system interface.

Approach:

The struct -
struct command_t{ char * name; int argc; char *[MAX_ARGS]; }

a). Parse PATH variable Before prompting a user for input, the first thing the shell must do is get and parse all the paths defined in the enviroment variable, PATH. i). int parsePath(char **) - takes an array of strings and saves the parsed paths individually.

b). Prompt the user i). void printPrompt(char *) - prints the shell's prompt. ii). int readCommand(char *) - reads input from user using stdin, returns 1 if input is valid, 0 otherwise. iii). void removeNewLineChar() - removes the newline char from valid user input.

c). Parse user input i). int parseCommand(char *, struct command_t *) - parses user input using strtok(char *, char *), then saves the command details in a struct. The first word from the input is taken as the command and the preceding words are saved as the command arguments, a count of arguments is saved too.

d). Look up path i). char *lookupPath(char **, char **) - Using the command name from the struct and the paths saved from parsePath(char **) function, this function first concatanates the command name to all paths, then it searches if the command file exists in any of those paths. If the command is an absolute path, it simply return that string, else search, if the file is found it returns the string of that path, otherwise it returns NULL.

e). Execute command i). If the command file was found, a child process is created using the system call function fork(). Then it uses execv(char *, char *[]) to execute the command as a child process. It uses the struct variables to pass the proper parameters, command.name, and command.argv.

d). Repeat/Exit i). After the execution of the child process, the user can type a new command, if the command is "exit" the shell frees memory used and terminates.