blob: 3293972b0d6f12edaa5015a9eae47bd6973ac1d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/**
** \file val.c
** \brief Evaluation value manipulation
*/
#include "ms_evalue.h"
/**
** \brief Allocate memory for a t_value and set his type
** \param type Type of valueue
** \return The allocated value
*/
t_value *value_new(t_value_type type)
{
t_value *value;
if ((value = (t_value*)malloc(sizeof(t_value*))) == NULL)
return (NULL);
value->type = type;
return (value);
}
/**
** \brief Create a new redirection value from a filename
** \param type Type of redirection and value
** \param filename Name of the file to open
** \return Redirection value
** \warning Undefined behavior on none redirection type.
*/
t_value *value_new_redir(t_value_type type, char *filename)
{
t_value *value;
if ((value = value_new(type)) == NULL)
return (NULL);
if (type == VAL_REDIR_IN)
value->data.fd = open(filename, O_RDONLY);
else if (type == VAL_REDIR_OUT)
value->data.fd = open(filename, O_WRONLY | O_CREAT);
else if (type == VAL_REDIR_APPEND)
value->data.fd = open(filename, O_APPEND | O_CREAT);
if (value->data.fd < 0)
{
free(value);
return (NULL);
}
return (value);
}
/**
** \brief Create a new string value (i.e error, arg, cmd)
** \param type String value type
** \param str String data
** \return String value
*/
t_value *value_new_string(t_value_type type, char *str)
{
t_value *value;
if ((value == malloc(sizeof(t_value))) == NULL)
return (NULL);
value->type = type;
if ((value->data.str = ft_strdup(str)) == NULL)
return (NULL);
return (value);
}
|