Parcourir la source

added check redirection func

master
10195501441 il y a 3 ans
Parent
révision
002294fd36
3 fichiers modifiés avec 43 ajouts et 58 suppressions
  1. BIN
      yeeshell
  2. +33
    -48
      yeeshell.c
  3. +10
    -10
      yeeshell.h

BIN
yeeshell Voir le fichier


+ 33
- 48
yeeshell.c Voir le fichier

@ -8,8 +8,6 @@
#include <dirent.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include "yeeshell.h"
/* record cmdline history */
@ -66,7 +64,6 @@ int main()
{
free(history[i]);
}
free(history);
exit(EXIT_SUCCESS);
}
@ -140,36 +137,56 @@ int parseline(char *cmdline, char **args)
return bg;
}
int redirect(char **args, char *redirect_file, char **redirect_args)
int check_redirect(char **args, char *redirect_filename, char **redirect_args)
{
int i = 0;
int i = 0, j = 0, redirect_flag = REDIRECT_NO;
while (args[i] != NULL)
{
redirect_args[i] = args[i];
if (args[i + 1] == ">")
if (!strcmp(args[i], ">"))
{
redirect_file = args[i + 2];
return OUT_REDRT;
redirect_flag = REDIRECT_OUT;
break;
}
else if (args[i + 1] == "<")
else if (!strcmp(args[i], "<"))
{
redirect_file = args[i + 2];
return IN_REDRT;
redirect_flag = REDIRECT_IN;
break;
}
i++;
}
return NO_REDRT;
if (redirect_flag == 1) /* redirect output */
{
redirect_filename = args[i + 1];
for (j = 0; j < i; j++)
{
redirect_args[i] = args[i];
}
}
else if (redirect_flag == 2) /* redirect input */
{
redirect_filename = args[0];
i++;
while (args[i] != NULL)
{
redirect_args[j++] = args[i];
}
}
return redirect_flag;
}
int execute(char *cmdline, char **args)
{
int bg = 0, redirect_flag = 0, fd = 0;
char *redirect_file, *redirect_args[ARGS_MAX_QUANTITY];
int bg = 0, i = 0, redirect_flag = 0;
pid_t pid;
char *redirect_filename;
char *redirect_args[ARGS_MAX_QUANTITY];
sigset_t mask_all, mask_prev;
sigprocmask(SIG_BLOCK, NULL, &mask_all);
sigaddset(&mask_all, SIGCHLD);
bg = parseline(cmdline, args);
redirect_flag = redirect(args, redirect_file, redirect_args);
redirect_flag = check_redirect(args, redirect_filename, redirect_args);
printf("%d %s %s\n", redirect_flag, redirect_filename, redirect_args);
if (args[0] == NULL)
{
@ -189,38 +206,6 @@ int execute(char *cmdline, char **args)
/* Set the pid of the current process to the group number of the process group it belongs to. */
/* avoid being grouped with tsh */
setpgid(0, 0);
switch (redirect_flag)
{
case 0: /* no redirection */
if (execvp(args[0], args) <= 0)
{
printf("%s: Command not found\n", args[0]);
exit(0);
}
break;
case 1: /* redirect output */
fd = open(redirect_file, O_CREAT | O_WRONLY | O_TRUNC, 0664);
dup(fd, 1);
if (execvp(args[0], redirect_args) <= 0)
{
printf("%s: Command not found\n", args[0]);
exit(0);
}
break;
case 2: /* redirect input */
fd = open(info.in_file, O_CREAT | O_RDONLY, 0666);
close(fileno(stdin));
dup2(fd, fileno(stdin));
close(fd);
if (execvp(args[0], redirect_args) <= 0)
{
printf("%s: Command not found\n", args[0]);
exit(0);
}
break;
}
if (execvp(args[0], args) <= 0)
{
printf("%s: Command not found\n", args[0]);

+ 10
- 10
yeeshell.h Voir le fichier

@ -2,7 +2,7 @@
#define CMDLINE_MAX_SIZE 1024 /* max length of a single command line */
#define ARGS_MAX_QUANTITY 128 /* max args on a command line */
#define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */
#define BUFFER_MAX_SIZE 64 /* max size of a buffer which contains parsed arguments */
#define CMDLINE_DIV ' \t\r\n\a'
#define CMDLINE_HISTORY_MAX_QUANTITY 256
#define JOBS_MAX_QUANTITY 16
@ -14,17 +14,17 @@
#define BG 2 /* running in background */
#define ST 3 /* stopped */
#define NO_REDRT 0 /* no redirection */
#define OUT_REDRT 1 /* the latter is output */
#define IN_REDRT 2 /* the former is input */
#define REDIRECT_NO 0 /* no redirection */
#define REDIRECT_OUT 1 /* redirect output */
#define REDIRECT_IN 2 /* redirect input */
/* job_t - The job struct */
struct job_t
{
pid_t pid; /* job PID */
int jid; /* job ID [1, 2, ...] */
int state; /* UNDEF, BG, FG, or ST */
char cmdline[CMDLINE_MAX_SIZE]; /* command line */
pid_t pid; /* job PID */
int jid; /* job ID [1, 2, ...] */
int state; /* UNDEF, BG, FG, or ST */
char cmdline[CMDLINE_MAX_SIZE]; /* command line */
};
/* readline - Get the command line */
@ -33,8 +33,8 @@ char *readline();
/* parseline - Evaluate the command line that the user has just typed in */
int parseline(char *cmdline, char **args);
/* redirect - Judge if the command contains redirection */
int redirect(char **args, char *redirect_file, char **redirect_args);
/* check_redirect - check if the command contains redirection */
int check_redirect(char **args, char *redirect_filename, char **redirect_args);
/* execute - Execute the command line */
int execute(char *cmdline, char **args);

Chargement…
Annuler
Enregistrer