Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
shell
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
coms352
shell
Commits
bcd47a9f
Commit
bcd47a9f
authored
Mar 02, 2018
by
Matthew Burket
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Work on getting piping working
parent
b922752f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
1 deletion
+40
-1
.idea/.name
.idea/.name
+1
-0
main.c
main.c
+35
-0
tokenize.c
tokenize.c
+4
-1
No files found.
.idea/.name
0 → 100644
View file @
bcd47a9f
coms352_shell
\ No newline at end of file
main.c
View file @
bcd47a9f
...
...
@@ -40,6 +40,10 @@ int main() {
exit
(
EXIT_SUCCESS
);
}
char
*
doMore
=
strchr
(
readBuff
,
'|'
);
if
(
doMore
!=
NULL
)
{
readBuff
=
strtok
(
readBuff
,
"|"
);
}
char
**
tokens
=
malloc
(
sizeof
(
char
)
*
5000
);
int
numTokens
=
tokenize
(
readBuff
,
tokens
);
char
*
lastToken
=
tokens
[
numTokens
-
2
];
...
...
@@ -114,13 +118,29 @@ int main() {
storeHist
(
readBuff
,
&
historyPos
,
historyBuff
);
int
runInBackground
=
0
;
int
doMoreCheck
=
0
;
if
(
doMore
!=
NULL
)
{
doMoreCheck
=
1
;
}
if
(
strcmp
(
lastToken
,
"&"
)
==
TRUE
)
{
runInBackground
=
1
;
tokens
[
numTokens
-
2
]
=
NULL
;
}
int
thePipe
[
2
];
if
(
pipe
(
thePipe
)
!=
0
)
{
fprintf
(
stderr
,
"Unable to create the pipe. Aborting."
);
exit
(
EXIT_FAILURE
);
}
if
(
fork
()
==
0
)
{
if
(
doMoreCheck
!=
0
)
{
close
(
STDOUT_FILENO
);
dup
(
thePipe
[
1
]);
close
(
thePipe
[
0
]);
}
int
returnCode
=
execvp
(
tokens
[
0
],
tokens
);
close
(
thePipe
[
1
]);
if
(
returnCode
==
-
1
)
{
fprintf
(
stdout
,
"bsh: Command not found
\n
"
);
}
...
...
@@ -129,6 +149,21 @@ int main() {
wait
(
NULL
);
}
}
// Do if more fork, otherwise short cricut and move on with life
if
(
doMoreCheck
!=
0
&&
fork
()
==
0
)
{
close
(
STDIN_FILENO
);
dup
(
thePipe
[
0
]);
close
(
thePipe
[
1
]);
char
*
moreCmd
[]
=
{
"more"
};
execvp
(
moreCmd
[
0
],
moreCmd
);
close
(
thePipe
[
0
]);
exit
(
EXIT_SUCCESS
);
}
else
if
(
doMoreCheck
!=
0
)
{
// wait(NULL);
}
free
(
tokens
);
}
}
...
...
tokenize.c
View file @
bcd47a9f
...
...
@@ -7,7 +7,8 @@
/// Takes in the user input and tokenizes it
///
/// \param string user input
/// \param output
/// \param output tokens
/// \param pipe does the command need to be piped to more
/// \return number of tokens
int
tokenize
(
char
*
string
,
char
**
output
)
{
// Make strtok thread safe
...
...
@@ -26,5 +27,7 @@ int tokenize(char *string, char **output) {
output
[
i
]
=
token
;
i
++
;
}
// Return token count
return
i
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment