-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
166 lines (140 loc) · 3.4 KB
/
builtin.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "builtin.h"
#include <limits.h>
#include "utils.h"
static const int EXECUTED = (int) true, NOT_EXECUTED = (int) false;
static const char EXIT_CMD_STR[] = "exit";
static const int LEN_EXIT_CMD_STR = 5;
static const char CD_CMD_STR[] = "cd";
static const int LEN_CD_CMD_STR = 3;
static const char PWD_CMD_STR[] = "pwd";
static const int LEN_PWD_CMD_STR = 4;
// returns true if the 'exit' call
// should be performed
//
// (It must not be called from here)
int
exit_shell(char *cmd, int *status)
{
if (strncmp(cmd, EXIT_CMD_STR, LEN_EXIT_CMD_STR) != 0) {
return NOT_EXECUTED;
}
*status = EXIT_SUCCESS;
return EXECUTED;
}
/*
* Returns true if the `cmd` string starts with `CD_CMD_STR`. Returns false otherwise
*/
static bool
is_cd_command(char *cmd)
{
int cmd_len = strlen(cmd);
int max_iter = MIN(LEN_CD_CMD_STR - 1, cmd_len);
int i = 0;
for (i = 0; i < max_iter; i++) {
if (cmd[i] != CD_CMD_STR[i]) {
return false;
}
}
if (i <= cmd_len) {
return cmd[i] == END_STRING || cmd[i] == SPACE;
}
return false;
}
/*
* Returns true if the string `path` has at least a character that is not a
* space character. Returns false otherwise
*/
static bool
is_non_empty(char *path)
{
int max_iter = MIN(PATH_MAX, strlen(path));
for (int i = 0; i < max_iter; i++) {
if (path[i] != SPACE) {
return true;
}
}
return false;
}
// returns true if "chdir" was performed
// this means that if 'cmd' contains:
// 1. $ cd directory (change to 'directory')
// 2. $ cd (change to $HOME)
// it has to be executed and then return true
//
// Remember to update the 'prompt' with the
// new directory.
//
// Examples:
// 1. cmd = ['c','d', ' ', '/', 'b', 'i', 'n', '\0']
// 2. cmd = ['c','d', '\0']
int
cd(char *cmd, char *prompt, int *status)
{
if (!is_cd_command(cmd)) {
return NOT_EXECUTED;
}
char dest_path[PATH_MAX] = { END_STRING };
char *temp_path = strchr(cmd, SPACE);
if (temp_path != NULL && is_non_empty(temp_path)) {
strncpy(dest_path, temp_path + 1, PATH_MAX - 1);
} else {
char *home_path = getenv(HOME_ENV_VAR_KEY);
if (home_path == NULL) {
perror("Error while retrieving home path");
*status = EXIT_FAILURE;
return EXECUTED;
}
strncpy(dest_path, home_path, PATH_MAX - 1);
}
char full_rest_path[PATH_MAX] = { END_STRING };
char *res = realpath(dest_path, full_rest_path);
if (res == NULL) {
perror("Error while retrieving realpath");
*status = EXIT_FAILURE;
return EXECUTED;
}
int _res = chdir(full_rest_path);
if (_res == GENERIC_ERROR_CODE) {
perror("Error while changing directory");
*status = EXIT_FAILURE;
return EXECUTED;
}
update_prompt(prompt, full_rest_path);
*status = EXIT_SUCCESS;
return EXECUTED;
}
// returns true if 'pwd' was invoked
// in the command line
//
// (It has to be executed here and then
// return true)
int
pwd(char *cmd, int *status)
{
if (strncmp(cmd, PWD_CMD_STR, LEN_PWD_CMD_STR) != 0) {
return NOT_EXECUTED;
}
char cwd_path[PATH_MAX] = { END_STRING };
char *res = getcwd(cwd_path, PATH_MAX);
if (res == NULL) {
perror("Error while executing pwd");
*status = EXIT_FAILURE;
return EXECUTED;
}
printf("%s\n", cwd_path);
*status = EXIT_SUCCESS;
return EXECUTED;
}
// returns true if `history` was invoked
// in the command line
//
// (It has to be executed here and then
// return true)
int
history(char *cmd, int *status)
{
// Your code here
MARK_UNUSED_ALWAYS(cmd);
MARK_UNUSED_ALWAYS(status);
return NOT_EXECUTED;
}