maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils.c File Reference
#include "shell.h"
#include <fcntl.h>
#include "libft.h"
#include "tokens.h"
#include "exit_status.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Functions

int open_file (t_ast *node, char *direction)
 open file in the context of redirection
 
int open_tmp_file (const char *type)
 

Function Documentation

◆ open_file()

int open_file ( t_ast node,
char *  direction 
)

open file in the context of redirection

-- "<" - read

  • ">" - write
  • ">>" - append
  • "tty" - open /dev/tty
  • Parameters
    nodecurrent node in the AST
  • Parameters
    directiontype of redirection
  • Returns
    int file descriptor

Definition at line 34 of file utils.c.

35{
36 int fd;
37
38 if ((ft_strcmp(direction, "<") == 0) || (ft_strcmp(direction, "read") == 0))
39 fd = open(node->args[0], O_RDONLY);
40 else if (ft_strcmp(direction, ">") == 0)
41 fd = open(node->args[0], O_WRONLY | O_CREAT | O_TRUNC, 0644);
42 else if ((ft_strcmp(direction, ">>") == 0) \
43 || (ft_strcmp(direction, "temp") == 0))
44 fd = open(node->args[0], O_WRONLY | O_CREAT | O_APPEND, 0644);
45 else if (ft_strcmp(direction, "tty") == 0)
46 fd = open("/dev/tty", O_RDWR);
47 else
48 fd = -1;
49 return (fd);
50}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char ** args
Definition tokens.h:52

References s_ast::args, and ft_strcmp().

Referenced by open_and_redirect(), redirect_append(), and redirect_in().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_tmp_file()

int open_tmp_file ( const char *  type)

Definition at line 52 of file utils.c.

53{
54 int file_fd;
55
56 file_fd = -1;
57 if (ft_strcmp(type, "w") == 0)
58 file_fd = open("/tmp/heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
59 else if (ft_strcmp(type, "r") == 0)
60 file_fd = open("/tmp/heredoc", O_RDONLY);
61 if (file_fd < 0)
62 ft_perror("open");
63 return (file_fd);
64}
int ft_perror(char *str)

References ft_perror(), and ft_strcmp().

Referenced by redirect_here_doc().

Here is the call graph for this function:
Here is the caller graph for this function: