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
19
int
open_file
(
t_ast
*node,
char
*direction);
20
int
open_tmp_file
(
const
char
*type);
21
34
int
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
52
int
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
}
ft_perror
int ft_perror(char *str)
Definition
exit_status_utils.c:18
exit_status.h
libft.h
ft_strcmp
int ft_strcmp(const char *s1, const char *s2)
Definition
ft_strcmp.c:24
open_file
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition
utils.c:34
open_tmp_file
int open_tmp_file(const char *type)
Definition
utils.c:52
shell.h
s_ast
Definition
tokens.h:50
s_ast::args
char ** args
Definition
tokens.h:52
tokens.h
src
redirection
utils.c
Generated by
1.9.8