Skip to content
Snippets Groups Projects
Commit 0d7b2f84 authored by 강병수's avatar 강병수
Browse files

done

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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;
}
}
......
......@@ -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);
......
#include <stdlib.h>
#include <unistd.h>
#include "signal_handlers.h"
void catch_sigint(int signalNo)
......
......@@ -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*));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment