-
Notifications
You must be signed in to change notification settings - Fork 0
/
internals.c
124 lines (109 loc) · 2.84 KB
/
internals.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
#include "internals.h"
#include "dirstack.h"
#include "args.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/history.h>
int is_internal(simple_command_t *s)
{
/* check for internal */
if (!strcmp(s->verb->string, EXIT) ||
!strcmp(s->verb->string, QUIT) ||
!strcmp(s->verb->string, CHDIR) ||
!strcmp(s->verb->string, PUSHD) ||
!strcmp(s->verb->string, POPD) ||
!strcmp(s->verb->string, DIRS) ||
!strcmp(s->verb->string, ECHO) ||
!strcmp(s->verb->string, PWD) ||
!strcmp(s->verb->string, HIST) ||
s->verb->next_part != NULL)
return 1;
return 0;
}
int run_internal(simple_command_t *s)
{
char *dir_str, *verb_str;
verb_str = strdup(env_arg(s->verb));
if (strlen(verb_str) == 0)
return 0;
/* exit internal cmd */
if (!strcmp(verb_str, EXIT) || !strcmp(verb_str, QUIT)){
/* write the command history to disk */
write_history(NULL);
/* exit gracefully */
exit(0);
}
/* cd internal cmd */
if (!strcmp(verb_str, CHDIR)) {
if (s->params == NULL) { // if cd has no parameters, change to $HOME directory
chdir(getenv("HOME"));
return 0;
}
/* change dir */
dir_str = strdup(env_arg(s->params));
if (chdir(dir_str) != 0) {
perror("Error when changing directory\n");
return 1;
}
}
/* push directory onto the stack */
if(!strcmp(verb_str, PUSHD)) {
if (s->params == NULL){
printf("Must insert directory name\n");
return 1;
}
/* insert the current directory onto the stack */
push(&ds, getcwd(NULL, 256));
if(chdir(env_arg(s->params)) != 0){
printf("Directory does not exist\n");
pop(&ds);
}
}
/* show directories in the stack */
if(!strcmp(verb_str, DIRS)) {
if (!print(&ds)){
printf("Stack is empty.\n");
return 1;
}
}
/* pop directory off the stack */
if(!strcmp(verb_str, POPD)) {
dir_str = pop(&ds);
if(dir_str) {
chdir(dir_str);
}else{
printf("Stack is empty.\n");
return 1;
}
}
/* echo [params ...] just echoes the parameters back to the user */
if(!strcmp(verb_str, ECHO)) {
int argc, i;
char **argv;
get_args(s, &argc, &argv);
for(i=1; i<=argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
}
/* prints working directory */
if(!strcmp(verb_str, PWD)) {
printf("%s \n", getcwd(NULL, 256));
}
/* prints the command history */
if(!strcmp(verb_str, HIST)){
/*
* HIST_ENTRY is a structure defined by the History library which holds the line and timestamp of
* a history entry.
* define in readline/history.h
*/
HIST_ENTRY **history;
int i;
if (history = history_list())
for (i=0; history[i] != NULL; i++)
printf ("%d %s\n", i, history[i]->line);
}
return 0;
}