tgrekov-pipex
HIVE pipex May 2024
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
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:07 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <fcntl.h>
21#include <string.h>
22#include <errno.h>
23#include <stdlib.h>
24#include "../mandatory/utils/utils.h"
25#include <libft.h>
26#include <ft_printf.h>
27
28int here_doc(char *limiter, int *in_out);
29char **get_paths(char **envp);
30int dispatcher(int n, char ***str_arrs, int *in_out);
31
41static void _input(int argc, char **argv, int *is_here_doc, int *trunc_append)
42{
43 *is_here_doc = 0;
44 *trunc_append = O_TRUNC;
45 if (argv[1] && !ft_strncmp("here_doc", argv[1], 8))
46 {
47 *is_here_doc = 1;
48 *trunc_append = O_APPEND;
49 }
50 if (argc < (5 + *is_here_doc))
51 {
52 ft_printf("%>Too few arguments: %d. Expected %d+\n",
53 2, argc - 1, 4 + *is_here_doc);
54 exit(1);
55 }
56}
57
58static int _open(char *name, int flags, int mode)
59{
60 int fd;
61
62 fd = open(name, flags, mode);
63 if (fd == -1)
64 ft_printf("%>Failed to open \"%s\": %s\n", 2, name, strerror(errno));
65 return (fd);
66}
67
68int main(int argc, char **argv, char **envp)
69{
70 char **paths;
71 int in_out[2];
72 int status;
73 int is_here_doc;
74 int trunc_append;
75
76 _input(argc, argv, &is_here_doc, &trunc_append);
77 in_out[1] = _open(argv[argc - 1], O_WRONLY | O_CREAT | trunc_append,
78 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
79 if (!is_here_doc)
80 in_out[0] = _open(argv[1], O_RDONLY, 0);
81 else if (!here_doc(argv[2], in_out))
82 return (1);
83 paths = get_paths(envp);
84 status = dispatcher(argc - 3 - is_here_doc,
85 (char **[]){paths, argv + 2 + is_here_doc, envp}, in_out);
86 arr_free((void **) paths);
87 return (status);
88}
void ** arr_free(void **arr)
Free each element in a null-terminated array, and then the array itself.
Definition arr_free.c:29
static void _input(int argc, char **argv, int *is_here_doc, int *trunc_append)
Check if in here_doc mode, verify arguments count accordingly, and set append or truncate accordingly...
Definition main.c:41
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 here_doc(char *limiter, int *in_out)
Capture multiline input from stdin and write to a pipe readable from in_out[0] until a line containin...
Definition here_doc.c:46
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252
int ft_strncmp(const char *s1, const char *s2, size_t n)
Compare up to n characters of string s1 with string s2.
Definition ft_strncmp.c:33