maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/11 14:33:02 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 13:49:49 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "shell.h"
14#include <fcntl.h>
15#include "libft.h"
16#include "tokens.h"
17#include "exit_status.h"
18
19int open_file(t_ast *node, char *direction);
20int open_tmp_file(const char *type);
21
34int open_file(t_ast *node, char *direction)
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}
51
52int open_tmp_file(const char *type)
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)
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition utils.c:34
int open_tmp_file(const char *type)
Definition utils.c:52
Definition tokens.h:50
char ** args
Definition tokens.h:52