Browse Source

support cd

master
alistair 5 years ago
parent
commit
326e62862c
  1. 0
      builtins.c
  2. 0
      builtins.h
  3. 40
      main.c
  4. 18
      run.c
  5. 9
      run.h

0
builtins.c

0
builtins.h

40
main.c

@ -1,14 +1,19 @@ @@ -1,14 +1,19 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include "error.h"
#include "run.h"
#define INITIALLINEBUFFER 80
#define INITIAL_LINE_BUFFER 80
#define INITIAL_WORD_SIZE 80
#define NUM_BUILTINS 2
int readline(FILE* in, char **out) {
int size_of_buffer = INITIALLINEBUFFER;
int size_of_buffer = INITIAL_LINE_BUFFER;
char *buffer = calloc(size_of_buffer, sizeof(char));
char character;
int count = 0;
@ -82,6 +87,28 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { @@ -82,6 +87,28 @@ int split_string(char **sep_string[], int *num, char *string, char delim) {
return ER_FAILURE;
}
int check_builtins(char *args[]) {
char *builtins[NUM_BUILTINS] = {
"cd",
};
if (!strcmp(args[0], "cd")) {
if (args[1] == 0) {
chdir(getenv("HOME"));
return ER_SUCCESS;
}
if (args[2] != 0) {
return ER_FAILURE;
}
chdir(args[1]);
}
return ER_FAILURE;
}
int repl(void) {
while (true) {
char *line;
@ -94,7 +121,13 @@ int repl(void) { @@ -94,7 +121,13 @@ int repl(void) {
int num_words;
split_string(&sep_string, &num_words, line, ' ');
execute(stdin, stdout, sep_string);
pid_t child;
if (check_builtins(sep_string)) {
execute(stdin, stdout, sep_string, &child);
}
waitpid(child, NULL, 0);
for (int i = 0; i < num_words; i++) {
free(sep_string[i]);
@ -104,7 +137,6 @@ int repl(void) { @@ -104,7 +137,6 @@ int repl(void) {
}
}
int main(int argc, char** argv) {
char *chicken[] = {

18
run.c

@ -1,11 +1,18 @@ @@ -1,11 +1,18 @@
#include "run.h"
/* Contains builtin functions and code for executing system programs */
int change_dir(char *dir) {
return chdir(dir);
}
/* Fork and exec a command given in args taking input from in and sending
* output to out. The arguments array must end in a NULL.
*
* Returns true on success or 1 on failure.
*/
int execute(FILE *in, FILE *out, char *args[]) {
int execute(FILE *in, FILE *out, char *args[], pid_t* child_ID) {
int in_FD = fileno(in);
int out_FD = fileno(out);
@ -16,6 +23,7 @@ int execute(FILE *in, FILE *out, char *args[]) { @@ -16,6 +23,7 @@ int execute(FILE *in, FILE *out, char *args[]) {
}
if (err) { // parent
*child_ID = err;
return ER_SUCCESS;
} else { // child
dup2(in_FD, 0);
@ -27,11 +35,3 @@ int execute(FILE *in, FILE *out, char *args[]) { @@ -27,11 +35,3 @@ int execute(FILE *in, FILE *out, char *args[]) {
return ER_SUCCESS;
}
int test_exec(void) {
char *arg[] = {"ls", "lah", 0};
execute(stdin, stdout, arg);
return 0;
}

9
run.h

@ -8,11 +8,8 @@ @@ -8,11 +8,8 @@
#ifndef RUNH
#define RUNH
/* Fork and exec a command given in args taking input from in and sending
* output to out. The arguments array must end in a NULL.
*
* Returns true on success or 1 on failure.
*/
int execute(FILE* in, FILE* out, char *args[]);
int execute(FILE *in, FILE *out, char *args[], pid_t* child_ID);
int change_dir(char *dir);
#endif

Loading…
Cancel
Save