-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_utils.c
102 lines (93 loc) · 2.48 KB
/
command_utils.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* :::::::: */
/* command_utils.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/03/07 13:08:00 by jvan-hal #+# #+# */
/* Updated: 2023/03/24 16:42:06 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include"pipex.h"
char *get_path(char **paths, char *command)
{
int i;
char *error;
char *path;
char *commandext;
if (access(command, F_OK) == 0)
return (command);
commandext = ft_strjoin("/", command);
i = 0;
while (paths && paths[i])
{
path = ft_strjoin(paths[i], commandext);
if (access(path, F_OK) == 0)
{
free(commandext);
return (path);
}
free(path);
++i;
}
error = ft_strjoin(command, ": command not found\n");
write(2, "pipex: ", 7);
write(2, error, ft_strlen(error));
free(error);
exit(127);
}
char **ft_env_backup(void)
{
char **env;
env = (char **)ft_calloc(8, sizeof(char *));
if (!env)
return (NULL);
env[0] = ft_strdup("/usr/local/bin");
env[1] = ft_strdup("/usr/bin");
env[2] = ft_strdup("/bin");
env[3] = ft_strdup("/usr/sbin");
env[4] = ft_strdup("/sbin");
env[5] = ft_strdup("/usr/local/munki");
env[6] = ft_strdup("/opt/X11/bin");
return (env);
}
char **get_paths(char **env)
{
char **split;
char *paths;
int i;
i = 0;
while (env[i])
{
if (ft_strnstr(env[i], "PATH", 4))
{
paths = ft_strtrim(env[i], "PATH=");
split = ft_split(paths, ':');
free(paths);
return (split);
}
++i;
}
return (ft_env_backup());
}
int open_outfile(t_info *state)
{
int outfd;
if (state->offset == 3)
outfd = open(state->argv[state->index + state->offset + 1], O_WRONLY
| O_CREAT | O_APPEND, 0644);
else
outfd = open(state->argv[state->index + state->offset + 1], O_WRONLY
| O_CREAT | O_TRUNC, 0644);
if (outfd == -1)
{
perror("File");
exit(EXIT_FAILURE);
}
return (outfd);
}