42sh/src/utils/ast/ast_redir.c

44 lines
1 KiB
C

#include "ast_redir.h"
#include <stdio.h>
#include <stdlib.h>
struct ast_redir *ast_get_redir(struct ast *node)
{
if (node == NULL || node->type != AST_REDIR)
return NULL;
return node->data;
}
struct ast *ast_create_redir(char *filename, int io_number,
enum ast_redir_type type)
{
struct ast_redir *redir_node = malloc(sizeof(struct ast_redir));
if (redir_node == NULL)
{
perror("Error: could not allocate more memory");
return NULL;
}
redir_node->filename =
filename; // Takes ownership? Usually yes in simple ASTs, or dup. Let's
// assume pointer copy for now, but user must manage memory.
redir_node->io_number = io_number;
redir_node->type = type;
struct ast *result = ast_create(AST_REDIR, redir_node);
if (result == NULL)
free(redir_node);
return result;
}
void ast_free_redir(struct ast_redir *redir)
{
if (redir == NULL)
return;
if (redir->filename != NULL)
free(redir->filename);
free(redir);
}