Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS210-shell
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nikolay Tsanov CS2018
CS210-shell
Commits
7bd37d2c
Commit
7bd37d2c
authored
5 years ago
by
krasimir
Browse files
Options
Downloads
Patches
Plain Diff
Stage 5
parent
958ad600
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
shell.c
+55
-11
55 additions, 11 deletions
shell.c
shell.h
+18
-0
18 additions, 0 deletions
shell.h
with
73 additions
and
11 deletions
shell.c
+
55
−
11
View file @
7bd37d2c
...
...
@@ -7,12 +7,18 @@
#include
"shell.h"
#include
<sys/wait.h>
int
counter
=
0
;
int
historySize
=
0
;
Command
history
[
MAX_HISTORY_LENGTH
];
void
run_shell
()
{
int
status
;
String
input
,
arguments
[
MAX_INPUT_LENGTH
],
originalPath
;
char
directory
[
MAX_PATH_LENGTH
];
for
(
int
i
=
0
;
i
<
MAX_HISTORY_LENGTH
;
i
++
)
{
history
[
i
].
line
=
malloc
(
sizeof
(
char
)
*
MAX_INPUT_LENGTH
);
}
// Set the current directory to HOME
if
(
getenv
(
"HOME"
)
!=
NULL
)
{
...
...
@@ -33,6 +39,7 @@ void run_shell() {
// Alocate memory for the input & read user's input
input
=
malloc
(
MAX_INPUT_LENGTH
*
sizeof
(
char
));
read_input
(
input
);
add_history
(
input
);
// Tokenize to get the arguments
tokenize
(
input
,
arguments
);
...
...
@@ -55,6 +62,23 @@ void read_input(String input) {
exit
(
EXIT_STATUS
);
}
}
void
add_history
(
String
input
)
{
if
(
strcspn
(
input
,
"!"
)
>
0
)
{
history
[
counter
].
number
=
counter
+
1
;
strcpy
(
history
[
counter
].
line
,
input
);
counter
=
(
counter
+
1
)
%
MAX_HISTORY_LENGTH
;
if
(
historySize
<=
20
)
{
historySize
++
;
}
}
}
// 1 - ls
// 2 - cd ..
// 3 - getpath
// ...
// 20 - ...
void
tokenize
(
String
input
,
String
arguments
[])
{
String
token
=
strtok
(
input
,
TOKENIZE_DELIM
);
int
index
=
1
;
...
...
@@ -91,17 +115,22 @@ int run_command(String arguments[]) {
setenv
(
"/usr"
,
"PATH"
,
1
);
}
//Implement the CD - command that changes the current directory.
// if(strcmp(arguments[0], "cd") == 0) {
// if(arguments[1] == NULL) {
// chdir(getenv("HOME"));
// }
if
(
strcmp
(
arguments
[
0
],
"history"
)
==
0
)
{
print_history
();
return
SUCCESS_STATUS
;
}
// if(chdir(arguments[1]) < 0) {
// perror("ERROR");
// }
// return SUCCESS_STATUS;
// }
//Implement the CD - command that changes the current directory.
if
(
strcmp
(
arguments
[
0
],
"cd"
)
==
0
)
{
if
(
arguments
[
1
]
==
NULL
)
{
chdir
(
getenv
(
"HOME"
));
}
if
(
chdir
(
arguments
[
1
])
<
0
)
{
perror
(
"ERROR"
);
}
return
SUCCESS_STATUS
;
}
// If the command was not found above, fork a child process and run the command there.
...
...
@@ -117,6 +146,21 @@ void get_directory(char directory[]) {
}
}
void
print_history
()
{
int
index
=
0
;
int
commandCounter
=
0
;
while
(
commandCounter
<
historySize
&&
history
[
index
].
number
!=
1
)
{
index
++
;
commandCounter
++
;
}
commandCounter
=
1
;
while
(
commandCounter
<
historySize
)
{
printf
(
"%d - %s"
,
commandCounter
,
history
[
index
].
line
);
index
=
(
index
+
1
)
%
MAX_HISTORY_LENGTH
;
commandCounter
++
;
}
}
int
execute_system_commands
(
String
arguments
[])
{
int
status
;
...
...
@@ -136,4 +180,4 @@ int execute_system_commands(String arguments[]) {
}
return
SUCCESS_STATUS
;
}
\ No newline at end of file
}
This diff is collapsed.
Click to expand it.
shell.h
+
18
−
0
View file @
7bd37d2c
/* shell.h */
#define EXIT_STATUS 0
#define SUCCESS_STATUS 1
#define MAX_HISTORY_LENGTH 20
#define MAX_INPUT_LENGTH 512
#define MAX_PATH_LENGTH 512
#define TOKENIZE_DELIM " \t\n|><&;"
...
...
@@ -18,6 +19,14 @@ typedef enum {
true
=
1
}
Boolean
;
/*
* Define command history struct
*/
typedef
struct
{
String
line
;
int
number
;
}
Command
;
/**
* Run the shell program
*/
...
...
@@ -28,6 +37,10 @@ void get_directory(char directory[]);
*/
int
execute_system_commands
(
String
argruments
[]);
/*
* Prints history
*/
void
print_history
();
/**
* Run the shell program
...
...
@@ -39,6 +52,11 @@ int run_command(String argruments[]);
*/
void
tokenize
(
String
input
,
String
argruments
[]);
/*
* Add input to history
*/
void
add_history
(
String
input
);
/**
* Run the shell program
*/
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment