commiting_from_my_own_personal_shell
This commit is contained in:
parent
3daf9ab37d
commit
ac6dbe8cd7
5 changed files with 210 additions and 0 deletions
92
smallsh/main.c
Normal file
92
smallsh/main.c
Normal file
|
@ -0,0 +1,92 @@
|
|||
#include "main.h"
|
||||
|
||||
int main() {
|
||||
|
||||
int status = 0;
|
||||
size_t input_size = 2048;
|
||||
size_t getline_len;
|
||||
char* input = malloc(sizeof(char) * input_size);
|
||||
|
||||
while(1) {
|
||||
printf(": ");
|
||||
fflush(stdout);
|
||||
getline_len = getline(&input, &input_size, stdin);
|
||||
input[getline_len - 1] = '\0';
|
||||
|
||||
// Ignore whitespace or comments
|
||||
if(input == NULL ||
|
||||
input[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
char*** array = malloc(sizeof(char**));
|
||||
int num_strings = input_format(input, input_size, array);
|
||||
|
||||
char* first_array = (*array)[0];
|
||||
|
||||
// Built in shell commands
|
||||
if(!strncmp(first_array, "exit", 4))
|
||||
return 0;
|
||||
else if(!strncmp(first_array, "cd", 2))
|
||||
inbuilt_cd((*array)[1]);
|
||||
else if (!strncmp(first_array, "status", 6))
|
||||
printf("%d\n", status);
|
||||
else
|
||||
status = run_command(array, num_strings);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int input_format(char* input, size_t input_size, char*** array) {
|
||||
|
||||
const int max_args = 512;
|
||||
int num_strings = 0;
|
||||
char* pch;
|
||||
*array = malloc(sizeof(char*) * max_args);
|
||||
|
||||
pch = strtok (input," ");
|
||||
while (pch != NULL)
|
||||
{
|
||||
(*array)[num_strings] = malloc(sizeof(char*) * (strlen(pch) + 1));
|
||||
strcpy((*array)[num_strings], pch);
|
||||
num_strings++;
|
||||
|
||||
pch = strtok (NULL, " ");
|
||||
}
|
||||
|
||||
return num_strings;
|
||||
}
|
||||
|
||||
void inbuilt_cd(char* args) {
|
||||
|
||||
if(args)
|
||||
chdir(args);
|
||||
else
|
||||
chdir(getenv("HOME"));
|
||||
|
||||
}
|
||||
|
||||
int run_command(char*** array, int num_array) {
|
||||
|
||||
char* comb_string = malloc(sizeof(char) * 100);
|
||||
sprintf(comb_string, "/usr/bin/");
|
||||
strcat(comb_string, (*array)[0]);
|
||||
int f = fork();
|
||||
pid_t pid;
|
||||
if(!f)
|
||||
pid = execv(comb_string, (*array));
|
||||
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
|
||||
free(comb_string);
|
||||
|
||||
return status;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
15
smallsh/main.h
Normal file
15
smallsh/main.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
#ifndef MAIN
|
||||
#define MAIN
|
||||
|
||||
#include "stdlib.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "unistd.h"
|
||||
#include "sys/wait.h"
|
||||
|
||||
int input_format(char*, size_t, char***);
|
||||
void inbuilt_cd(char*);
|
||||
int run_command(char***, int);
|
||||
|
||||
#endif
|
17
smallsh/makefile
Normal file
17
smallsh/makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
CC=gcc --std=gnu99 -g
|
||||
output=smallsh
|
||||
|
||||
all: main.c main.h node.o input.o
|
||||
$(CC) main.c node.o input.o -o $(output)
|
||||
|
||||
node.o: node.c node.h
|
||||
$(CC) -c node.c -o node.o
|
||||
|
||||
input.o: input.h input.c
|
||||
$(CC) -c input.c -o input.o
|
||||
|
||||
test: all
|
||||
./p3testscript
|
||||
|
||||
clean:
|
||||
rm -fr *.o vgcore.* $(output)
|
62
smallsh/node.c
Normal file
62
smallsh/node.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
|
||||
#include "node.h"
|
||||
|
||||
void append_node(struct node* head, struct node* node_app) {
|
||||
|
||||
if(!head) {
|
||||
head = node_app;
|
||||
return;
|
||||
}
|
||||
|
||||
struct node* temp = head;
|
||||
|
||||
while(temp->node != NULL) {
|
||||
temp = temp->node;
|
||||
}
|
||||
temp->node = node_app;
|
||||
|
||||
}
|
||||
|
||||
struct node* appendv_node(struct node* head, void* data) {
|
||||
|
||||
struct node* node = malloc(sizeof(struct node));
|
||||
node->data = data;
|
||||
node->node = NULL;
|
||||
|
||||
append_node(head, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
int count_nodes(struct node* head) {
|
||||
|
||||
int num = 1;
|
||||
|
||||
if(!head)
|
||||
return -1;
|
||||
|
||||
struct node* temp = head;
|
||||
while(temp->node) {
|
||||
|
||||
temp = temp->node;
|
||||
num++;
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void iterate_nodes(struct node* head, void *func (void*, void*), void* val) {
|
||||
|
||||
if(!head)
|
||||
return;
|
||||
|
||||
func(head->data, val);
|
||||
|
||||
struct node* temp = head;
|
||||
|
||||
while(temp->node) {
|
||||
temp = temp->node;
|
||||
func(temp->data, val);
|
||||
}
|
||||
}
|
||||
|
24
smallsh/node.h
Normal file
24
smallsh/node.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "stdlib.h"
|
||||
|
||||
#ifndef NODES
|
||||
#define NODES
|
||||
|
||||
struct node {
|
||||
struct node* node;
|
||||
void* data;
|
||||
};
|
||||
|
||||
// Appends Node to list
|
||||
void append_node(struct node*, struct node*);
|
||||
|
||||
// Creates node with value and appends
|
||||
// Returns node that was created
|
||||
struct node* appendv_node(struct node*, void*);
|
||||
|
||||
// Counts the number of nodes in a list
|
||||
int count_nodes(struct node*);
|
||||
|
||||
// Iterates over every node in a list and runs the given function
|
||||
void iterate_nodes(struct node*, void *func(void*, void*), void*);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue