Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mysh-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
강병수
mysh-1
Commits
0d7b2f84
Commit
0d7b2f84
authored
7 years ago
by
강병수
Browse files
Options
Downloads
Patches
Plain Diff
done
parent
98fbc382
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/built_in.c
+10
-3
10 additions, 3 deletions
src/built_in.c
src/commands.c
+39
-2
39 additions, 2 deletions
src/commands.c
src/main.c
+1
-0
1 addition, 0 deletions
src/main.c
src/signal_handlers.c
+3
-0
3 additions, 0 deletions
src/signal_handlers.c
src/utils.c
+2
-5
2 additions, 5 deletions
src/utils.c
with
55 additions
and
10 deletions
src/built_in.c
+
10
−
3
View file @
0d7b2f84
...
...
@@ -8,6 +8,8 @@
#include
"built_in.h"
int
PID
=
1
;
int
do_cd
(
int
argc
,
char
**
argv
)
{
if
(
!
validate_cd_argv
(
argc
,
argv
))
return
-
1
;
...
...
@@ -36,7 +38,12 @@ int do_fg(int argc, char** argv) {
if
(
!
validate_fg_argv
(
argc
,
argv
))
return
-
1
;
// TODO: Fill this.
int
stat
;
if
(
PID
!=
1
)
{
printf
(
"%d running %s
\n
"
,
PID
,
running_command
);
waitpid
(
PID
,
&
stat
,
0
);
PID
=
1
;
}
return
0
;
}
...
...
This diff is collapsed.
Click to expand it.
src/commands.c
+
39
−
2
View file @
0d7b2f84
...
...
@@ -2,6 +2,9 @@
#include
<stdlib.h>
#include
<string.h>
#include
<assert.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include
"commands.h"
#include
"built_in.h"
...
...
@@ -28,6 +31,23 @@ static int is_built_in_command(const char* command_name)
/*
* Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality.
*/
void
run_command
(
struct
single_command
*
com
)
{
execv
(
com
->
argv
[
0
],
com
->
argv
);
char
PATH
[
50
];
char
*
tok
,
ptrtmp
=
NULL
;
sprintf
(
PATH
,
"%s"
,
getenv
(
"PATH"
));
tok
=
strtok_r
(
PATH
,
":"
,
&
ptrtmp
);
while
(
tok
){
char
PATH_tmp
[
50
];
char
*
tok_tmp
=
NULL
;
sprintf
(
PATH_tmp
,
"%s/%s"
,
tok
,
com
->
argv
[
0
]);
execv
(
PATH_tmp
,
com
->
argv
);
tok
=
strtok_r
(
NULL
,
":"
,
&
ptrtmp
);
}
}
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
{
if
(
n_commands
>
0
)
{
...
...
@@ -49,8 +69,25 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
return
0
;
}
else
if
(
strcmp
(
com
->
argv
[
0
],
"exit"
)
==
0
)
{
return
1
;
}
else
{
}
else
if
(
n_commands
==
1
)
{
int
pid
=
fork
(),
stat
=
0
;
if
(
pid
>
0
)
{
wait
(
&
stat
);
return
stat
?
-
1
:
0
;
}
else
if
(
!
pid
)
{
run_command
(
com
);
fprintf
(
stderr
,
"%s: command not found
\n
"
,
com
->
argv
[
0
]);
exit
(
1
);
}
else
if
(
pid
<
0
)
{
fprintf
(
stderr
,
"fork error
\n
"
,
com
->
argv
[
0
]);
exit
(
1
);
}
}
else
{
return
-
1
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main.c
+
1
−
0
View file @
0d7b2f84
...
...
@@ -10,6 +10,7 @@ int main()
{
char
buf
[
8096
];
putenv
(
"PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
);
while
(
1
)
{
fgets
(
buf
,
8096
,
stdin
);
...
...
This diff is collapsed.
Click to expand it.
src/signal_handlers.c
+
3
−
0
View file @
0d7b2f84
#include
<stdlib.h>
#include
<unistd.h>
#include
"signal_handlers.h"
void
catch_sigint
(
int
signalNo
)
...
...
This diff is collapsed.
Click to expand it.
src/utils.c
+
2
−
5
View file @
0d7b2f84
...
...
@@ -4,9 +4,7 @@
#include
<stdlib.h>
#include
<string.h>
void
mysh_parse_command
(
const
char
*
command
,
int
*
n_commands
,
struct
single_command
(
*
commands
)[])
void
mysh_parse_command
(
const
char
*
command
,
int
*
n_commands
,
struct
single_command
(
*
commands
)[])
{
char
buf
[
4096
];
strcpy
(
buf
,
command
);
...
...
@@ -28,8 +26,7 @@ void mysh_parse_command(const char* command,
*
n_commands
=
ti
;
}
void
parse_single_command
(
const
char
*
command
,
int
*
argc
,
char
***
argv
)
void
parse_single_command
(
const
char
*
command
,
int
*
argc
,
char
***
argv
)
{
const
int
kMaxArgc
=
512
;
*
argv
=
(
char
**
)
malloc
(
kMaxArgc
*
sizeof
(
char
*
));
...
...
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