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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
HongJiHo
mysh-1
Commits
bbf2b84f
Commit
bbf2b84f
authored
7 years ago
by
HongJiHo
Browse files
Options
Downloads
Patches
Plain Diff
mysh-1 finish
parent
98fbc382
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/built_in.c
+6
-1
6 additions, 1 deletion
src/built_in.c
src/commands.c
+255
-4
255 additions, 4 deletions
src/commands.c
src/main.c
+6
-0
6 additions, 0 deletions
src/main.c
src/signal_handlers.c
+5
-4
5 additions, 4 deletions
src/signal_handlers.c
with
272 additions
and
9 deletions
src/built_in.c
+
6
−
1
View file @
bbf2b84f
...
@@ -36,7 +36,12 @@ int do_fg(int argc, char** argv) {
...
@@ -36,7 +36,12 @@ int do_fg(int argc, char** argv) {
if
(
!
validate_fg_argv
(
argc
,
argv
))
if
(
!
validate_fg_argv
(
argc
,
argv
))
return
-
1
;
return
-
1
;
// TODO: Fill this.
int
pid
=
waitpid
(
0
,
0
,
0
);
if
(
pid
==
-
1
)
printf
(
"no such jobs.
\n
"
);
else
printf
(
"%d done
\n
"
,
pid
);
return
0
;
return
0
;
}
}
...
...
This diff is collapsed.
Click to expand it.
src/commands.c
+
255
−
4
View file @
bbf2b84f
...
@@ -2,16 +2,44 @@
...
@@ -2,16 +2,44 @@
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<assert.h>
#include
<assert.h>
#include
<sys/types.h>
#include
<sys/wait.h>
#include
<sys/socket.h>
#include
<sys/un.h>
#include
<unistd.h>
#include
<pthread.h>
#include
"commands.h"
#include
"commands.h"
#include
"built_in.h"
#include
"built_in.h"
#define SOCK_PATH "tpf_unix_sock.server"
#define CLIENT_PATH "tpf_unix_sock.client"
#define UNIX_PATH_MAX 108
pid_t
pID
;
/*
struct sockaddr_un{
unsigned short int sun_family; //AF_UNIX
char sun_path[UNIX_PATH_MAX]; //pathname
};*/
static
char
PATH
[
5
][
1028
]
=
{
{
"/usr/local/bin/"
},
{
"/usr/bin/"
},
{
"/bin/"
},
{
"/usr/sbin/"
},
{
"/sbin/"
}
};
static
struct
built_in_command
built_in_commands
[]
=
{
static
struct
built_in_command
built_in_commands
[]
=
{
{
"cd"
,
do_cd
,
validate_cd_argv
},
{
"cd"
,
do_cd
,
validate_cd_argv
},
{
"pwd"
,
do_pwd
,
validate_pwd_argv
},
{
"pwd"
,
do_pwd
,
validate_pwd_argv
},
{
"fg"
,
do_fg
,
validate_fg_argv
}
{
"fg"
,
do_fg
,
validate_fg_argv
}
};
};
void
server_thread
();
void
client_thread
(
void
*
command
);
static
int
is_built_in_command
(
const
char
*
command_name
)
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
]);
static
const
int
n_built_in_commands
=
sizeof
(
built_in_commands
)
/
sizeof
(
built_in_commands
[
0
]);
...
@@ -30,11 +58,23 @@ static int is_built_in_command(const char* command_name)
...
@@ -30,11 +58,23 @@ static int is_built_in_command(const char* command_name)
*/
*/
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
int
evaluate_command
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
{
{
if
(
n_commands
>
0
)
{
struct
single_command
*
com
=
(
*
commands
);
struct
single_command
*
com
=
(
*
commands
);
if
(
n_commands
==
1
)
{
//struct single_command* com = (*commands);
assert
(
com
->
argc
!=
0
);
assert
(
com
->
argc
!=
0
);
//background processing
int
background
=
0
;
if
(
strcmp
(
com
->
argv
[
com
->
argc
-
1
],
"&"
)
==
0
){
background
=
1
;
com
->
argv
[
com
->
argc
-
1
]
=
NULL
;
com
->
argc
=
com
->
argc
-
1
;
}
//do built_in_command
int
built_in_pos
=
is_built_in_command
(
com
->
argv
[
0
]);
int
built_in_pos
=
is_built_in_command
(
com
->
argv
[
0
]);
if
(
built_in_pos
!=
-
1
)
{
if
(
built_in_pos
!=
-
1
)
{
if
(
built_in_commands
[
built_in_pos
].
command_validate
(
com
->
argc
,
com
->
argv
))
{
if
(
built_in_commands
[
built_in_pos
].
command_validate
(
com
->
argc
,
com
->
argv
))
{
...
@@ -50,12 +90,222 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
...
@@ -50,12 +90,222 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
}
else
if
(
strcmp
(
com
->
argv
[
0
],
"exit"
)
==
0
)
{
}
else
if
(
strcmp
(
com
->
argv
[
0
],
"exit"
)
==
0
)
{
return
1
;
return
1
;
}
else
{
}
else
{
//process creation
pID
=
fork
();
//fork error
if
(
pID
==
-
1
){
fprintf
(
stderr
,
"fork fail
\n
"
);
}
//child process
else
if
(
pID
==
0
){
path_exec
(
com
);
fprintf
(
stderr
,
"%s: command not found
\n
"
,
com
->
argv
[
0
]);
fprintf
(
stderr
,
"%s: command not found
\n
"
,
com
->
argv
[
0
]);
return
-
1
;
exit
(
0
);
}
//parent process
else
{
if
(
background
==
1
)
printf
(
"background pid : %d
\n
"
,
pID
);
else
waitpid
(
pID
,
0
,
0
);
}
return
0
;
}
}
//IPC
else
if
(
n_commands
>=
2
){
void
*
status
;
pthread_t
threads
[
2
];
int
pid
=
fork
();
if
(
pid
==
0
){
for
(
int
l
=
0
;
l
<
n_commands
;
l
++
){
pthread_create
(
&
threads
[
1
],
NULL
,
&
client_thread
,
(
void
*
)
com
);
pthread_join
(
threads
[
1
],
&
status
);
com
++
;
}
exit
(
0
);
}
else
{
pthread_create
(
&
threads
[
0
],
NULL
,
&
server_thread
,
(
void
*
)
&
n_commands
);
waitpid
(
pid
,
NULL
,
0
);
pthread_join
(
threads
[
0
],
NULL
);
}
return
0
;
}
return
0
;
}
void
path_exec
(
struct
single_command
*
com
){
if
(
execv
(
com
->
argv
[
0
],
com
->
argv
)
==
-
1
){
//need path resoultion
char
*
tmp
=
com
->
argv
[
0
];
for
(
int
k
=
0
;
k
<
5
;
k
++
){
com
->
argv
[
0
]
=
strcat
(
PATH
[
k
],
tmp
);
if
(
execv
(
com
->
argv
[
0
],
com
->
argv
)
==
-
1
)
com
->
argv
[
0
]
=
tmp
;
else
break
;
}
}
}
void
server_thread
(){
int
server_sock
,
client_sock
,
len
;
struct
sockaddr_un
server_sockaddr
;
struct
sockaddr_un
client_sockaddr
;
char
buf
[
256
];
memset
(
&
server_sockaddr
,
0
,
sizeof
(
struct
sockaddr_un
));
memset
(
&
client_sockaddr
,
0
,
sizeof
(
struct
sockaddr_un
));
memset
(
buf
,
0
,
256
);
//create domain stream socket
server_sock
=
socket
(
AF_UNIX
,
SOCK_STREAM
,
0
);
if
(
server_sock
==
-
1
){
printf
(
"SOCKET ERROR
\n
"
);
exit
(
1
);
}
//bind
server_sockaddr
.
sun_family
=
AF_UNIX
;
strcpy
(
server_sockaddr
.
sun_path
,
SOCK_PATH
);
len
=
sizeof
(
server_sockaddr
);
unlink
(
SOCK_PATH
);
if
(
bind
(
server_sock
,
(
struct
sockaddr
*
)
&
server_sockaddr
,
len
)
==
-
1
){
printf
(
"BIND ERROR
\n
"
);
close
(
server_sock
);
exit
(
1
);
}
//listen
if
(
listen
(
server_sock
,
10
)
==
-
1
){
printf
(
"LISTEN ERROR
\n
"
);
close
(
server_sock
);
exit
(
1
);
}
//accept
client_sock
=
accept
(
server_sock
,
(
struct
sockaddr
*
)
&
client_sockaddr
,
&
len
);
if
(
client_sock
==
-
1
){
printf
(
"ACCEPT ERROR
\n
"
);
close
(
server_sock
);
close
(
client_sock
);
exit
(
1
);
}
}
dup2
(
client_sock
,
STDIN_FILENO
);
int
h
=
2
;
while
(
h
--
){
//send data
memset
(
buf
,
0
,
256
);
if
(
send
(
client_sock
,
buf
,
strlen
(
buf
),
0
)
==
-
1
){
printf
(
"SEND ERROR
\n
"
);
close
(
server_sock
);
close
(
client_sock
);
exit
(
1
);
}
}
//read and print
if
(
recv
(
client_sock
,
buf
,
sizeof
(
buf
),
0
)
==
-
1
){
printf
(
"REC ERROR
\n
"
);
close
(
server_sock
);
close
(
client_sock
);
exit
(
1
);
}
else
printf
(
"DATA RECEIVED = %s
\n
"
,
buf
);
}
//close
close
(
server_sock
);
close
(
client_sock
);
return
0
;
return
0
;
}
void
client_thread
(
void
*
command
){
int
client_sock
,
len
;
struct
sockaddr_un
server_sockaddr
;
struct
sockaddr_un
client_sockaddr
;
char
buf
[
256
];
memset
(
&
server_sockaddr
,
0
,
sizeof
(
struct
sockaddr_un
));
memset
(
&
client_sockaddr
,
0
,
sizeof
(
struct
sockaddr_un
));
//create
client_sock
=
socket
(
AF_UNIX
,
SOCK_STREAM
,
0
);
if
(
client_sock
==
-
1
){
printf
(
"SOCKET ERROR
\n
"
);
exit
(
1
);
}
//bind
client_sockaddr
.
sun_family
=
AF_UNIX
;
strcpy
(
client_sockaddr
.
sun_path
,
CLIENT_PATH
);
len
=
sizeof
(
client_sockaddr
);
unlink
(
CLIENT_PATH
);
if
(
bind
(
client_sock
,
(
struct
sockaddr
*
)
&
client_sockaddr
,
len
)
==
-
1
){
printf
(
"BIND ERROR
\n
"
);
close
(
client_sock
);
exit
(
1
);
}
//set up
server_sockaddr
.
sun_family
=
AF_UNIX
;
strcpy
(
server_sockaddr
.
sun_path
,
SOCK_PATH
);
if
(
connect
(
client_sock
,
(
struct
sockaddr
*
)
&
server_sockaddr
,
len
)
==
-
1
){
printf
(
"CONNECT ERROR
\n
"
);
close
(
client_sock
);
exit
(
1
);
}
void
*
status
;
int
tempfd
=
dup
(
STDOUT_FILENO
);
int
pid
=
fork
();
if
(
pid
==
0
){
path_exec
(
command
);
exit
(
0
);
}
else
{
waitpid
(
-
1
,
&
status
,
0
);
dup2
(
tempfd
,
STDOUT_FILENO
);
close
(
tempfd
);
}
int
h
=
2
;
while
(
h
--
){
//send
if
(
send
(
client_sock
,
buf
,
strlen
(
buf
),
0
)
==
-
1
){
printf
(
"SEND ERROR
\n
"
);
close
(
client_sock
);
exit
(
1
);
}
//Read
memset
(
buf
,
0
,
sizeof
(
buf
));
if
(
recv
(
client_sock
,
buf
,
sizeof
(
buf
),
0
)
==
-
1
){
printf
(
"RECV ERROR
\n
"
);
close
(
client_sock
);
exit
(
1
);
}
else
printf
(
"DATA RECEIVED = %s
\n
"
,
buf
);
}
//close
close
(
client_sock
);
return
0
;
}
}
void
free_commands
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
void
free_commands
(
int
n_commands
,
struct
single_command
(
*
commands
)[
512
])
...
@@ -74,3 +324,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
...
@@ -74,3 +324,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
memset
((
*
commands
),
0
,
sizeof
(
struct
single_command
)
*
n_commands
);
memset
((
*
commands
),
0
,
sizeof
(
struct
single_command
)
*
n_commands
);
}
}
This diff is collapsed.
Click to expand it.
src/main.c
+
6
−
0
View file @
bbf2b84f
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<signal.h>
#include
"commands.h"
#include
"commands.h"
#include
"built_in.h"
#include
"built_in.h"
#include
"utils.h"
#include
"utils.h"
#include
"signal_handlers.h"
int
main
()
int
main
()
{
{
char
buf
[
8096
];
char
buf
[
8096
];
catch_sigint
(
SIGINT
);
catch_sigtstp
(
SIGTSTP
);
while
(
1
)
{
while
(
1
)
{
printf
(
"$ "
);
fgets
(
buf
,
8096
,
stdin
);
fgets
(
buf
,
8096
,
stdin
);
struct
single_command
commands
[
512
];
struct
single_command
commands
[
512
];
...
@@ -25,6 +30,7 @@ int main()
...
@@ -25,6 +30,7 @@ int main()
if
(
ret
==
1
)
{
if
(
ret
==
1
)
{
break
;
break
;
}
}
memset
(
buf
,
0
,
sizeof
(
buf
));
}
}
return
0
;
return
0
;
...
...
This diff is collapsed.
Click to expand it.
src/signal_handlers.c
+
5
−
4
View file @
bbf2b84f
#include
<signal.h>
#include
"signal_handlers.h"
#include
"signal_handlers.h"
void
catch_sigint
(
int
signalNo
)
void
catch_sigint
(
int
signalNo
)
//process interrupt
{
{
// TODO: File this!
signal
(
signalNo
,
SIG_IGN
);
}
}
void
catch_sigtstp
(
int
signalNo
)
void
catch_sigtstp
(
int
signalNo
)
//process stop
{
{
// TODO: File this!
signal
(
signalNo
,
SIG_IGN
);
}
}
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