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
Joey Le
Cs283
Commits
124bd733
Commit
124bd733
authored
2 months ago
by
Joey Le
Browse files
Options
Downloads
Patches
Plain Diff
Fixed it now
parent
966b801e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment-06/starter/dshlib.c
+41
-17
41 additions, 17 deletions
Assignment-06/starter/dshlib.c
with
41 additions
and
17 deletions
Assignment-06/starter/dshlib.c
+
41
−
17
View file @
124bd733
...
...
@@ -10,7 +10,6 @@
#include
"dshlib.h"
// Function to build a command buffer from a command line
int
build_cmd_buff
(
char
*
cmd_line
,
cmd_buff_t
*
cmd_buff
)
{
if
(
cmd_line
==
NULL
||
cmd_buff
==
NULL
)
{
return
ERR_CMD_OR_ARGS_TOO_BIG
;
...
...
@@ -26,11 +25,11 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
char
*
ptr
=
original_line
;
char
*
arg_start
=
NULL
;
bool
in_quotes
=
false
;
//
For quotes
bool
in_quotes
=
false
;
//For quotes
char
quote_char
=
'\0'
;
while
(
*
ptr
!=
'\0'
)
{
if
((
isspace
((
unsigned
char
)
*
ptr
)
&&
!
in_quotes
)
{
if
((
isspace
((
unsigned
char
)
*
ptr
)
&&
!
in_quotes
)
)
{
if
(
arg_start
!=
NULL
)
{
*
ptr
=
'\0'
;
cmd_buff
->
argv
[
cmd_buff
->
argc
++
]
=
arg_start
;
...
...
@@ -60,11 +59,18 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
cmd_buff
->
argv
[
cmd_buff
->
argc
]
=
NULL
;
// Debugging output to verify parsing
// printf("Parsed command:\n");
// for (int i = 0; i < cmd_buff->argc; i++) {
// printf(" argv[%d]: %s\n", i, cmd_buff->argv[i]);
// }
return
OK
;
}
// Function to trim whitespace from a string
char
*
trim_whitespace
(
char
*
str
)
{
char
*
trim_whitespace
(
char
*
str
)
{
//Had to make a new function for readability
char
*
end
;
while
(
isspace
((
unsigned
char
)
*
str
))
str
++
;
...
...
@@ -76,13 +82,15 @@ char *trim_whitespace(char *str) {
end
=
str
+
strlen
(
str
)
-
1
;
while
(
end
>
str
&&
isspace
((
unsigned
char
)
*
end
))
end
--
;
//
N
ew null terminator
//
n
ew null terminator
*
(
end
+
1
)
=
'\0'
;
return
str
;
}
// Function to parse a pipeline of commands
int
parse_pipeline
(
const
char
*
cmd_line
,
command_list_t
*
clist
)
{
if
(
cmd_line
==
NULL
||
clist
==
NULL
)
{
return
ERR_CMD_OR_ARGS_TOO_BIG
;
...
...
@@ -92,8 +100,7 @@ int parse_pipeline(const char *cmd_line, command_list_t *clist) {
if
(
line_copy
==
NULL
)
{
return
ERR_MEMORY
;
}
// Parsing using pipe
//Parsing using pipe.
clist
->
num
=
0
;
char
*
saveptr
;
char
*
command
=
strtok_r
(
line_copy
,
"|"
,
&
saveptr
);
...
...
@@ -119,11 +126,22 @@ int parse_pipeline(const char *cmd_line, command_list_t *clist) {
return
OK
;
}
// Function to execute a pipeline of commands
int
execute_pipeline
(
command_list_t
*
clist
)
{
int
num_commands
=
clist
->
num
;
int
pipefd
[
2
*
(
num_commands
-
1
)];
pid_t
pids
[
num_commands
];
// for (int i = 0; i < clist->num; i++) {
// printf("Command %d:\n", i);
// for (int j = 0; clist->commands[i].argv[j] != NULL; j++) {
// printf(" argv[%d]: %s\n", j, clist->commands[i].argv[j]);
// }
// }
for
(
int
i
=
0
;
i
<
num_commands
-
1
;
i
++
)
{
if
(
pipe
(
pipefd
+
2
*
i
)
==
-
1
)
{
...
...
@@ -141,7 +159,7 @@ int execute_pipeline(command_list_t *clist) {
}
if
(
pids
[
i
]
==
0
)
{
// Child process
//
Redirect input
if not
the
first
command
// if not first
moves input
if
(
i
>
0
)
{
if
(
dup2
(
pipefd
[
2
*
(
i
-
1
)],
STDIN_FILENO
)
==
-
1
)
{
perror
(
"dup2 stdin"
);
...
...
@@ -157,7 +175,7 @@ int execute_pipeline(command_list_t *clist) {
}
}
// Close all pipe file descriptors
// Close all pipe file descriptors
. Very important stuff
for
(
int
j
=
0
;
j
<
2
*
(
num_commands
-
1
);
j
++
)
{
close
(
pipefd
[
j
]);
}
...
...
@@ -169,12 +187,12 @@ int execute_pipeline(command_list_t *clist) {
}
}
// Close all pipe
file descriptors in the parent
// Close
s
all pipe
for
(
int
i
=
0
;
i
<
2
*
(
num_commands
-
1
);
i
++
)
{
close
(
pipefd
[
i
]);
}
//
W
ait
for all child processes to finis
h
//
w
ait
pid so everyhting is smoot
h
for
(
int
i
=
0
;
i
<
num_commands
;
i
++
)
{
int
status
;
waitpid
(
pids
[
i
],
&
status
,
0
);
...
...
@@ -188,7 +206,8 @@ int execute_pipeline(command_list_t *clist) {
return
OK
;
}
// Main loop for executing local commands
int
exec_local_cmd_loop
()
{
char
cmd_buff
[
SH_CMD_MAX
];
int
rc
=
OK
;
...
...
@@ -217,7 +236,9 @@ int exec_local_cmd_loop() {
continue
;
}
rc
=
execute_pipeline
(
&
cmd_list
);
// Execute the pipeline
execute_pipeline
(
&
cmd_list
);
// Free memory for each command's buffer
for
(
int
i
=
0
;
i
<
cmd_list
.
num
;
i
++
)
{
...
...
@@ -274,4 +295,7 @@ int exec_local_cmd_loop() {
}
return
rc
;
}
\ No newline at end of file
}
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