fix: now compiles and works for simple commands + clang format

This commit is contained in:
matteo 2026-01-17 19:11:55 +01:00
parent 6099bbb9e3
commit 352c122549
23 changed files with 168 additions and 150 deletions

View file

@ -1,13 +1,13 @@
#include "execution.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "../utils/ast/ast.h"
@ -70,7 +70,7 @@ static int builtin_echo(char **argv)
}
if (newline)
printf("\n");
fflush(stdout);
return 0;
}
@ -120,7 +120,7 @@ static int try_builtin(char **argv)
{
if (!argv || !argv[0])
return 0;
if (strcmp(argv[0], "echo") == 0)
return builtin_echo(argv);
if (strcmp(argv[0], "true") == 0)
@ -242,7 +242,7 @@ int execution(struct ast *ast)
case AST_AND_OR: {
struct ast_and_or *ao_node = ast_get_and_or(ast);
int left_ret = execution(ao_node->left);
if (ao_node->type == AST_AND_OR_TYPE_AND)
{
if (left_ret == 0)
@ -258,38 +258,42 @@ int execution(struct ast *ast)
}
case AST_REDIR: {
struct ast_redir *redir = ast_get_redir(ast);
int fd_target = redir->io_number;
if (fd_target == -1)
{
if (redir->type == AST_REDIR_TYPE_LESS || redir->type == AST_REDIR_TYPE_DLESS || redir->type == AST_REDIR_TYPE_LESSAND)
if (redir->type == AST_REDIR_TYPE_LESS
|| redir->type == AST_REDIR_TYPE_DLESS
|| redir->type == AST_REDIR_TYPE_LESSAND)
fd_target = 0;
else
fd_target = 1;
}
int saved_fd = dup(fd_target);
int new_fd = -1;
int flags = 0;
int mode = 0644;
if (redir->type == AST_REDIR_TYPE_GREAT || redir->type == AST_REDIR_TYPE_CLOBBER)
if (redir->type == AST_REDIR_TYPE_GREAT
|| redir->type == AST_REDIR_TYPE_CLOBBER)
{
flags = O_WRONLY | O_CREAT | O_TRUNC;
new_fd = open(redir->filename, flags, mode);
}
else if (redir->type == AST_REDIR_TYPE_DGREAT)
{
flags = O_WRONLY | O_CREAT | O_APPEND;
new_fd = open(redir->filename, flags, mode);
flags = O_WRONLY | O_CREAT | O_APPEND;
new_fd = open(redir->filename, flags, mode);
}
else if (redir->type == AST_REDIR_TYPE_LESS)
{
flags = O_RDONLY;
new_fd = open(redir->filename, flags);
flags = O_RDONLY;
new_fd = open(redir->filename, flags);
}
else if (redir->type == AST_REDIR_TYPE_GREATAND || redir->type == AST_REDIR_TYPE_LESSAND)
else if (redir->type == AST_REDIR_TYPE_GREATAND
|| redir->type == AST_REDIR_TYPE_LESSAND)
{
// Simple fd duplication
new_fd = atoi(redir->filename);
@ -297,16 +301,18 @@ int execution(struct ast *ast)
if (dup2(new_fd, fd_target) == -1)
{
perror("dup2");
if (saved_fd != -1) close(saved_fd);
if (saved_fd != -1)
close(saved_fd);
return 1;
}
new_fd = -2; // Mark as "already duped"
}
if (new_fd == -1)
{
perror("open");
if (saved_fd != -1) close(saved_fd);
if (saved_fd != -1)
close(saved_fd);
return 1;
}
@ -316,7 +322,8 @@ int execution(struct ast *ast)
{
perror("dup2");
close(new_fd);
if (saved_fd != -1) close(saved_fd);
if (saved_fd != -1)
close(saved_fd);
return 1;
}
close(new_fd);