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
Andrew To
CS283
Commits
34f5ebfb
Commit
34f5ebfb
authored
4 months ago
by
dt686
Browse files
Options
Downloads
Patches
Plain Diff
small fix
parent
91e3e9a0
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
hw5/starter/bats/student_tests.sh
+63
-0
63 additions, 0 deletions
hw5/starter/bats/student_tests.sh
hw5/starter/dsh
+0
-0
0 additions, 0 deletions
hw5/starter/dsh
hw5/starter/dshlib.c
+28
-16
28 additions, 16 deletions
hw5/starter/dshlib.c
with
91 additions
and
16 deletions
hw5/starter/bats/student_tests.sh
+
63
−
0
View file @
34f5ebfb
...
@@ -12,3 +12,66 @@ EOF
...
@@ -12,3 +12,66 @@ EOF
# Assertions
# Assertions
[
"
$status
"
-eq
0
]
[
"
$status
"
-eq
0
]
}
}
@test
"Change directory then do ls"
{
current
=
$(
pwd
)
cd
/tmp
mkdir
-p
student-test
cd
student-test
touch
test_file
cd
..
run
"
${
current
}
/dsh"
<<
EOF
cd student-test | ls
EOF
# Strip all whitespace (spaces, tabs, newlines) from the output
stripped_output
=
$(
echo
"
$output
"
|
tr
-d
'[:space:]'
)
# Expected output with all whitespace removed for easier matching
expected_output
=
"test_filedsh3>dsh3>cmdloopreturned0"
# These echo commands will help with debugging and will only print
#if the test fails
echo
"Captured stdout:"
echo
"Output:
$output
"
echo
"Exit Status:
$status
"
echo
"
${
stripped_output
}
->
${
expected_output
}
"
# Check exact match
[
"
$stripped_output
"
=
"
$expected_output
"
]
# Assertions
[
"
$status
"
-eq
0
]
}
@test
"Change directory"
{
current
=
$(
pwd
)
cd
/tmp
mkdir
-p
student-test
run
"
${
current
}
/dsh"
<<
EOF
cd student-test
pwd
EOF
# Strip all whitespace (spaces, tabs, newlines) from the output
stripped_output
=
$(
echo
"
$output
"
|
tr
-d
'[:space:]'
)
# Expected output with all whitespace removed for easier matching
expected_output
=
"/tmp/student-testdsh3>dsh3>dsh3>cmdloopreturned0"
# These echo commands will help with debugging and will only print
#if the test fails
echo
"Captured stdout:"
echo
"Output:
$output
"
echo
"Exit Status:
$status
"
echo
"
${
stripped_output
}
->
${
expected_output
}
"
# Check exact match
[
"
$stripped_output
"
=
"
$expected_output
"
]
# Assertions
[
"
$status
"
-eq
0
]
}
This diff is collapsed.
Click to expand it.
hw5/starter/dsh
0 → 100755
+
0
−
0
View file @
34f5ebfb
File added
This diff is collapsed.
Click to expand it.
hw5/starter/dshlib.c
+
28
−
16
View file @
34f5ebfb
...
@@ -189,10 +189,16 @@ int execute_pipeline(command_list_t *clist){
...
@@ -189,10 +189,16 @@ int execute_pipeline(command_list_t *clist){
}
}
// Execute command
// Execute command
if
(
strcmp
(
clist
->
commands
[
i
].
argv
[
0
],
"cd"
)
==
0
){
continue
;
}
else
{
execvp
(
clist
->
commands
[
i
].
argv
[
0
],
clist
->
commands
[
i
].
argv
);
execvp
(
clist
->
commands
[
i
].
argv
[
0
],
clist
->
commands
[
i
].
argv
);
perror
(
"execvp"
);
perror
(
"execvp"
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
}
}
}
// Parent process: close all pipe ends
// Parent process: close all pipe ends
...
@@ -206,9 +212,9 @@ int execute_pipeline(command_list_t *clist){
...
@@ -206,9 +212,9 @@ int execute_pipeline(command_list_t *clist){
waitpid
(
pids
[
i
],
NULL
,
0
);
waitpid
(
pids
[
i
],
NULL
,
0
);
}
}
exit
(
OK
);
//
exit(OK);
//
return OK;
return
OK
;
}
}
...
@@ -222,12 +228,12 @@ int exec_local_cmd_loop()
...
@@ -222,12 +228,12 @@ int exec_local_cmd_loop()
// TODO IMPLEMENT MAIN LOOP
// TODO IMPLEMENT MAIN LOOP
while
(
1
){
while
(
1
){
printf
(
"%s"
,
SH_PROMPT
);
if
(
fgets
(
cmd_buff
,
ARG_MAX
,
stdin
)
==
NULL
){
if
(
fgets
(
cmd_buff
,
ARG_MAX
,
stdin
)
==
NULL
){
printf
(
"
\n
"
);
printf
(
"
\n
"
);
break
;
break
;
}
}
printf
(
"%s"
,
SH_PROMPT
);
//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'
;
...
@@ -241,16 +247,22 @@ int exec_local_cmd_loop()
...
@@ -241,16 +247,22 @@ int exec_local_cmd_loop()
}
}
build_cmd_list
(
cmd_buff
,
&
clist
);
build_cmd_list
(
cmd_buff
,
&
clist
);
int
supervisor
=
fork
();
if
(
strcmp
(
clist
.
commands
[
0
].
argv
[
0
],
"cd"
)
==
0
){
if
(
supervisor
==
0
)
{
if
(
clist
.
commands
[
0
].
argc
==
2
){
execute_pipeline
(
&
clist
);
// Supervisor process
chdir
(
clist
.
commands
[
0
].
argv
[
1
]);
}
else
{
printf
(
"cd command requires 1 argument
\n
"
);
}
for
(
int
i
=
0
;
i
<
clist
.
num
-
1
;
i
++
){
clist
.
commands
[
i
]
=
clist
.
commands
[
i
+
1
];
}
}
if
(
supervisor
==
-
1
)
{
clist
.
num
--
;
perror
(
"fork supervisor"
);
exit
(
EXIT_FAILURE
);
}
}
waitpid
(
supervisor
,
NULL
,
0
);
execute_pipeline
(
&
clist
);
}
}
...
...
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