Skip to content
Snippets Groups Projects
Commit d65569e7 authored by 김정환's avatar 김정환
Browse files

mysh-1

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ int do_fg(int argc, char** argv) { ...@@ -38,6 +38,7 @@ int do_fg(int argc, char** argv) {
// TODO: Fill this. // TODO: Fill this.
return 0; return 0;
} }
......
...@@ -2,9 +2,18 @@ ...@@ -2,9 +2,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <paths.h>
#define SOCK_PATH "tpf_unix_sock.server"
#define DATA "Hello from server"
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 },
...@@ -12,6 +21,8 @@ static struct built_in_command built_in_commands[] = { ...@@ -12,6 +21,8 @@ static struct built_in_command built_in_commands[] = {
{ "fg", do_fg, validate_fg_argv } { "fg", do_fg, validate_fg_argv }
}; };
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,31 +41,146 @@ static int is_built_in_command(const char* command_name) ...@@ -30,31 +41,146 @@ 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])
{ {
pid_t pid;
// int server_sock, client_sock, len, rc;
// int bytes_rec = 0;
// struct sockaddr_un server_sockaddr;
// struct sockaddr_un client_sockaddr;
// char buf[256];
// int backlog = 10;
// memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
// memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
// memset(buf, 0, 256);
// //create unix domain stream socket
// server_sock = sockt(AF_UNIX, SOCK_STREAM, 0);
// if (server_sock == -1)
// {
// printf("SOCKET ERROR: %d\n", sock_errno());
// exit(1);
// }
// //set up the sockaddr structure, giving it a filepath to bind to
// server_sockaddr.sun_family = AF_UNIX;
// strcpy(server_sockaddr.sun_path, SOCK_PATH);
// len = sizeof(server_sockaddr);
// unlink(SOCK_PATH);
// rc = bind(server_sock, (struct sockaddr *) &server_sockaddr, len);
// if (rc == -1)
// {
// printf("BIND ERROR = %d", sock_errno());
// close(server_sock);
// exit(1);
// }
// //read data on the server from clients and print the data that was read
// printf("waiting to recvfrom...\n");
// bytes_rec = recvfrom(server_sock, buf, 256, 0, (struct sockaddr *) &peer_sock, &len);
// if(bytes_rec = -1)
// {
// printf("RECVFROM ERROR = %d", sock_errno());
// close(server_sock);
// exit(1);
// }
// else
// {
// printf("DATA RECEIVED = %s\n", buf);
// }
// //close the socket and exit
// close(server_sock);
if (n_commands > 0) { if (n_commands > 0) {
struct single_command* com = (*commands); struct single_command* com = (*commands);
assert(com->argc != 0); assert(com->argc != 0);
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_do(com->argc, com->argv) != 0) { if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv))
{
if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0)
{
fprintf(stderr, "%s: Error occurs\n", com->argv[0]); fprintf(stderr, "%s: Error occurs\n", com->argv[0]);
} }
} else { }
else
{
fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]);
return -1; return -1;
} }
} else if (strcmp(com->argv[0], "") == 0) {
}
else if (strcmp(com->argv[0], "") == 0)
{
return 0; return 0;
} else if (strcmp(com->argv[0], "exit") == 0) { }
else if (strcmp(com->argv[0], "exit") == 0)
{
return 1; return 1;
} else { }
else
{
pid = fork();
int status = 0;
if(pid < 0)
{
fprintf(stderr, "Fork failed.");
return 0;
}
else if(pid == 0)
{
// char* path = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:";
// char* saveptr = NULL;
// char* tok = strtok_r(path, ":", &saveptr);
// printf("%s",tok);
// while( tok != NULL)
// {
// char* buf;
// strcpy(buf, tok);
// strcat(buf, "/");
// strcat(buf, com->argv[0]);
// execv(buf, com->argv);
// tok = strtok_r(NULL, ":", &saveptr);
// }
execv(com->argv[0], com->argv);
fprintf(stderr, "%s: command not found\n", com->argv[0]); fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1; exit(1);
} }
else
{
wait(&status);
return 0;
}
} }
}
return 0; return 0;
} }
......
...@@ -6,11 +6,21 @@ ...@@ -6,11 +6,21 @@
#include "built_in.h" #include "built_in.h"
#include "utils.h" #include "utils.h"
#include "signal_handlers.h"
#include <signal.h>
#include <sys/types.h>
#include <signal_handlers.h>
int main() int main()
{ {
char buf[8096]; char buf[8096];
while (1) { while (1) {
signal(SIGINT, (void*)catch_sigint);
signal(SIGTSTP, (void*)catch_sigtstp);
memset(buf, NULL,8096);
fgets(buf, 8096, stdin); fgets(buf, 8096, stdin);
struct single_command commands[512]; struct single_command commands[512];
...@@ -25,6 +35,7 @@ int main() ...@@ -25,6 +35,7 @@ int main()
if (ret == 1) { if (ret == 1) {
break; break;
} }
} }
return 0; return 0;
......
#include "signal_handlers.h" #include "signal_handlers.h"
#include <signal.h>
#include <sys/types.h>
#include <signal_handlers.h>
void catch_sigint(int signalNo) void catch_sigint(int signalNo)
{ {
// TODO: File this! // TODO: File this!
return 1;
} }
void catch_sigtstp(int signalNo) void catch_sigtstp(int signalNo)
{ {
// TODO: File this! // TODO: File this!
return 1;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment