Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cs283
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joey Le
Cs283
Commits
920b9aa5
Commit
920b9aa5
authored
4 months ago
by
Joey Le
Browse files
Options
Downloads
Patches
Plain Diff
Fixed a few bugs on powershell hopefully it works on tux?!?!?!
parent
65c97d44
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment-05/starter/dshlib.c
+14
-74
14 additions, 74 deletions
Assignment-05/starter/dshlib.c
with
14 additions
and
74 deletions
Assignment-05/starter/dshlib.c
+
14
−
74
View file @
920b9aa5
...
@@ -53,9 +53,6 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
...
@@ -53,9 +53,6 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
return
OK
;
return
OK
;
}
}
int
execute_pipeline
(
command_list_t
*
cmd_list
)
{
int
execute_pipeline
(
command_list_t
*
cmd_list
)
{
int
num_commands
=
cmd_list
->
num
;
int
num_commands
=
cmd_list
->
num
;
int
pipefd
[
2
];
int
pipefd
[
2
];
...
@@ -76,7 +73,7 @@ int execute_pipeline(command_list_t *cmd_list) {
...
@@ -76,7 +73,7 @@ int execute_pipeline(command_list_t *cmd_list) {
return
ERR_MEMORY
;
return
ERR_MEMORY
;
}
}
if
(
pids
[
i
]
==
0
)
{
if
(
pids
[
i
]
==
0
)
{
// Child process
if
(
prev_pipe_read
!=
-
1
)
{
if
(
prev_pipe_read
!=
-
1
)
{
dup2
(
prev_pipe_read
,
STDIN_FILENO
);
dup2
(
prev_pipe_read
,
STDIN_FILENO
);
close
(
prev_pipe_read
);
close
(
prev_pipe_read
);
...
@@ -89,7 +86,7 @@ int execute_pipeline(command_list_t *cmd_list) {
...
@@ -89,7 +86,7 @@ int execute_pipeline(command_list_t *cmd_list) {
}
}
cmd_buff_t
*
cmd
=
&
cmd_list
->
commands
[
i
];
cmd_buff_t
*
cmd
=
&
cmd_list
->
commands
[
i
];
execvp
(
cmd
->
exe
,
cmd
->
argv
);
execvp
(
cmd
->
argv
[
0
]
,
cmd
->
argv
);
perror
(
"execvp"
);
perror
(
"execvp"
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
else
{
}
else
{
...
@@ -135,30 +132,17 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
...
@@ -135,30 +132,17 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
char
*
command
=
strtok_r
(
trimmed_line
,
PIPE_STRING
,
&
saveptr1
);
char
*
command
=
strtok_r
(
trimmed_line
,
PIPE_STRING
,
&
saveptr1
);
while
(
command
!=
NULL
&&
command_count
<
CMD_MAX
)
{
while
(
command
!=
NULL
&&
command_count
<
CMD_MAX
)
{
memset
(
&
clist
->
commands
[
command_count
],
0
,
sizeof
(
c
ommand
_t
));
memset
(
&
clist
->
commands
[
command_count
],
0
,
sizeof
(
c
md_buff
_t
));
char
*
token
=
strtok_r
(
command
,
" "
,
&
saveptr2
);
char
*
token
=
strtok_r
(
command
,
" "
,
&
saveptr2
);
if
(
token
!=
NULL
)
{
if
(
token
!=
NULL
)
{
if
(
strlen
(
token
)
>=
EXE_MAX
)
{
free
(
original_line
);
return
ERR_CMD_OR_ARGS_TOO_BIG
;
}
strcpy
(
clist
->
commands
[
command_count
].
exe
,
token
);
int
arg_count
=
0
;
int
arg_count
=
0
;
while
((
token
=
strtok_r
(
NULL
,
" "
,
&
saveptr2
))
!=
NULL
)
{
while
(
token
!=
NULL
&&
arg_count
<
CMD_ARGV_MAX
-
1
)
{
if
(
arg_count
>=
ARG_MAX
-
1
)
{
clist
->
commands
[
command_count
].
argv
[
arg_count
]
=
token
;
free
(
original_line
);
return
ERR_CMD_OR_ARGS_TOO_BIG
;
}
if
(
strlen
(
token
)
>=
ARG_MAX
)
{
free
(
original_line
);
return
ERR_CMD_OR_ARGS_TOO_BIG
;
}
strcpy
(
clist
->
commands
[
command_count
].
args
[
arg_count
],
token
);
arg_count
++
;
arg_count
++
;
token
=
strtok_r
(
NULL
,
" "
,
&
saveptr2
);
}
}
clist
->
commands
[
command_count
].
arg
s
[
arg_count
]
=
NULL
;
clist
->
commands
[
command_count
].
arg
v
[
arg_count
]
=
NULL
;
}
}
command_count
++
;
command_count
++
;
...
@@ -166,7 +150,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
...
@@ -166,7 +150,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
}
}
if
(
command
!=
NULL
)
{
if
(
command
!=
NULL
)
{
// Too many commands
free
(
original_line
);
free
(
original_line
);
return
ERR_TOO_MANY_COMMANDS
;
return
ERR_TOO_MANY_COMMANDS
;
}
}
...
@@ -176,51 +159,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
...
@@ -176,51 +159,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
return
OK
;
return
OK
;
}
}
/*
* Implement your exec_local_cmd_loop function by building a loop that prompts the
* user for input. Use the SH_PROMPT constant from dshlib.h and then
* use fgets to accept user input.
*
* while(1){
* printf("%s", SH_PROMPT);
* if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){
* printf("\n");
* break;
* }
* //remove the trailing \n from cmd_buff
* cmd_buff[strcspn(cmd_buff,"\n")] = '\0';
*
* //IMPLEMENT THE REST OF THE REQUIREMENTS
* }
*
* Also, use the constants in the dshlib.h in this code.
* SH_CMD_MAX maximum buffer size for user input
* EXIT_CMD constant that terminates the dsh program
* SH_PROMPT the shell prompt
* OK the command was parsed properly
* WARN_NO_CMDS the user command was empty
* ERR_TOO_MANY_COMMANDS too many pipes used
* ERR_MEMORY dynamic memory management failure
*
* errors returned
* OK No error
* ERR_MEMORY Dynamic memory management failure
* WARN_NO_CMDS No commands parsed
* ERR_TOO_MANY_COMMANDS too many pipes used
*
* console messages
* CMD_WARN_NO_CMD print on WARN_NO_CMDS
* CMD_ERR_PIPE_LIMIT print on ERR_TOO_MANY_COMMANDS
* CMD_ERR_EXECUTE print on execution failure of external command
*
* Standard Library Functions You Might Want To Consider Using (assignment 1+)
* malloc(), free(), strlen(), fgets(), strcspn(), printf()
*
* Standard Library Functions You Might Want To Consider Using (assignment 2+)
* fork(), execvp(), exit(), chdir()
*/
int
exec_local_cmd_loop
()
{
int
exec_local_cmd_loop
()
{
char
cmd_buff
[
SH_CMD_MAX
];
char
cmd_buff
[
SH_CMD_MAX
];
int
rc
=
OK
;
int
rc
=
OK
;
...
@@ -254,7 +192,9 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
...
@@ -254,7 +192,9 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
rc
=
ERR_MEMORY
;
rc
=
ERR_MEMORY
;
}
}
free_command_list
(
&
cmd_list
);
for
(
int
i
=
0
;
i
<
cmd_list
.
num
;
i
++
)
{
free
(
cmd_list
.
commands
[
i
].
_cmd_buffer
);
}
}
else
{
}
else
{
cmd_buff_t
cmd
;
cmd_buff_t
cmd
;
if
(
build_cmd_buff
(
cmd_buff
,
&
cmd
)
!=
OK
)
{
if
(
build_cmd_buff
(
cmd_buff
,
&
cmd
)
!=
OK
)
{
...
...
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