-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_files_utils.c
More file actions
71 lines (65 loc) · 1.99 KB
/
create_files_utils.c
File metadata and controls
71 lines (65 loc) · 1.99 KB
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
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_files_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adurusoy <adurusoy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 22:13:07 by adurusoy #+# #+# */
/* Updated: 2023/12/27 23:11:36 by adurusoy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void create_files_utils(t_shell **shell, char *home, t_parse *m_next,
char **pwd)
{
*pwd = ft_strdup(m_next->text[0]);
if (!*pwd)
{
if (home)
free(home);
malloc_error(5, shell);
}
}
void create_files_utils2(t_shell **shell, char **home)
{
*home = get_env((*shell)->env, "HOME");
if (!(*home))
malloc_error(5, shell);
}
void create_files_utils3(t_parse *nparse, char **pwd)
{
if (*pwd)
free(*pwd);
printf("minishell: %s: No such file or directory\n", nparse->text[0]);
}
int create_in_files_me(t_parse *parse, t_shell *shell)
{
t_parse *nparse;
char *pwd;
char *home;
home = NULL;
pwd = NULL;
create_files_utils2(&shell, &home);
nparse = parse->next;
if (!ft_strnstr(nparse->text[0], home, ft_strlen(home)))
handle_relative_path(&pwd, parse);
else
create_files_utils(&shell, home, nparse, &pwd);
if (!access(pwd, F_OK))
nparse->fd = open(pwd, O_RDONLY, 0777);
else
{
create_files_utils3(nparse, &pwd);
return (0);
}
if (pwd)
free(pwd);
free(home);
parse->infile = nparse->fd;
return (1);
}