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
7458f409
Commit
7458f409
authored
7 years ago
by
최민정
Browse files
Options
Downloads
Plain Diff
result
parents
85376be2
b37404c2
No related branches found
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
+14
-2
14 additions, 2 deletions
src/built_in.c
src/commands.c
+54
-15
54 additions, 15 deletions
src/commands.c
src/main.c
+6
-1
6 additions, 1 deletion
src/main.c
src/signal_handlers.c
+3
-3
3 additions, 3 deletions
src/signal_handlers.c
src/utils.c
+2
-2
2 additions, 2 deletions
src/utils.c
with
79 additions
and
23 deletions
src/built_in.c
+
14
−
2
View file @
7458f409
...
...
@@ -5,8 +5,15 @@
#include
<sys/stat.h>
#include
<unistd.h>
#include
<linux/limits.h>
#include
<signal.h>
#include
"built_in.h"
#include
"signal_handlers.h"
#include
"commands.h"
int
pid_list
[
512
];
void
add_pid
(
int
pid
){
pid_list
[
sizeof
(
pid_list
)]
=
pid
;}
int
do_cd
(
int
argc
,
char
**
argv
)
{
if
(
!
validate_cd_argv
(
argc
,
argv
))
...
...
@@ -35,16 +42,21 @@ int do_pwd(int argc, char** argv) {
int
do_fg
(
int
argc
,
char
**
argv
)
{
if
(
!
validate_fg_argv
(
argc
,
argv
))
return
-
1
;
int
pid
;
char
curdir
[
PATH_MAX
];
int
pid
=
getpid
();
getcwd
(
curdir
,
PATH_MAX
);
printf
(
"%s running
\t
%s
\n
"
,
pid
,
curdir
);
signal
(
19
,
SIG_DFL
);
while
(
pid
==
NULL
)
pid
=
pid_list
;
kill
(
pid
,
18
);
return
0
;
}
int
validate_cd_argv
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
return
0
;
if
(
strcmp
(
argv
[
0
],
"cd"
)
!=
0
)
return
0
;
...
...
This diff is collapsed.
Click to expand it.
src/commands.c
+
54
−
15
View file @
7458f409
...
...
@@ -2,7 +2,7 @@
#include
<stdlib.h>
#include
<string.h>
#include
<assert.h>
#include
<unistd.h>
#include
"commands.h"
#include
"built_in.h"
...
...
@@ -12,6 +12,9 @@ static struct built_in_command built_in_commands[] = {
{
"fg"
,
do_fg
,
validate_fg_argv
}
};
int
pid_list
[
512
];
int
n_pid
=
0
;
static
int
is_built_in_command
(
const
char
*
command_name
)
{
static
const
int
n_built_in_commands
=
sizeof
(
built_in_commands
)
/
sizeof
(
built_in_commands
[
0
]);
...
...
@@ -29,25 +32,53 @@ 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
creation_process
(
const
char
*
Path
,
char
*
const
argv
[]){
pid_t
pid
;
int
background
(
int
argc
,
char
**
argv
){
int
pid
,
buf
;
if
(
!
validate_bg
(
argc
,
argv
))
return
-
1
;
const
char
use_path
=
Path
+
buf
=
process_creation
(
argv
);
if
(
buf
==
-
1
)
return
-
1
;
pid
=
fork
();
if
(
buf
>
0
)
pid
=
buf
;
if
(
pid
>=
0
){
if
(
pid
==
0
)
execv
(
Path
,
argv
);
else
}
else
{
printf
(
"Sorry, Fork failed.
\n
"
);
add_pid
(
pid
);
printf
(
"%d"
,
pid
);
return
1
;
}
int
validate_bg
(
int
argc
,
char
**
argv
){
if
(
argc
!=
1
)
return
0
;
if
(
strcmp
(
argv
[
0
],
"&"
)
!=
0
)
return
0
;
if
(
access
(
argv
[
0
],
F_OK
)
==
0
)
return
0
;
return
1
;
}
int
process_creation
(
struct
single_command
(
*
commands
)[]){
int
pid
;
struct
single_command
*
com
=
(
*
commands
);
char
path
[
4096
];
strcpy
(
path
,(
*
com
).
argv
[
0
]);
pid
=
fork
();
if
(
pid
>=
0
){
if
(
pid
==
0
)
execv
(
path
,(
*
com
).
argv
);
return
pid
;
}
else
{
printf
(
"Sorry, Fork Failed!"
);
return
-
1
;
}
}
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
{
...
...
@@ -70,6 +101,13 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
return
0
;
}
else
if
(
strcmp
(
com
->
argv
[
0
],
"exit"
)
==
0
)
{
return
1
;
}
else
if
(
strcmp
(
com
->
argv
[
n_commands
],
"&"
)
==
0
){
if
(
background
(
com
->
argc
,
com
->
argv
)
==
-
1
)
return
-
1
;
return
0
;
}
else
if
(
process_creation
((
commands
))
==-
1
){
return
0
;
}
else
{
fprintf
(
stderr
,
"%s: command not found
\n
"
,
com
->
argv
[
0
]);
return
-
1
;
...
...
@@ -92,7 +130,8 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
}
free
(
argv
);
}
memset
((
*
commands
),
0
,
sizeof
(
struct
single_command
)
*
n_commands
);
}
}
This diff is collapsed.
Click to expand it.
src/main.c
+
6
−
1
View file @
7458f409
...
...
@@ -5,14 +5,19 @@
#include
"commands.h"
#include
"built_in.h"
#include
"utils.h"
#include
"signal_handlers.h"
#include
<signal.h>
int
main
()
{
char
buf
[
8096
];
while
(
1
)
{
signal
(
SIGINT
,
catch_sigint
);
signal
(
SIGTSTP
,
catch_sigtstp
);
fgets
(
buf
,
8096
,
stdin
);
struct
single_command
commands
[
512
];
int
n_commands
=
0
;
mysh_parse_command
(
buf
,
&
n_commands
,
&
commands
);
...
...
This diff is collapsed.
Click to expand it.
src/signal_handlers.c
+
3
−
3
View file @
7458f409
#include
"signal_handlers.h"
#include
<signal.h>
void
catch_sigint
(
int
signalNo
)
{
while
(
1
)
sleep
(
1
);
signal
(
signalNo
,
SIG_IGN
);
}
void
catch_sigtstp
(
int
signalNo
)
{
signal
(
18
,
SIG_DFL
);
}
This diff is collapsed.
Click to expand it.
src/utils.c
+
2
−
2
View file @
7458f409
...
...
@@ -12,7 +12,7 @@ void mysh_parse_command(const char* command,
strcpy
(
buf
,
command
);
char
*
saveptr
=
NULL
;
char
*
tok
=
strtok_r
(
buf
,
"|"
,
&
saveptr
);
char
*
tok
=
strtok_r
(
buf
,
"|"
,
&
saveptr
);
//pipe
int
ti
=
0
;
...
...
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