File size: 2,750 Bytes
4559903
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "shell.h"

#include <errno.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>

static char **split(char *line, int *arg_count, char *delim) {
  char *copy = malloc(strlen(line));

  strcpy(copy, line);

  char **words = NULL;
  char *word = strtok(copy, delim);
  size_t counter = 0;

  while(word != NULL) {
    words = (char**)realloc(words, (counter+1)*sizeof(char*));
    words[counter] = (char*)malloc(strlen(word)+1);
    strcpy(words[counter], word);
    counter++;
    word = strtok(NULL, delim);
  }

  words = (char**)realloc(words, (counter+1)*sizeof(char*));
  words[counter] = NULL;
  *arg_count = counter;
  free(copy);
  return words;
}

static bool dir_exists(char path[]) {
    struct stat sb;
    bool exists = false;

    if(stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
        exists = true;
    }

    return exists;
}

static bool run_builtin(char **args, int arg_count) {
  bool is_builtin_command = false;

  if(strcmp(args[0], "cd") == 0) {
    if((arg_count > 1 && strcmp(args[1], "~") == 0) || arg_count == 1) {
      chdir(getenv("HOME"));
    }

    else {
      if(dir_exists(args[1])) {
        chdir(args[1]);
      }

      else {
        fprintf(stderr, "dtsh: %s: directory does not exist\n", args[1]);
      }
    }

    is_builtin_command = true;
  }

  else if(strcmp(args[0], "exit") == 0) {
    exit(1);
  }

  return is_builtin_command;
}

static char **redirect(char **args, int arg_count) {
  if(strcmp(args[arg_count-2], ">") == 0) {
    int fd = open(args[arg_count-1], O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    dup2(fd, fileno(stdout));
    args[arg_count-2] = 0;
    args[arg_count-1] = 0;
  }

  else if(strcmp(args[arg_count-2], ">>") == 0) {
    int fd = open(args[arg_count-1], O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    dup2(fd, fileno(stdout));
    args[arg_count-2] = 0;
    args[arg_count-1] = 0;
  }

  else if(strcmp(args[arg_count-2], "<") == 0) {
    int fd = open(args[arg_count-1], O_RDONLY);
    dup2(fd, fileno(stdin));
    args[arg_count-2] = 0;
    args[arg_count-1] = 0;
  }

  return args;
}

static void execute(char *line) {
  int arg_count;
  char **args = split(line, &arg_count, " ");

    if(!run_builtin(args, arg_count)) {
      pid_t pid = fork();

      if(pid == 0) {
        if(arg_count >= 3) {
          args = redirect(args, arg_count);
        }

        shell_register_current_pid(pid);
        execvp(args[0], args);
        fprintf(stderr, "error: couldn't execute %s\n", args[0]);
      }

      else {
        int status;
        wait(&status);
      }
    }
}

void shell_execute(char *line) {
  int arg_count;
  char **args = split(line, &arg_count, " ");
  execute(line);
  free(args);
}