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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ansh Bhajjan
cs283
Commits
23ae7975
Commit
23ae7975
authored
4 months ago
by
Ansh
Browse files
Options
Downloads
Patches
Plain Diff
Testing write errors
parent
dd4747e4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
assignments/assignment-5/starter/dsh
+0
-0
0 additions, 0 deletions
assignments/assignment-5/starter/dsh
assignments/assignment-5/starter/dshlib.c
+143
-41
143 additions, 41 deletions
assignments/assignment-5/starter/dshlib.c
with
143 additions
and
41 deletions
assignments/assignment-5/starter/dsh
+
0
−
0
View file @
23ae7975
No preview for this file type
This diff is collapsed.
Click to expand it.
assignments/assignment-5/starter/dshlib.c
+
143
−
41
View file @
23ae7975
...
@@ -99,9 +99,9 @@ Built_In_Cmds exec_built_in_cmd(cmd_buff_t *cmd) {
...
@@ -99,9 +99,9 @@ Built_In_Cmds exec_built_in_cmd(cmd_buff_t *cmd) {
}
}
int
build_cmd_list
(
char
*
cmd_line
,
command_list_t
*
clist
)
{
int
build_cmd_list
(
char
*
cmd_line
,
command_list_t
*
clist
)
{
printf
(
"Before trimming the cmd_line in build_cmd_list: '%s'
\n
"
,
cmd_line
);
//
printf("Before trimming the cmd_line in build_cmd_list: '%s'\n", cmd_line);
trim_spaces
(
cmd_line
);
trim_spaces
(
cmd_line
);
printf
(
"Trimmed cmd_line: '%s'
\n
"
,
cmd_line
);
//
printf("Trimmed cmd_line: '%s'\n", cmd_line);
char
*
cmd_tok_save
=
NULL
;
char
*
cmd_tok_save
=
NULL
;
char
*
cmd_tok
=
strtok_r
(
cmd_line
,
PIPE_STRING
,
&
cmd_tok_save
);
char
*
cmd_tok
=
strtok_r
(
cmd_line
,
PIPE_STRING
,
&
cmd_tok_save
);
...
@@ -111,7 +111,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
...
@@ -111,7 +111,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
while
(
cmd_tok
!=
NULL
)
{
while
(
cmd_tok
!=
NULL
)
{
trim_spaces
(
cmd_tok
);
trim_spaces
(
cmd_tok
);
printf
(
"Command %d: '%s'
\n
"
,
index
,
cmd_tok
);
//
printf("Command %d: '%s'\n", index, cmd_tok);
if
(
index
>=
CMD_MAX
)
{
if
(
index
>=
CMD_MAX
)
{
return
ERR_TOO_MANY_COMMANDS
;
return
ERR_TOO_MANY_COMMANDS
;
...
@@ -121,7 +121,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
...
@@ -121,7 +121,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
cmd
->
argc
=
0
;
cmd
->
argc
=
0
;
cmd
->
_cmd_buffer
=
cmd_tok
;
cmd
->
_cmd_buffer
=
cmd_tok
;
printf
(
"Command %d (before args parsing): '%s'
\n
"
,
index
,
cmd_tok
);
//
printf("Command %d (before args parsing): '%s'\n", index, cmd_tok);
char
*
arg_tok_save
=
NULL
;
char
*
arg_tok_save
=
NULL
;
char
*
arg_tok
=
strtok_r
(
cmd_tok
,
" "
,
&
arg_tok_save
);
char
*
arg_tok
=
strtok_r
(
cmd_tok
,
" "
,
&
arg_tok_save
);
...
@@ -129,7 +129,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
...
@@ -129,7 +129,7 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
while
(
arg_tok
!=
NULL
&&
cmd
->
argc
<
CMD_ARGV_MAX
)
{
while
(
arg_tok
!=
NULL
&&
cmd
->
argc
<
CMD_ARGV_MAX
)
{
trim_spaces
(
arg_tok
);
trim_spaces
(
arg_tok
);
printf
(
"Command %d: argv[%d]: '%s'
\n
"
,
index
,
cmd
->
argc
,
arg_tok
);
//
printf("Command %d: argv[%d]: '%s'\n", index, cmd->argc, arg_tok);
cmd
->
argv
[
cmd
->
argc
]
=
arg_tok
;
cmd
->
argv
[
cmd
->
argc
]
=
arg_tok
;
cmd
->
argc
++
;
cmd
->
argc
++
;
...
@@ -137,17 +137,20 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
...
@@ -137,17 +137,20 @@ int build_cmd_list(char *cmd_line, command_list_t *clist) {
arg_tok
=
strtok_r
(
NULL
,
" "
,
&
arg_tok_save
);
arg_tok
=
strtok_r
(
NULL
,
" "
,
&
arg_tok_save
);
}
}
printf
(
"Command %d finished parsing. argc = %d
\n
"
,
index
,
cmd
->
argc
);
cmd
->
argv
[
cmd
->
argc
]
=
NULL
;
//printf("Command %d finished parsing. argc = %d\n", index, cmd->argc);
index
++
;
index
++
;
cmd_tok
=
strtok_r
(
NULL
,
PIPE_STRING
,
&
cmd_tok_save
);
cmd_tok
=
strtok_r
(
NULL
,
PIPE_STRING
,
&
cmd_tok_save
);
}
}
clist
->
num
=
index
;
clist
->
num
=
index
;
printf
(
"Total number of commands: %d
\n
"
,
clist
->
num
);
//
printf("Total number of commands: %d\n", clist->num);
return
OK
;
return
OK
;
}
}
/*
int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
int pipe_fds[2];
int pipe_fds[2];
pid_t pid;
pid_t pid;
...
@@ -173,36 +176,44 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
...
@@ -173,36 +176,44 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
}
}
if(pid == 0) {
if(pid == 0) {
printf("Command %d: Before redirecting, STDOUT_FILENO = %d\n", cmd_index, STDOUT_FILENO);
// Debug: Print
argv to verify contents
// Debug: Print
the arguments for execvp to check if the command is correct
printf("Command %d: execvp arguments:\n", cmd_index);
printf("Command %d: execvp arguments:\n", cmd_index);
for (int i = 0; cmd->argv[i] != NULL; i++) {
for (int i = 0; cmd->argv[i] != NULL; i++) {
printf
(
"argv[%d]: %s
\n
"
,
i
,
cmd
->
argv
[
i
]);
printf("argv[%d]:
'
%s
'
\n", i, cmd->argv[i]);
}
}
// Ensure stdin redirection for the second command
if (cmd_index > 0) {
if (cmd_index > 0) {
if (dup2(clist->pipes[cmd_index - 1][0], STDIN_FILENO) == -1) {
if (dup2(clist->pipes[cmd_index - 1][0], STDIN_FILENO) == -1) {
perror("Failed to redirect stdin");
perror("Failed to redirect stdin");
exit(1);
exit(1);
}
}
printf("Command %d: stdin redirected from pipe[%d][0] to STDIN_FILENO\n", cmd_index, cmd_index - 1);
printf("Command %d: stdin redirected from pipe[%d][0] to STDIN_FILENO\n", cmd_index, cmd_index - 1);
close(clist->pipes[cmd_index - 1][0]);
printf("Command %d: Closed pipe[%d][0] in child process\n", cmd_index, cmd_index - 1);
}
}
// Redirect stdout if it's not the last command
if (!is_last_cmd) {
if (!is_last_cmd) {
if (dup2(pipe_fds[1], STDOUT_FILENO) == -1) {
if (dup2(pipe_fds[1], STDOUT_FILENO) == -1) {
perror("Failed to redirect stdout");
perror("Failed to redirect stdout");
exit(1);
exit(1);
}
}
printf("Command %d: stdout redirected to pipe[%d][1] from STDOUT_FILENO\n", cmd_index, cmd_index);
printf("Command %d: stdout redirected to pipe[%d][1] from STDOUT_FILENO\n", cmd_index, cmd_index);
//close(pipe_fds[1]); // Close the write end of the pipe
//printf("Command %d: Closed pipe[%d][1] in child process\n", cmd_index, cmd_index);
}
}
for(int i = 0; i < clist->num - 1; i++) {
for(int i = 0; i < clist->num - 1; i++) {
printf
(
"Command %d: Closing pipe[%d][0] and pipe[%d][1]
\n
"
,
cmd_index
,
i
,
i
);
if (i != cmd_index - 1) { // Don't close the pipe we're reading from
close(clist->pipes[i][0]);
close(clist->pipes[i][0]);
close(clist->pipes[i][1]);
close(clist->pipes[i][1]);
printf("Command %d: Closed clist->pipes[%d][0] and clist->pipes[%d][1] in child process\n", cmd_index, i, i);
}
}
}
close
(
pipe_fds
[
0
]);
close(pipe_fds[1]);
close(pipe_fds[1]);
if(execvp(cmd->argv[0], cmd->argv) == -1) {
if(execvp(cmd->argv[0], cmd->argv) == -1) {
...
@@ -213,21 +224,111 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
...
@@ -213,21 +224,111 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
else {
else {
if(!is_last_cmd) {
if(!is_last_cmd) {
close(pipe_fds[1]);
close(pipe_fds[1]);
printf
(
"Command %d: Parent closing pipe[%d][1]
\n
"
,
cmd_index
,
cmd_index
);
printf("Command %d: Parent closing write end pipe[%d][1]\n", cmd_index, cmd_index);
//close(pipe_fds[0]);
//printf("Command %d: Parent closing read end pipe[%d][0]\n", cmd_index, cmd_index);
}
}
/*
if(cmd_index > 0) {
close(clist->pipes[cmd_index - 1][0]);
printf("Command %d: Parent closing pipe[%d][0]\n", cmd_index, cmd_index - 1);
}
int status;
waitpid(pid, &status, 0);
printf("Command %d: Child process finished with status %d\n", cmd_index, status);
}
return OK;
}
*/
int
exec_cmd
(
cmd_buff_t
*
cmd
,
int
cmd_index
,
command_list_t
*
clist
)
{
int
pipe_fds
[
2
];
pid_t
pid
;
int
is_last_cmd
=
(
cmd_index
==
clist
->
num
-
1
);
printf
(
"Command %d: is_last_cmd = %d
\n
"
,
cmd_index
,
is_last_cmd
);
// Create pipe for non-last commands
if
(
!
is_last_cmd
)
{
if
(
pipe
(
pipe_fds
)
==
-
1
)
{
perror
(
"Pipe failed"
);
return
-
1
;
}
printf
(
"Command %d: Pipe created with fd[0] = %d, fd[1] = %d
\n
"
,
cmd_index
,
pipe_fds
[
0
],
pipe_fds
[
1
]);
}
pid
=
fork
();
if
(
pid
==
-
1
)
{
perror
(
"Fork failed"
);
return
-
1
;
}
if
(
pid
==
0
)
{
// Child process
printf
(
"Command %d: Before redirecting, STDOUT_FILENO = %d
\n
"
,
cmd_index
,
STDOUT_FILENO
);
printf
(
"Debug: Command %d, before redirecting, pipe_fds[1] = %d
\n
"
,
cmd_index
,
pipe_fds
[
1
]);
// Debug: Print the arguments for execvp to check if the command is correct
printf
(
"Command %d: execvp arguments:
\n
"
,
cmd_index
);
for
(
int
i
=
0
;
cmd
->
argv
[
i
]
!=
NULL
;
i
++
)
{
printf
(
"argv[%d]: '%s'
\n
"
,
i
,
cmd
->
argv
[
i
]);
}
// Redirect input from previous command, if it's not the first command
if
(
cmd_index
>
0
)
{
if
(
dup2
(
clist
->
pipes
[
cmd_index
-
1
][
0
],
STDIN_FILENO
)
==
-
1
)
{
perror
(
"Failed to redirect stdin"
);
exit
(
1
);
}
printf
(
"Command %d: stdin redirected from pipe[%d][0] to STDIN_FILENO
\n
"
,
cmd_index
,
cmd_index
-
1
);
close
(
clist
->
pipes
[
cmd_index
-
1
][
0
]);
// Close the read end of the previous pipe
printf
(
"Command %d: Closed pipe[%d][0] in child process
\n
"
,
cmd_index
,
cmd_index
-
1
);
}
// Redirect stdout to the pipe if this is not the last command
if
(
!
is_last_cmd
)
{
if
(
dup2
(
pipe_fds
[
1
],
STDOUT_FILENO
)
==
-
1
)
{
perror
(
"Failed to redirect stdout"
);
exit
(
1
);
}
printf
(
"Command %d: stdout redirected to pipe[%d][1] from STDOUT_FILENO
\n
"
,
cmd_index
,
cmd_index
);
close
(
pipe_fds
[
1
]);
// Close the write end of the pipe after redirection
printf
(
"Command %d: Closed pipe[%d][1] in child process
\n
"
,
cmd_index
,
cmd_index
);
}
// Close all unnecessary file descriptors in child process
for
(
int
i
=
0
;
i
<
clist
->
num
-
1
;
i
++
)
{
for
(
int
i
=
0
;
i
<
clist
->
num
-
1
;
i
++
)
{
if
(
i
!=
cmd_index
-
1
)
{
// Don't close the pipe we're reading from
close
(
clist
->
pipes
[
i
][
0
]);
close
(
clist
->
pipes
[
i
][
0
]);
close
(
clist
->
pipes
[
i
][
1
]);
close
(
clist
->
pipes
[
i
][
1
]);
printf
(
"Command %d: Closed clist->pipes[%d][0] and clist->pipes[%d][1] in child process
\n
"
,
cmd_index
,
i
,
i
);
}
}
}
*/
// Execute the command
if
(
execvp
(
cmd
->
argv
[
0
],
cmd
->
argv
)
==
-
1
)
{
perror
(
"Execvp failed"
);
printf
(
"Debug: Execvp failed for command %d. Pipe fds: pipe_fds[0] = %d, pipe_fds[1] = %d
\n
"
,
cmd_index
,
pipe_fds
[
0
],
pipe_fds
[
1
]);
exit
(
1
);
}
}
else
{
// Parent process
// Close the write end of the pipe in the parent if it's not the last command
if
(
!
is_last_cmd
)
{
close
(
pipe_fds
[
1
]);
// Parent closes the write end of the pipe
printf
(
"Command %d: Parent closing write end pipe[%d][1]
\n
"
,
cmd_index
,
cmd_index
);
}
// Close the read end of the previous pipe in the parent process
if
(
cmd_index
>
0
)
{
if
(
cmd_index
>
0
)
{
close
(
clist
->
pipes
[
cmd_index
-
1
][
0
]);
close
(
clist
->
pipes
[
cmd_index
-
1
][
0
]);
// Parent closes the previous read end
printf
(
"Command %d: Parent closing pipe[%d][0]
\n
"
,
cmd_index
,
cmd_index
-
1
);
printf
(
"Command %d: Parent closing pipe[%d][0]
\n
"
,
cmd_index
,
cmd_index
-
1
);
}
}
// Wait for the child process to finish
int
status
;
int
status
;
waitpid
(
pid
,
&
status
,
0
);
waitpid
(
pid
,
&
status
,
0
);
printf
(
"Command %d: Child process finished with status %d
\n
"
,
cmd_index
,
status
);
printf
(
"Command %d: Child process finished with status %d
\n
"
,
cmd_index
,
status
);
...
@@ -235,6 +336,7 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
...
@@ -235,6 +336,7 @@ int exec_cmd(cmd_buff_t *cmd, int cmd_index, command_list_t *clist) {
return
OK
;
return
OK
;
}
}
int
exec_local_cmd_loop
()
{
int
exec_local_cmd_loop
()
{
char
*
cmd_buff
=
malloc
(
SH_CMD_MAX
*
sizeof
(
char
));
char
*
cmd_buff
=
malloc
(
SH_CMD_MAX
*
sizeof
(
char
));
...
@@ -255,7 +357,7 @@ int exec_local_cmd_loop() {
...
@@ -255,7 +357,7 @@ int exec_local_cmd_loop() {
break
;
break
;
}
}
printf
(
"Raw input: '%s'
\n
"
,
cmd_buff
);
//
printf("Raw input: '%s'\n", cmd_buff);
// remove the trailing \n from cmd_buff
// remove the trailing \n from cmd_buff
cmd_buff
[
strcspn
(
cmd_buff
,
"
\n
"
)]
=
'\0'
;
cmd_buff
[
strcspn
(
cmd_buff
,
"
\n
"
)]
=
'\0'
;
...
@@ -263,7 +365,7 @@ int exec_local_cmd_loop() {
...
@@ -263,7 +365,7 @@ int exec_local_cmd_loop() {
// IMPLEMENT THE REST OF THE REQUIREMENTS
// IMPLEMENT THE REST OF THE REQUIREMENTS
trim_spaces
(
cmd_buff
);
trim_spaces
(
cmd_buff
);
printf
(
"Trimmed input in exec_local_cmd_loop: '%s'
\n
"
,
cmd_buff
);
//
printf("Trimmed input in exec_local_cmd_loop: '%s'\n", cmd_buff);
if
(
strlen
(
cmd_buff
)
==
0
)
{
if
(
strlen
(
cmd_buff
)
==
0
)
{
printf
(
CMD_WARN_NO_CMD
);
printf
(
CMD_WARN_NO_CMD
);
...
@@ -276,7 +378,7 @@ int exec_local_cmd_loop() {
...
@@ -276,7 +378,7 @@ int exec_local_cmd_loop() {
}
}
rc
=
build_cmd_list
(
cmd_buff
,
&
clist
);
rc
=
build_cmd_list
(
cmd_buff
,
&
clist
);
printf
(
"RC value: %d
\n
"
,
rc
);
//
printf("RC value: %d\n", rc);
switch
(
rc
)
{
switch
(
rc
)
{
case
OK
:
case
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