Prototype:
Main File: main.c
Will contain the main function and the PSH_READ function.
// main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "psh.h"
#include "builtin.h"
Header File: psh.h
Contains the declarations for the functions and variables used across multiple files.
// psh.h
#ifndef PSH_H
#define PSH_H
#include <linux/limits.h>
#include <stdlib.h>
#include <unistd.h>
// Global Variables
extern char path_history[MAX_HISTORY][PATH_MAX];
extern int history_index;
extern char cwd[1024];
extern char *builtin_str[];
extern int (*builtin_func[])(char **);
extern int size_builtin_str;
// Function Declarations
int PSH_READ();
char **PSH_TOKENIZER(char *);
int PSH_EXEC_EXTERNAL(char **);
void remove_last_component(char *);
int resolve_and_manage_symlink(char *, char *);
char *commonSuffix(char *, char *);
char *helper_cd_func1(const char *, const char *);
#endif
Header File for Built-in Commands: builtin.h
This file will contain the declarations for the built-in commands.
// builtin.h
#ifndef BUILTIN_H
#define BUILTIN_H
int PSH_EXIT(char **);
int PSH_CD(char **);
int PSH_ECHO(char **);
int PSH_PWD(char **);
int PSH_HISTORY(char **);
#endif
builtin.c
char path_history[MAX_HISTORY][PATH_MAX];
int history_index = 0;
char cwd[1024];
char *builtin_str[] = {"exit", "cd", "echo", "pwd", "history"};
int (*builtin_func[])(char **) = {&PSH_EXIT, &PSH_CD, &PSH_ECHO, &PSH_PWD, &PSH_HISTORY};
int size_builtin_str = sizeof(builtin_str) / sizeof(builtin_str[0]);
int PSH_EXIT(char **token_arr)
{
...
}
Execute.c tokenizer and execution of external commands
#include <unistd.h>
#include "psh.h"
char **PSH_TOKENIZER(char *line)
{...}
helpers.c
// helpers.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "psh.h"
void remove_last_component(char *path)
{
...
}
int resolve_and_manage_symlink(char *localdir, char *resolved_path)
{
...
}
Sample makefile (update with our file structure)
# Makefile
CC = gcc
CFLAGS = -Wall -Wextra -g
OBJ = main.o execute.o builtin.o helpers.o
psh: $(OBJ)
$(CC) $(CFLAGS) -o psh $(OBJ)
main.o: main.c psh.h builtin.h
$(CC) $(CFLAGS) -c main.c
execute.o: execute.c psh.h
$(CC) $(CFLAGS) -c execute.c
builtin.o: builtin.c psh.h builtin.h
$(CC) $(CFLAGS) -c builtin.c
helpers.o: helpers.c psh.h
$(CC) $(CFLAGS) -c helpers.c
clean:
rm -f *.o psh