added input redirection
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef REDIRECTION_H
|
||||||
|
#define REDIRECTION_H
|
||||||
|
|
||||||
|
|
||||||
|
int detectRedirect(char **args, char **filename);
|
||||||
|
void executeRedirect(char **args, int type, char *filename);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include<readline/readline.h>
|
#include<readline/readline.h>
|
||||||
#include<readline/history.h>
|
#include<readline/history.h>
|
||||||
#include<signal.h>
|
#include<signal.h>
|
||||||
|
#include"include/redirection.h"
|
||||||
pid_t backgroundPids[64];
|
pid_t backgroundPids[64];
|
||||||
int backgroundCount = 0;
|
int backgroundCount = 0;
|
||||||
void handleSIGINT(int sig) {
|
void handleSIGINT(int sig) {
|
||||||
@@ -112,6 +113,14 @@ int main() {
|
|||||||
free(parsed);
|
free(parsed);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
char *filename;
|
||||||
|
int redirectionType = detectRedirect(parsed, &filename);
|
||||||
|
if (redirectionType) {
|
||||||
|
executeRedirect(parsed, redirectionType, filename);
|
||||||
|
free(parsed);
|
||||||
|
free(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if(pid == 0) {
|
if(pid == 0) {
|
||||||
signal(SIGINT, SIG_DFL);
|
signal(SIGINT, SIG_DFL);
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ CC = gcc
|
|||||||
CFLAGS = -Wall
|
CFLAGS = -Wall
|
||||||
LIBS = -lreadline
|
LIBS = -lreadline
|
||||||
|
|
||||||
tinysh: main.c
|
tinysh: main.c redirection.c
|
||||||
$(CC) $(CFLAGS) main.c -o tinysh $(LIBS)
|
$(CC) $(CFLAGS) main.c redirection.c -o tinysh $(LIBS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f tinysh
|
rm -f tinysh
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#include"include/redirection.h"
|
||||||
|
#include<string.h>
|
||||||
|
#include<fcntl.h>
|
||||||
|
#include<unistd.h>
|
||||||
|
#include<sys/wait.h>
|
||||||
|
#include<signal.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
int detectRedirect(char **args, char **filename) {
|
||||||
|
for(int i = 0; args[i] != NULL; i++) {
|
||||||
|
if(strcmp(args[i], "<") == 0) {
|
||||||
|
*filename = args[i + 1];
|
||||||
|
args[i] = NULL;
|
||||||
|
args[i + 1] = NULL;
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
if(strcmp(args[i], ">") == 0) {
|
||||||
|
*filename = args[i + 1];
|
||||||
|
args[i] = NULL;
|
||||||
|
args[i + 1] = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(strcmp(args[i], ">>") == 0) {
|
||||||
|
*filename = args[i + 1];
|
||||||
|
args[i] = NULL;
|
||||||
|
args [i + 1] = NULL;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void executeRedirect(char **args, int type, char *filename) {
|
||||||
|
if (type == 1) {
|
||||||
|
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
|
pid_t pid = fork();
|
||||||
|
if(pid == 0) {
|
||||||
|
dup2(fd, STDOUT_FILENO);
|
||||||
|
close(fd);
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
execvp(args[0], args);
|
||||||
|
perror("tinysh");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else if(pid > 0) {
|
||||||
|
close(fd);
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type == 2) {
|
||||||
|
int fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
||||||
|
pid_t pid = fork();
|
||||||
|
if(pid == 0) {
|
||||||
|
dup2(fd, STDOUT_FILENO);
|
||||||
|
close(fd);
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
execvp(args[0], args);
|
||||||
|
perror("tinysh");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (pid > 0) {
|
||||||
|
close(fd);
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type == 3) {
|
||||||
|
int fd = open(filename, O_RDONLY);
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
|
dup2(fd, STDIN_FILENO);
|
||||||
|
close(fd);
|
||||||
|
signal(SIGINT, SIG_DFL);
|
||||||
|
execvp(args[0], args);
|
||||||
|
perror("tinysh");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else if (pid > 0) {
|
||||||
|
close(fd);
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user