tgrekov-pipex
HIVE pipex May 2024
Loading...
Searching...
No Matches
main.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/04/02 13:59:01 by tgrekov #+# #+# */
9/* Updated: 2024/05/20 12:27:56 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include <fcntl.h>
14#include <string.h>
15#include <errno.h>
16#include <stdlib.h>
17#include "utils/utils.h"
18#include <ft_printf.h>
19
20char **get_paths(char **envp);
21int dispatcher(char **paths, char **argv, char **envp, int *in_out);
22
23static int _open(char *name, int flags, int mode)
24{
25 int fd;
26
27 fd = open(name, flags, mode);
28 if (fd == -1)
29 ft_printf("%>Failed to open \"%s\": %s\n", 2, name, strerror(errno));
30 return (fd);
31}
32
33int main(int argc, char **argv, char **envp)
34{
35 char **paths;
36 int in_out[2];
37 int status;
38
39 if (argc != 5)
40 {
41 ft_printf("%>Wrong number of arguments: %d. Expected 4\n", 2, argc - 1);
42 return (1);
43 }
44 in_out[1] = _open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC,
45 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
46 in_out[0] = _open(argv[1], O_RDONLY, 0);
47 paths = get_paths(envp);
48 status = dispatcher(paths, argv + 2, envp, in_out);
49 arr_free((void **) paths);
50 return (status);
51}
void ** arr_free(void **arr)
Free each element in a null-terminated array, and then the array itself.
Definition arr_free.c:29
char ** get_paths(char **envp)
Create a null-terminated, unique array of paths from the environment pointer envp....
Definition get_paths.c:73
int dispatcher(int n, char ***str_arrs, int *in_out)
Dispatch n child processes with argument strings str_arrs[1][i], environment pointer str_arrs[2],...
Definition dispatcher.c:85
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252