changed buildsystem to automake
This commit is contained in:
+77
-76
@@ -1,81 +1,82 @@
|
||||
#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;
|
||||
#include "include/redirection.h"
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void detectRedirections(char **args, Redirection *in, Redirection *out) {
|
||||
in->type = REDIRECT_NONE;
|
||||
in->filename = NULL;
|
||||
out->type = REDIRECT_NONE;
|
||||
out->filename = NULL;
|
||||
|
||||
int j = 0;
|
||||
for (int i = 0; args[i] != NULL; i++) {
|
||||
if (strcmp(args[i], "<") == 0) {
|
||||
if (args[i + 1] != NULL) {
|
||||
in->type = REDIRECT_IN;
|
||||
in->filename = args[i+1];
|
||||
i++; // skip filename
|
||||
}
|
||||
} else if (strcmp(args[i], ">") == 0) {
|
||||
if (args[i + 1] != NULL) {
|
||||
out->type = REDIRECT_OUT;
|
||||
out->filename = args[i+1];
|
||||
i++;
|
||||
}
|
||||
} else if (strcmp(args[i], ">>") == 0) {
|
||||
if (args[i + 1] != NULL) {
|
||||
out->type = REDIRECT_APPEND;
|
||||
out->filename = args[i+1];
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
args[j++] = args[i];
|
||||
}
|
||||
}
|
||||
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;
|
||||
args[j] = NULL;
|
||||
}
|
||||
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);
|
||||
|
||||
int applyRedirections(Redirection in, Redirection out) {
|
||||
if (in.type == REDIRECT_IN) {
|
||||
int fd = open(in.filename, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
perror("open input");
|
||||
return -1;
|
||||
}
|
||||
if (dup2(fd, STDIN_FILENO) < 0) {
|
||||
perror("dup2 input");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
else if(pid > 0) {
|
||||
close(fd);
|
||||
wait(NULL);
|
||||
|
||||
if (out.type == REDIRECT_OUT) {
|
||||
int fd = open(out.filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) {
|
||||
perror("open output");
|
||||
return -1;
|
||||
}
|
||||
if (dup2(fd, STDOUT_FILENO) < 0) {
|
||||
perror("dup2 output");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
} else if (out.type == REDIRECT_APPEND) {
|
||||
int fd = open(out.filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
|
||||
if (fd < 0) {
|
||||
perror("open output append");
|
||||
return -1;
|
||||
}
|
||||
if (dup2(fd, STDOUT_FILENO) < 0) {
|
||||
perror("dup2 output append");
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user