├── Makefile
├── README.md
├── includes
├── minishell.h
└── termcaps.h
├── libft
├── Makefile
├── includes
│ ├── ft_printf.h
│ ├── get_next_line.h
│ └── libft.h
└── srcs
│ ├── ft_atof.c
│ ├── ft_atoi.c
│ ├── ft_atoi_base.c
│ ├── ft_bzero.c
│ ├── ft_c_to_str.c
│ ├── ft_calloc.c
│ ├── ft_check_base.c
│ ├── ft_convert_base.c
│ ├── ft_count_split.c
│ ├── ft_free_split.c
│ ├── ft_intlen.c
│ ├── ft_intlen_base.c
│ ├── ft_is_in_stri.c
│ ├── ft_is_space.c
│ ├── ft_isalnum.c
│ ├── ft_isalpha.c
│ ├── ft_isascii.c
│ ├── ft_isdigit.c
│ ├── ft_isprint.c
│ ├── ft_itoa.c
│ ├── ft_itoa_base.c
│ ├── ft_lst_remove_if.c
│ ├── ft_lstadd_back.c
│ ├── ft_lstadd_front.c
│ ├── ft_lstclear.c
│ ├── ft_lstdelone.c
│ ├── ft_lstiter.c
│ ├── ft_lstlast.c
│ ├── ft_lstmap.c
│ ├── ft_lstnew.c
│ ├── ft_lstsize.c
│ ├── ft_memalloc.c
│ ├── ft_memccpy.c
│ ├── ft_memchr.c
│ ├── ft_memcmp.c
│ ├── ft_memcpy.c
│ ├── ft_memdel.c
│ ├── ft_memmove.c
│ ├── ft_memset.c
│ ├── ft_printf
│ ├── Makefile
│ ├── buffer.c
│ ├── convert.c
│ ├── convert_int.c
│ ├── ft_printf.c
│ ├── parse.c
│ ├── utils.c
│ └── utils2.c
│ ├── ft_putchar.c
│ ├── ft_putchar_fd.c
│ ├── ft_putendl.c
│ ├── ft_putendl_fd.c
│ ├── ft_putnbr.c
│ ├── ft_putnbr_fd.c
│ ├── ft_putstr.c
│ ├── ft_putstr_fd.c
│ ├── ft_split.c
│ ├── ft_split_n.c
│ ├── ft_ssplit.c
│ ├── ft_str_c_count.c
│ ├── ft_str_end.c
│ ├── ft_str_isdigit.c
│ ├── ft_strcat.c
│ ├── ft_strchr.c
│ ├── ft_strcmp.c
│ ├── ft_strcpy.c
│ ├── ft_strdel.c
│ ├── ft_strdup.c
│ ├── ft_strequ.c
│ ├── ft_strfree_join.c
│ ├── ft_strjoin.c
│ ├── ft_strjoin_double_free.c
│ ├── ft_strjoin_free.c
│ ├── ft_strlcat.c
│ ├── ft_strlcpy.c
│ ├── ft_strlen.c
│ ├── ft_strlen_c.c
│ ├── ft_strmapi.c
│ ├── ft_strncat.c
│ ├── ft_strncmp.c
│ ├── ft_strncpy.c
│ ├── ft_strndup.c
│ ├── ft_strnew.c
│ ├── ft_strnstr.c
│ ├── ft_strrchr.c
│ ├── ft_strstr.c
│ ├── ft_strtrim.c
│ ├── ft_substr.c
│ ├── ft_tolower.c
│ ├── ft_toupper.c
│ ├── ft_uintlen.c
│ ├── ft_uitoa.c
│ └── get_next_line.c
└── srcs
├── commands
├── cd.c
├── echo.c
├── env.c
├── exit.c
├── export.c
├── pwd.c
└── unset.c
├── env
├── env_utils.c
└── env_utils2.c
├── main.c
├── main_bonus.c
├── parsing
├── bin_path.c
├── cmds.c
├── parsing_cmds.c
├── parsing_cmds_bonus.c
├── parsing_token.c
├── parsing_token_bonus.c
├── token.c
├── token2.c
└── wildcard_bonus.c
├── termcaps
├── cursor.c
├── history.c
├── init.c
├── loop.c
├── move_line.c
├── move_word.c
├── string_manip.c
└── utils.c
└── utils
├── command_process.c
├── command_process2.c
├── command_process2_bonus.c
├── errors.c
├── exec.c
├── free_utils.c
├── gnl_no_eof.c
├── quotes.c
├── redirect.c
├── redirect_bonus.c
├── signals.c
├── signals_bonus.c
├── subshell_bonus.c
├── utils.c
├── utils2.c
└── wait_commmand_bonus.c
/README.md:
--------------------------------------------------------------------------------
1 | # Minishell
2 |
3 | `minishell` is a command interpreter based on ``bash``.
4 | It must implements basic functionalities of a shell like environment variable, builtins, pipes...
5 |
6 | 
7 |
8 | ## Features
9 |
10 | - ``cd``
11 | - ``pwd``
12 | - ``echo``
13 | - ``export``
14 | - ``unset``
15 | - ``env``
16 | - ``exit``
17 | - ``CTRL-C``
18 | - ``CTRL-\``
19 | - ``CTRL-D``
20 | - ``|`` pipes
21 | - ``;`` semicolons
22 | - ``>`` ``>>`` ``<`` ``<<`` redirections
23 | - ``&&`` ``||`` operators
24 | - ``*`` wildcard
25 | - local variable
26 | - env expansions + ``$?``
27 | - line edition
28 | - termcaps
29 | - history (up/down arrows)
30 | - move cursor (left/right arrows)
31 | - move word by word (CTRL-left/CTRL-right)
32 | - move to beginning/end of line (HOME/END)
33 | - change line (CTRL-UP/CTRL-DOWN)
34 | - copy/paste/cut from cursor position (ALT-X/ALT-C/ALT-V)
35 |
36 | ## Usage
37 |
38 | ```shell
39 | # Compile the mandatory part
40 | make
41 |
42 | # Compile with bonus (termcaps)
43 | make bonus
44 |
45 | # Launch the shell
46 | ./minishell
47 | ```
48 |
49 | ## Authors
50 |
51 | * [solaldunckel](https://github.com/solaldunckel)
52 | * [hguerni](https://github.com/hguerni)
53 |
--------------------------------------------------------------------------------
/includes/termcaps.h:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* termcaps.h :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/26 03:50:22 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 14:52:41 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #ifndef TERMCAPS_H
14 | # define TERMCAPS_H
15 |
16 | # include "minishell.h"
17 | # include
18 | # include
19 | # include
20 | # include
21 |
22 | # define BACKSPACE 127
23 | # define EOF_KEY 4
24 | # define LEFT_ARROW 4479771
25 | # define RIGHT_ARROW 4414235
26 | # define UP_ARROW 4283163
27 | # define DOWN_ARROW 4348699
28 |
29 | # define HOME 4741915
30 | # define END 4610843
31 |
32 | # define ALT_X 8948194
33 | # define ALT_C 42947
34 | # define ALT_V 10127586
35 |
36 | # define CTRL_LEFT 74995417045787
37 | # define CTRL_RIGHT 73895905418011
38 | # define CTRL_UP 71696882162459
39 | # define CTRL_DOWN 72796393790235
40 |
41 | typedef struct s_hist
42 | {
43 | char *cmd;
44 | struct s_hist *prev;
45 | struct s_hist *next;
46 | } t_hist;
47 |
48 | typedef struct s_termcap
49 | {
50 | struct termios term;
51 | struct termios term_backup;
52 | t_hist *history;
53 | t_hist *cur_history;
54 | char *backup_cmd;
55 | char *copy_cmd;
56 | int start_row;
57 | int start_col;
58 | int col;
59 | int row;
60 | int plen;
61 | int cur_pos;
62 | int currow;
63 | int curcol;
64 | int lenlen;
65 | int rowoffset;
66 | int mod_offset;
67 | int endcol;
68 | int endrow;
69 | char *cm;
70 | char *ce;
71 | char *dl;
72 | long backspace;
73 | } t_termcap;
74 |
75 | t_termcap *g_tc;
76 |
77 | void add_cmd_to_history(char *cmd);
78 | void add_history_list(t_hist **begin, t_hist *new);
79 |
80 | int putchar_tc(int tc);
81 |
82 | void move_cursor_left(void);
83 | void move_cursor_right(void);
84 | void move_cursor_begin(void);
85 | void move_cursor_end(void);
86 |
87 | void move_prev_word(void);
88 | void move_next_word(void);
89 |
90 | void move_prev_line(void);
91 | void move_next_line(void);
92 |
93 | void up_history(void);
94 | void down_history(void);
95 |
96 | void cut_line(void);
97 | void copy_line(void);
98 | void paste_line(void);
99 |
100 | void print_char(long c);
101 | void delete_char(void);
102 | /*
103 | ** UTILS
104 | */
105 |
106 | int termcaps_loop(void);
107 | void init_term();
108 | void init_tc();
109 | void cursor_win();
110 | void get_cursor_position(int *col, int *rows);
111 | char *ft_strjoin_middle(char *s1, const char *s2, int div);
112 | void empty_space(int len);
113 | char *ft_sdpfr(const char *s1, char *s2);
114 |
115 | #endif
116 |
--------------------------------------------------------------------------------
/libft/includes/get_next_line.h:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* get_next_line.h :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/11 11:17:21 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/16 16:18:15 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #ifndef GET_NEXT_LINE_H
14 | # define GET_NEXT_LINE_H
15 |
16 | # ifndef BUFFER_SIZE
17 | # define BUFFER_SIZE 2048
18 | # endif
19 |
20 | # define SUCCESS 1
21 | # define FINISH 0
22 | # define ERROR -1
23 |
24 | # include "libft.h"
25 | # include
26 |
27 | int get_next_line(int fd, char **line);
28 |
29 | #endif
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_atof.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_atof.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/10 23:17:32 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 15:35:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | float ft_atof(char *str)
16 | {
17 | float atof;
18 | int atoi;
19 | int i;
20 | int fac;
21 |
22 | fac = 1;
23 | atof = 0;
24 | i = 0;
25 | while (ft_is_space(str[i]))
26 | i++;
27 | str[i] == '-' ? fac = -1 : 0;
28 | atoi = ft_atoi(str);
29 | i = ft_intlen(atoi);
30 | fac == -1 ? i++ : 0;
31 | if (str[i] != '.')
32 | return (atoi);
33 | i++;
34 | while (str[i] >= '0' && str[i] <= '9')
35 | {
36 | fac *= 10;
37 | atof = atof * 10 + str[i] - 48;
38 | i++;
39 | }
40 | atof = atof / fac;
41 | return (atoi + atof);
42 | }
43 |
--------------------------------------------------------------------------------
/libft/srcs/ft_atoi.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_atoi.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:01:38 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/12 14:44:37 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_atoi(const char *nptr)
16 | {
17 | int atoi;
18 | int negative;
19 |
20 | atoi = 0;
21 | negative = 0;
22 | while (ft_is_space(*nptr))
23 | nptr++;
24 | if (*nptr == '-' || *nptr == '+')
25 | {
26 | if (*nptr == '-')
27 | negative = 1;
28 | nptr++;
29 | }
30 | while (*nptr >= '0' && *nptr <= '9')
31 | {
32 | atoi = atoi * 10 + *nptr - 48;
33 | nptr++;
34 | }
35 | return (negative ? -atoi : atoi);
36 | }
37 |
38 | long ft_atol(const char *nptr)
39 | {
40 | long atoi;
41 | int negative;
42 |
43 | atoi = 0;
44 | negative = 0;
45 | while (ft_is_space(*nptr))
46 | nptr++;
47 | if (*nptr == '-' || *nptr == '+')
48 | {
49 | if (*nptr == '-')
50 | negative = 1;
51 | nptr++;
52 | }
53 | while (*nptr >= '0' && *nptr <= '9')
54 | {
55 | atoi = atoi * 10 + *nptr - 48;
56 | nptr++;
57 | }
58 | return (negative ? -atoi : atoi);
59 | }
60 |
--------------------------------------------------------------------------------
/libft/srcs/ft_atoi_base.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_atoi_base.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 14:18:06 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:10:59 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | intmax_t ft_atoi_base(char *str, char *base)
16 | {
17 | int i;
18 | intmax_t atoi;
19 | int negative;
20 | int base_len;
21 |
22 | i = 0;
23 | atoi = 0;
24 | negative = 0;
25 | base_len = ft_strlen(base);
26 | if (!ft_check_base(base))
27 | return (FALSE);
28 | while (ft_is_space(*str))
29 | str++;
30 | if (*str == '+' || *str == '-')
31 | {
32 | if (*str == '-')
33 | negative = 1;
34 | str++;
35 | }
36 | while (ft_is_in_stri(*str, base) >= 0)
37 | {
38 | atoi = atoi * base_len + ft_is_in_stri(*str, base);
39 | str++;
40 | }
41 | return (negative ? -atoi : atoi);
42 | }
43 |
--------------------------------------------------------------------------------
/libft/srcs/ft_bzero.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_bzero.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 17:55:30 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 17:28:45 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_bzero(void *s, size_t n)
16 | {
17 | size_t i;
18 | char *str;
19 |
20 | i = 0;
21 | str = s;
22 | if (n == 0)
23 | return ;
24 | while (i < n)
25 | {
26 | str[i] = 0;
27 | i++;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_c_to_str.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_c_to_str.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/24 22:01:13 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:01:21 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_c_to_str(char c)
16 | {
17 | char *s;
18 |
19 | s = ft_calloc(2, sizeof(char));
20 | s[0] = c;
21 | s[1] = '\0';
22 | return (s);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_calloc.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_calloc.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:34:14 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 16:36:42 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_calloc(size_t count, size_t size)
16 | {
17 | return (ft_memalloc(count * size));
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_check_base.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_check_base.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 14:57:02 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 14:58:04 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_check_base(char *base)
16 | {
17 | int i;
18 | int j;
19 |
20 | i = 0;
21 | if (ft_strlen(base) <= 1)
22 | return (0);
23 | while (base[i])
24 | {
25 | j = i + 1;
26 | if (base[i] == '-' || base[i] == '+' || base[i] < 32
27 | || base[i] > 127 || base[i] == ' ')
28 | return (0);
29 | while (base[j])
30 | {
31 | if (base[i] == base[j])
32 | return (0);
33 | j++;
34 | }
35 | i++;
36 | }
37 | return (1);
38 | }
39 |
--------------------------------------------------------------------------------
/libft/srcs/ft_convert_base.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_convert_base.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 14:59:20 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:10:13 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_convert_base(char *nbr, char *base_from, char *base_to)
16 | {
17 | intmax_t num;
18 | int i;
19 | char *tab;
20 |
21 | i = 0;
22 | if (!ft_check_base(base_from) || !ft_check_base(base_to))
23 | return (NULL);
24 | num = ft_atoi_base(nbr, base_from);
25 | tab = ft_itoa_base(num, base_to);
26 | return (tab);
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_count_split.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_count_split.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/03 10:04:47 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/03 10:05:05 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_count_split(char **split)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (split[i])
21 | i++;
22 | return (i);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_free_split.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_free_split.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/03 10:00:22 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/23 15:59:57 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_free_split(char ***split)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (*split && (*split)[i])
21 | {
22 | ft_strdel(&(*split)[i]);
23 | i++;
24 | }
25 | free(*split);
26 | *split = NULL;
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_intlen.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_intlen.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 13:03:37 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:50:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_intlen(intmax_t n)
16 | {
17 | size_t len;
18 |
19 | len = 0;
20 | if (!n)
21 | len++;
22 | while (n)
23 | {
24 | n = n / 10;
25 | len++;
26 | }
27 | return (len);
28 | }
29 |
--------------------------------------------------------------------------------
/libft/srcs/ft_intlen_base.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_intlen_base.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 15:35:31 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:09:00 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_intlen_base(uintmax_t n, char *base)
16 | {
17 | size_t len;
18 | uintmax_t base_len;
19 |
20 | len = 1;
21 | base_len = ft_strlen(base);
22 | while (n >= base_len)
23 | {
24 | n = n / base_len;
25 | len++;
26 | }
27 | return (len);
28 | }
29 |
--------------------------------------------------------------------------------
/libft/srcs/ft_is_in_stri.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_is_in_stri.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 14:28:28 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:18:32 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_is_in_stri(char c, char *str)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (str[i])
21 | {
22 | if (str[i] == c)
23 | return (i);
24 | i++;
25 | }
26 | return (-1);
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_is_space.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_is_space.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 14:26:21 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 14:27:58 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_is_space(char c)
16 | {
17 | return (c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\r'
18 | || c == '\f');
19 | }
20 |
--------------------------------------------------------------------------------
/libft/srcs/ft_isalnum.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isalnum.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:53:05 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/07 11:58:38 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_isalnum(int c)
16 | {
17 | if (ft_isalpha(c) || ft_isdigit(c))
18 | return (1);
19 | return (0);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_isalpha.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isalpha.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:40:42 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:36:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_isalpha(int c)
16 | {
17 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
18 | return (1);
19 | return (0);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_isascii.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isascii.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 12:30:42 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:35:57 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_isascii(int c)
16 | {
17 | if (c >= 0 && c <= 127)
18 | return (1);
19 | return (0);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_isdigit.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isdigit.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:50:40 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:36:18 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_isdigit(int c)
16 | {
17 | if (c >= '0' && c <= '9')
18 | return (1);
19 | return (0);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_isprint.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isprint.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 13:30:16 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:36:21 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_isprint(int c)
16 | {
17 | if (c >= 32 && c <= 126)
18 | return (1);
19 | return (0);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_itoa.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_itoa.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 12:54:59 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:11:34 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_itoa(intmax_t n)
16 | {
17 | char *str;
18 | int neg;
19 | int num_len;
20 |
21 | neg = 0;
22 | num_len = ft_intlen(n);
23 | if (n < 0)
24 | {
25 | num_len++;
26 | neg = 1;
27 | n = -n;
28 | }
29 | num_len = ft_intlen(n);
30 | if (!(str = ft_calloc((num_len + 1), sizeof(char))))
31 | return (NULL);
32 | str[num_len] = '\0';
33 | while (num_len)
34 | {
35 | str[--num_len] = n % 10 + 48;
36 | n = n / 10;
37 | }
38 | return (str);
39 | }
40 |
--------------------------------------------------------------------------------
/libft/srcs/ft_itoa_base.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_itoa_base.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 15:31:09 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:00:46 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_itoa_base(uintmax_t n, char *base)
16 | {
17 | char *str;
18 | int num_len;
19 | int base_len;
20 |
21 | num_len = ft_intlen_base(n, base);
22 | base_len = ft_strlen(base);
23 | if (!(str = ft_calloc((num_len + 1), sizeof(char))))
24 | return (NULL);
25 | str[num_len] = '\0';
26 | while (num_len)
27 | {
28 | str[--num_len] = base[n % base_len];
29 | n = n / base_len;
30 | }
31 | return (str);
32 | }
33 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lst_remove_if.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lst_remove_if.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/17 03:35:22 by sdunckel #+# #+# */
9 | /* Updated: 2020/01/17 03:35:51 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lst_remove_if(t_list **begin_list, void *data, int (*cmp)(),
16 | void (*del)())
17 | {
18 | t_list *current;
19 | t_list *next;
20 |
21 | if (!*begin_list)
22 | return ;
23 | while (*begin_list && cmp((*begin_list)->content, data) == 0)
24 | {
25 | next = (*begin_list)->next;
26 | del(*begin_list);
27 | *begin_list = next;
28 | }
29 | if (!*begin_list)
30 | return ;
31 | current = *begin_list;
32 | while (current && current->next)
33 | {
34 | if (cmp(current->next->content, data) == 0)
35 | {
36 | next = current->next->next;
37 | del(current->next);
38 | current->next = next;
39 | }
40 | else
41 | current = current->next;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstadd_back.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstadd_back.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:29:07 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:05:10 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lstadd_back(t_list **alst, t_list *new)
16 | {
17 | t_list *tmp;
18 |
19 | if (!alst || !new)
20 | return ;
21 | if (*alst)
22 | {
23 | tmp = *alst;
24 | while (tmp->next)
25 | tmp = tmp->next;
26 | tmp->next = new;
27 | }
28 | else
29 | *alst = new;
30 | }
31 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstadd_front.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstadd_front.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:17:51 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:05:01 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lstadd_front(t_list **alst, t_list *new)
16 | {
17 | if (!alst || !new || !*alst)
18 | return ;
19 | new->next = *alst;
20 | *alst = new;
21 | }
22 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstclear.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstclear.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:34:19 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/24 02:12:54 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lstclear(t_list **lst, void (*del)(void *))
16 | {
17 | t_list *tmp;
18 | t_list *tmp2;
19 |
20 | if (!lst || !del)
21 | return ;
22 | tmp = *lst;
23 | while (tmp)
24 | {
25 | del(tmp->content);
26 | tmp2 = tmp->next;
27 | free(tmp);
28 | tmp = tmp2;
29 | }
30 | *lst = NULL;
31 | }
32 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstdelone.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstdelone.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:32:11 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:04:38 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lstdelone(t_list *lst, void (*del)(void *))
16 | {
17 | if (!lst || !del)
18 | return ;
19 | del(lst->content);
20 | free(lst);
21 | }
22 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstiter.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstiter.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:41:16 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:04:26 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_lstiter(t_list *lst, void (*f)(void *))
16 | {
17 | t_list *tmp;
18 |
19 | if (!lst || !f)
20 | return ;
21 | tmp = lst;
22 | while (tmp)
23 | {
24 | f(tmp);
25 | tmp = tmp->next;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstlast.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstlast.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:27:37 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 17:12:12 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | t_list *ft_lstlast(t_list *lst)
16 | {
17 | t_list *tmp;
18 |
19 | tmp = lst;
20 | if (!lst)
21 | return (tmp);
22 | while (tmp->next)
23 | tmp = tmp->next;
24 | return (tmp);
25 | }
26 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstmap.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstmap.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:43:38 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 15:23:16 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | t_list *ft_lstmap(t_list *lst, void *(*f)(void*), void (*del)(void *))
16 | {
17 | t_list **begin;
18 | t_list *temp1;
19 | t_list *temp3;
20 |
21 | if (!(begin = (t_list **)malloc(sizeof(t_list))))
22 | return (NULL);
23 | if (!(lst) || !(*begin = ft_lstnew(f(lst->content))) || f == NULL)
24 | return (NULL);
25 | lst = lst->next;
26 | temp1 = *begin;
27 | while (lst)
28 | {
29 | temp3 = temp1;
30 | if (!(temp1 = ft_lstnew(f(lst->content))))
31 | {
32 | ft_lstclear(begin, del);
33 | return (NULL);
34 | }
35 | temp3->next = temp1;
36 | lst = lst->next;
37 | }
38 | temp1->next = NULL;
39 | return (*begin);
40 | }
41 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstnew.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstnew.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:12:48 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 12:47:33 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | t_list *ft_lstnew(void *content)
16 | {
17 | t_list *lst;
18 |
19 | if (!(lst = (t_list*)malloc(sizeof(t_list))))
20 | return (NULL);
21 | lst->content = content;
22 | lst->next = NULL;
23 | return (lst);
24 | }
25 |
--------------------------------------------------------------------------------
/libft/srcs/ft_lstsize.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstsize.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:23:32 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:59:31 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_lstsize(t_list *lst)
16 | {
17 | int count;
18 | t_list *tmp;
19 |
20 | tmp = lst;
21 | count = 0;
22 | while (tmp)
23 | {
24 | tmp = tmp->next;
25 | count++;
26 | }
27 | return (count);
28 | }
29 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memalloc.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memalloc.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:27:16 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 09:39:36 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memalloc(size_t size)
16 | {
17 | void *mem;
18 |
19 | if (!(mem = malloc(size)))
20 | return (NULL);
21 | ft_memset(mem, 0, size);
22 | return (mem);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memccpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memccpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:01:11 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 17:36:53 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n)
16 | {
17 | size_t i;
18 | char *sd;
19 | char *sr;
20 | unsigned char uc;
21 |
22 | sd = dst;
23 | sr = (void*)src;
24 | uc = c;
25 | i = 0;
26 | while (i < n)
27 | {
28 | sd[i] = sr[i];
29 | i++;
30 | if (sr[i - 1] == uc)
31 | return (dst + i);
32 | }
33 | return (NULL);
34 | }
35 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:15:45 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 18:02:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memchr(const void *s, int c, size_t n)
16 | {
17 | size_t i;
18 | char *ss;
19 |
20 | i = 0;
21 | ss = (void*)s;
22 | while (i < n)
23 | {
24 | if (ss[i] == c)
25 | return (ss + i);
26 | i++;
27 | }
28 | return (NULL);
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memcmp.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memcmp.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:18:21 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:43:29 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_memcmp(const void *s1, const void *s2, size_t n)
16 | {
17 | size_t i;
18 | char *ss1;
19 | char *ss2;
20 |
21 | ss1 = (void*)s1;
22 | ss2 = (void*)s2;
23 | i = 0;
24 | while (i < n)
25 | {
26 | if (ss1[i] != ss2[i])
27 | return ((unsigned char)ss1[i] - (unsigned char)ss2[i]);
28 | i++;
29 | }
30 | return (0);
31 | }
32 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memcpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memcpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 17:57:34 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 17:28:39 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memcpy(void *dst, const void *src, size_t n)
16 | {
17 | size_t i;
18 | char *sd;
19 | char *sr;
20 |
21 | sd = dst;
22 | sr = (void*)src;
23 | i = 0;
24 | while (i < n)
25 | {
26 | sd[i] = sr[i];
27 | i++;
28 | }
29 | return (dst);
30 | }
31 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memdel.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memdel.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:28:59 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 09:29:37 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_memdel(void **ap)
16 | {
17 | free(*ap);
18 | *ap = NULL;
19 | }
20 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memmove.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memmove.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:11:10 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:59:58 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memmove(void *dst, const void *src, size_t len)
16 | {
17 | size_t i;
18 | char *sd;
19 | char *sr;
20 |
21 | sd = dst;
22 | sr = (void*)src;
23 | i = 0;
24 | if (sr < sd)
25 | {
26 | i++;
27 | while (i <= len)
28 | {
29 | sd[len - i] = sr[len - i];
30 | i++;
31 | }
32 | }
33 | else
34 | {
35 | while (i < len)
36 | {
37 | sd[i] = sr[i];
38 | i++;
39 | }
40 | }
41 | return (dst);
42 | }
43 |
--------------------------------------------------------------------------------
/libft/srcs/ft_memset.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memset.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 17:52:13 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 17:06:41 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memset(void *b, int c, size_t len)
16 | {
17 | size_t i;
18 | char *s;
19 |
20 | s = b;
21 | i = 0;
22 | while (i < len)
23 | {
24 | s[i] = c;
25 | i++;
26 | }
27 | return (b);
28 | }
29 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: sdunckel +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2019/10/07 12:02:19 by sdunckel #+# #+# #
9 | # Updated: 2020/01/14 19:40:27 by sdunckel ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | NAME = libftprintf.a
14 |
15 | SRCS = \
16 | ft_printf.c \
17 | parse.c \
18 | convert.c \
19 | convert_int.c \
20 | utils.c \
21 | buffer.c \
22 | utils2.c
23 |
24 | OBJS = ${SRCS:.c=.o}
25 |
26 | HEADER = ../../includes
27 |
28 | CC = gcc
29 | CFLAGS = -Wall -Wextra -Werror
30 | RM = rm -f
31 |
32 | all: ${NAME}
33 |
34 | $(NAME): ${OBJS}
35 | @ar -rcs ${NAME} ${OBJS}
36 |
37 | %.o: %.c
38 | @${CC} ${CFLAGS} -I ${HEADER} -o $@ -c $<
39 |
40 | clean:
41 | @${RM} ${OBJS}
42 |
43 | fclean: clean
44 | @${RM} ${NAME}
45 |
46 | re: fclean all
47 |
48 | .PHONY: all fclean clean re
49 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/buffer.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* buffer.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/14 19:32:42 by sdunckel #+# #+# */
9 | /* Updated: 2020/01/14 19:32:56 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | void ft_dump_buffer(t_printf *tab)
16 | {
17 | write(tab->fd, tab->buf, tab->buf_count);
18 | tab->buf_count = 0;
19 | }
20 |
21 | void ft_add_to_buff(t_printf *tab, char *str, int len)
22 | {
23 | int i;
24 |
25 | i = 0;
26 | tab->ret += len;
27 | while (i < len)
28 | {
29 | tab->buf[tab->buf_count] = str[i];
30 | tab->buf_count++;
31 | if (tab->buf_count == BUFFER_SIZE)
32 | ft_dump_buffer(tab);
33 | i++;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/convert.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* convert.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/22 10:34:24 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/03 19:34:54 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | void ft_convert_str(va_list ap, t_printf *tab)
16 | {
17 | char *str;
18 | char *sp;
19 |
20 | if (tab->precision && tab->precision_width < 0)
21 | tab->precision_width = -tab->precision_width;
22 | if (!(str = va_arg(ap, char *)))
23 | str = ft_strdup_l("(null)", tab);
24 | else
25 | str = ft_strdup_l(str, tab);
26 | tab->len = ft_strlen(str);
27 | sp = ft_print_sp(tab);
28 | ft_join_all(str, sp, tab);
29 | }
30 |
31 | void ft_convert_c(va_list ap, t_printf *tab)
32 | {
33 | char *str;
34 | char *sp;
35 |
36 | str = NULL;
37 | tab->len = 1;
38 | tab->converter == 'c' ? str = ft_c_to_str(va_arg(ap, int)) : 0;
39 | tab->converter == '%' ? str = ft_c_to_str('%') : 0;
40 | sp = ft_print_sp(tab);
41 | ft_join_all(str, sp, tab);
42 | }
43 |
44 | void ft_convert_n(va_list ap, t_printf *tab)
45 | {
46 | intmax_t *n;
47 |
48 | if (tab->l_count >= 2)
49 | n = (intmax_t *)va_arg(ap, long long *);
50 | else if (tab->l_count == 1)
51 | n = (intmax_t *)va_arg(ap, long *);
52 | else if (tab->h_count && ((tab->h_count % 2) == 0))
53 | n = (intmax_t *)((char *)va_arg(ap, int *));
54 | else if (tab->h_count && ((tab->h_count % 2) != 0))
55 | n = (intmax_t *)((short *)va_arg(ap, int *));
56 | else
57 | n = (intmax_t *)va_arg(ap, int *);
58 | if (!n)
59 | return ;
60 | *n = tab->ret;
61 | }
62 |
63 | void ft_convert_x(va_list ap, t_printf *tab)
64 | {
65 | char *str;
66 | char *sp;
67 |
68 | str = NULL;
69 | tab->is_int = 1;
70 | ft_size_u(ap, tab);
71 | tab->converter == 'x' ? str = ft_itoa_base(tab->u, "0123456789abcdef") : 0;
72 | tab->converter == 'X' ? str = ft_itoa_base(tab->u, "0123456789ABCDEF") : 0;
73 | tab->len = ft_strlen(str);
74 | str = ft_num_precision(str, tab);
75 | tab->len = ft_strlen(str);
76 | if (tab->u == 0 && tab->precision && tab->precision_width == 0
77 | && !tab->width)
78 | {
79 | free(str);
80 | return ;
81 | }
82 | if (tab->u == 0 && tab->precision && tab->precision_width == 0)
83 | {
84 | free(str);
85 | str = ft_strdup(" ");
86 | }
87 | tab->sharp && tab->u ? tab->len += 2 : 0;
88 | sp = ft_print_sp(tab);
89 | ft_join_all(str, sp, tab);
90 | }
91 |
92 | void ft_convert_p(va_list ap, t_printf *tab)
93 | {
94 | char *str;
95 | char *sp;
96 |
97 | tab->u = va_arg(ap, long unsigned);
98 | str = ft_itoa_base(tab->u, "0123456789abcdef");
99 | tab->precision ? tab->zero = 0 : 0;
100 | tab->len = ft_strlen(str);
101 | str = ft_num_precision(str, tab);
102 | tab->len = ft_strlen(str) + 2;
103 | if (tab->u == 0 && tab->precision && tab->precision_width == 0)
104 | {
105 | free(str);
106 | str = ft_strdup("");
107 | tab->len -= 1;
108 | }
109 | sp = ft_print_sp(tab);
110 | ft_join_all(str, sp, tab);
111 | }
112 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/convert_int.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_convert_int.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/23 15:43:05 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 11:32:47 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | void ft_size_int(va_list ap, t_printf *tab)
16 | {
17 | if (tab->l_count >= 2)
18 | tab->n = (intmax_t)va_arg(ap, long long);
19 | else if (tab->l_count == 1)
20 | tab->n = (intmax_t)va_arg(ap, long);
21 | else if (tab->h_count && ((tab->h_count % 2) == 0))
22 | tab->n = (intmax_t)((char)va_arg(ap, int));
23 | else if (tab->h_count && ((tab->h_count % 2) != 0))
24 | tab->n = (intmax_t)((short)va_arg(ap, int));
25 | else
26 | tab->n = (intmax_t)va_arg(ap, int);
27 | }
28 |
29 | void ft_size_u(va_list ap, t_printf *tab)
30 | {
31 | if (tab->l_count >= 2)
32 | tab->u = (uintmax_t)va_arg(ap, unsigned long long);
33 | else if (tab->l_count == 1)
34 | tab->u = (uintmax_t)va_arg(ap, unsigned long);
35 | else if (tab->h_count && ((tab->h_count % 2) == 0))
36 | tab->u = (uintmax_t)((unsigned char)va_arg(ap, int));
37 | else if (tab->h_count && ((tab->h_count % 2) != 0))
38 | tab->u = (uintmax_t)((unsigned short)va_arg(ap, int));
39 | else
40 | tab->u = (uintmax_t)va_arg(ap, unsigned int);
41 | }
42 |
43 | void ft_convert_int(va_list ap, t_printf *tab)
44 | {
45 | char *str;
46 | char *sp;
47 |
48 | tab->is_int = 1;
49 | ft_size_int(ap, tab);
50 | str = itoa_printf(tab->n);
51 | tab->len = ft_intlen(tab->n);
52 | str = ft_num_precision(str, tab);
53 | tab->len = ft_strlen(str);
54 | (tab->n < 0) ? tab->len++ : 0;
55 | (tab->n >= 0) && (tab->plus || tab->space) ? tab->len++ : 0;
56 | if (tab->n == 0 && tab->precision && tab->precision_width == 0
57 | && !tab->width)
58 | {
59 | free(str);
60 | return ;
61 | }
62 | if (tab->n == 0 && tab->precision && tab->precision_width == 0)
63 | {
64 | free(str);
65 | str = ft_strdup(" ");
66 | }
67 | sp = ft_print_sp(tab);
68 | ft_join_all(str, sp, tab);
69 | }
70 |
71 | void ft_convert_uint(va_list ap, t_printf *tab)
72 | {
73 | char *str;
74 | char *sp;
75 |
76 | tab->is_int = 1;
77 | ft_size_u(ap, tab);
78 | str = ft_uitoa(tab->u);
79 | tab->len = ft_strlen(str);
80 | str = ft_num_precision(str, tab);
81 | tab->len = ft_strlen(str);
82 | if (tab->u == 0 && tab->precision && tab->precision_width == 0
83 | && !tab->width)
84 | {
85 | free(str);
86 | return ;
87 | }
88 | if (tab->u == 0 && tab->precision && tab->precision_width == 0)
89 | {
90 | free(str);
91 | str = ft_strdup(" ");
92 | }
93 | sp = ft_print_sp(tab);
94 | ft_join_all(str, sp, tab);
95 | }
96 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/ft_printf.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_printf.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/10 11:10:11 by sdunckel #+# #+# */
9 | /* Updated: 2020/01/14 19:32:54 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | void ft_print_normal(t_printf *tab, char *str)
16 | {
17 | int len;
18 |
19 | len = 0;
20 | while (str[tab->i] && str[tab->i] != '%')
21 | {
22 | tab->buf[tab->buf_count] = str[tab->i];
23 | tab->buf_count++;
24 | len++;
25 | if (tab->buf_count == BUFFER_SIZE)
26 | ft_dump_buffer(tab);
27 | tab->i++;
28 | }
29 | tab->ret += len;
30 | tab->i--;
31 | }
32 |
33 | void ft_init_struct(t_printf *tab)
34 | {
35 | tab->buf_count = 0;
36 | tab->ret = 0;
37 | tab->fd = 1;
38 | tab->width = 0;
39 | tab->precision = 0;
40 | tab->precision_width = 0;
41 | tab->precision_parsing = 0;
42 | tab->converter = 0;
43 | tab->minus = 0;
44 | tab->zero = 0;
45 | tab->plus = 0;
46 | tab->space = 0;
47 | tab->sharp = 0;
48 | tab->len = 0;
49 | tab->sp_len = 0;
50 | tab->is_int = 0;
51 | tab->h_count = 0;
52 | tab->l_count = 0;
53 | tab->n = 0;
54 | tab->u = 0;
55 | tab->i = 0;
56 | }
57 |
58 | int ft_printf(const char *str, ...)
59 | {
60 | t_printf tab;
61 | va_list ap;
62 |
63 | ft_init_struct(&tab);
64 | va_start(ap, str);
65 | while (str[tab.i])
66 | {
67 | if (str[tab.i] == '%')
68 | {
69 | if (str[tab.i + 1] == '\0')
70 | break ;
71 | if (ft_is_from_pf(str[tab.i + 1]))
72 | ft_parse((char*)str, ap, &tab);
73 | }
74 | else
75 | ft_print_normal(&tab, (char*)str);
76 | tab.i++;
77 | }
78 | va_end(ap);
79 | ft_dump_buffer(&tab);
80 | return (tab.ret);
81 | }
82 |
83 | int ft_dprintf(int fd, const char *str, ...)
84 | {
85 | t_printf tab;
86 | va_list ap;
87 |
88 | ft_init_struct(&tab);
89 | tab.fd = fd;
90 | va_start(ap, str);
91 | while (str[tab.i])
92 | {
93 | if (str[tab.i] == '%')
94 | {
95 | if (str[tab.i + 1] == '\0')
96 | break ;
97 | if (ft_is_from_pf(str[tab.i + 1]))
98 | ft_parse((char*)str, ap, &tab);
99 | }
100 | else
101 | ft_print_normal(&tab, (char*)str);
102 | tab.i++;
103 | }
104 | va_end(ap);
105 | ft_dump_buffer(&tab);
106 | return (tab.ret);
107 | }
108 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/utils.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_ft.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/23 15:45:21 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 11:42:46 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | void ft_add_sign(t_printf *tab)
16 | {
17 | if (tab->n < 0)
18 | {
19 | tab->len--;
20 | ft_add_to_buff(tab, "-", 1);
21 | }
22 | else if (tab->plus && (tab->n >= 0))
23 | {
24 | tab->len--;
25 | ft_add_to_buff(tab, "+", 1);
26 | }
27 | else if (tab->space && !tab->plus && (tab->n >= 0))
28 | {
29 | tab->len--;
30 | ft_add_to_buff(tab, " ", 1);
31 | }
32 | }
33 |
34 | void ft_add_prefix(t_printf *tab)
35 | {
36 | if (tab->converter == 'x')
37 | {
38 | ft_add_to_buff(tab, "0x", 2);
39 | tab->len -= 2;
40 | }
41 | if (tab->converter == 'X')
42 | {
43 | ft_add_to_buff(tab, "0X", 2);
44 | tab->len -= 2;
45 | }
46 | }
47 |
48 | void ft_join_all(char *str, char *sp, t_printf *tab)
49 | {
50 | (tab->is_int && tab->zero) ? ft_add_sign(tab) : 0;
51 | if (tab->zero && tab->converter == 'p')
52 | {
53 | ft_add_to_buff(tab, "0x", 2);
54 | tab->len -= 2;
55 | }
56 | if (tab->sharp && tab->u && tab->zero && tab->precision_width < tab->len)
57 | ft_add_prefix(tab);
58 | if (sp && !tab->minus)
59 | ft_add_to_buff(tab, sp, tab->sp_len);
60 | if (!tab->zero && tab->converter == 'p')
61 | {
62 | ft_add_to_buff(tab, "0x", 2);
63 | tab->len -= 2;
64 | }
65 | if (tab->sharp && tab->u && !tab->zero && tab->precision_width < tab->len)
66 | ft_add_prefix(tab);
67 | if (tab->is_int && !tab->zero)
68 | ft_add_sign(tab);
69 | ft_add_to_buff(tab, str, tab->len);
70 | if (sp && tab->minus)
71 | ft_add_to_buff(tab, sp, tab->sp_len);
72 | free(str);
73 | free(sp);
74 | }
75 |
76 | char *ft_print_sp(t_printf *tab)
77 | {
78 | char *str;
79 | int i;
80 |
81 | i = 0;
82 | if (tab->width < tab->len)
83 | return (NULL);
84 | if (!(str = ft_calloc((tab->width - tab->len + 1), sizeof(char))))
85 | return (NULL);
86 | if (tab->precision && tab->precision_width > tab->len)
87 | tab->zero = 0;
88 | while (i < tab->width - tab->len)
89 | {
90 | if (tab->zero)
91 | str[i] = '0';
92 | else
93 | str[i] = ' ';
94 | i++;
95 | }
96 | tab->sp_len = i;
97 | str[i] = '\0';
98 | return (str);
99 | }
100 |
101 | char *ft_num_precision(char *str, t_printf *tab)
102 | {
103 | char *tmp;
104 | int i;
105 | int j;
106 |
107 | i = 0;
108 | j = 0;
109 | if (!tab->precision)
110 | return (str);
111 | if (tab->precision_width < tab->len)
112 | return (str);
113 | if (!(tmp = ft_calloc(tab->precision_width + tab->len + 1, sizeof(char))))
114 | return (NULL);
115 | while (i < tab->precision_width - tab->len)
116 | {
117 | tmp[i] = '0';
118 | i++;
119 | }
120 | while (str[j])
121 | {
122 | tmp[i + j] = str[j];
123 | j++;
124 | }
125 | tmp[i + j] = '\0';
126 | free(str);
127 | return (tmp);
128 | }
129 |
--------------------------------------------------------------------------------
/libft/srcs/ft_printf/utils2.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_ft2.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/11 09:51:05 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 17:30:55 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "ft_printf.h"
14 |
15 | char *itoa_printf(intmax_t n)
16 | {
17 | char *str;
18 | int num_len;
19 |
20 | num_len = ft_intlen(n);
21 | if (!(str = ft_calloc((num_len + 1), sizeof(char))))
22 | return (NULL);
23 | str[num_len] = '\0';
24 | while (num_len)
25 | {
26 | if (n < 0)
27 | {
28 | str[--num_len] = -(n % 10) + 48;
29 | n = n / 10;
30 | n = -n;
31 | }
32 | else
33 | {
34 | str[--num_len] = n % 10 + 48;
35 | n = n / 10;
36 | }
37 | }
38 | return (str);
39 | }
40 |
41 | int ft_atoi_printf(char *str, int *i)
42 | {
43 | int atoi;
44 |
45 | atoi = 0;
46 | while (str[*i] >= '0' && str[*i] <= '9')
47 | {
48 | atoi = atoi * 10 + str[*i] - 48;
49 | (*i)++;
50 | }
51 | (*i)--;
52 | return (atoi);
53 | }
54 |
55 | char *ft_strdup_l(char *s, t_printf *tab)
56 | {
57 | char *str;
58 | int i;
59 | int len;
60 |
61 | i = 0;
62 | len = ft_strlen(s);
63 | if (tab->precision && tab->precision_width < len)
64 | len = tab->precision_width;
65 | if (!(str = ft_calloc(len + 1, sizeof(char))))
66 | return (NULL);
67 | while (i < len)
68 | {
69 | str[i] = s[i];
70 | i++;
71 | }
72 | str[i] = '\0';
73 | return (str);
74 | }
75 |
76 | void ft_set_precision(t_printf *tab)
77 | {
78 | tab->precision = 1;
79 | tab->precision_parsing = 1;
80 | tab->precision_width = 0;
81 | }
82 |
83 | void ft_reset_flags(t_printf *tab)
84 | {
85 | tab->width = 0;
86 | tab->precision = 0;
87 | tab->precision_width = 0;
88 | tab->precision_parsing = 0;
89 | tab->converter = 0;
90 | tab->minus = 0;
91 | tab->zero = 0;
92 | tab->plus = 0;
93 | tab->space = 0;
94 | tab->sharp = 0;
95 | tab->len = 0;
96 | tab->sp_len = 0;
97 | tab->is_int = 0;
98 | tab->h_count = 0;
99 | tab->l_count = 0;
100 | tab->n = 0;
101 | tab->u = 0;
102 | }
103 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putchar.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putchar.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 08:48:49 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 14:00:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putchar(char c)
16 | {
17 | ft_putchar_fd(c, 1);
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putchar_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putchar_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 13:55:28 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:37:53 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putchar_fd(char c, int fd)
16 | {
17 | write(fd, &c, 1);
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putendl.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putendl.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:05:58 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 16:50:21 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putendl(char *s)
16 | {
17 | ft_putstr(s);
18 | ft_putchar('\n');
19 | }
20 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putendl_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putendl_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:00:28 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 14:01:18 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putendl_fd(char *s, int fd)
16 | {
17 | ft_putstr_fd(s, fd);
18 | ft_putchar_fd('\n', fd);
19 | }
20 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putnbr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putnbr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 08:57:51 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 14:03:20 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putnbr(int n)
16 | {
17 | ft_putnbr_fd(n, 1);
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putnbr_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putnbr_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 14:02:04 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 16:28:30 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putnbr_fd(int n, int fd)
16 | {
17 | long nb;
18 |
19 | nb = n;
20 | if (nb < 0)
21 | {
22 | ft_putchar_fd('-', fd);
23 | nb = -nb;
24 | }
25 | if (nb >= 10)
26 | {
27 | ft_putnbr_fd(nb / 10, fd);
28 | ft_putchar_fd(nb % 10 + 48, fd);
29 | }
30 | if (nb < 10)
31 | ft_putchar_fd(nb + 48, fd);
32 | }
33 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putstr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putstr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:04:37 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 13:59:37 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putstr(char *s)
16 | {
17 | ft_putstr_fd(s, 1);
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_putstr_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putstr_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 13:57:53 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/18 09:42:27 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putstr_fd(char *s, int fd)
16 | {
17 | write(fd, s, ft_strlen(s));
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_split.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_split.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:54:44 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:03:07 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | static size_t count_words(char const *s, char c)
16 | {
17 | int count;
18 | int is_word;
19 |
20 | is_word = 0;
21 | count = 0;
22 | while (*s)
23 | {
24 | if (*s == c)
25 | is_word = 0;
26 | else if (is_word == 0)
27 | {
28 | is_word = 1;
29 | count++;
30 | }
31 | s++;
32 | }
33 | return (count);
34 | }
35 |
36 | static size_t w_s(char const *s, int pos, char c)
37 | {
38 | size_t len;
39 |
40 | len = 0;
41 | while (s[pos])
42 | {
43 | if (s[pos] == c)
44 | return (len);
45 | len++;
46 | pos++;
47 | }
48 | return (len);
49 | }
50 |
51 | char **ft_split(char const *s, char c)
52 | {
53 | char **tab;
54 | int i;
55 | int j;
56 | int k;
57 |
58 | i = -1;
59 | j = 0;
60 | k = 0;
61 | if (!(tab = malloc(sizeof(char*) * (count_words(s, c) + 1))))
62 | return (NULL);
63 | while (s[++i])
64 | {
65 | if (s[i] != c)
66 | {
67 | if (k == 0)
68 | if (!(tab[j] = (char*)malloc(sizeof(char) * w_s(s, i, c) + 1)))
69 | return (NULL);
70 | tab[j][k] = s[i];
71 | tab[j][++k] = '\0';
72 | }
73 | if ((s[i] == c && s[i + 1] != c && k > 0) && (k = 0) == 0)
74 | j++;
75 | }
76 | tab[count_words(s, c)] = NULL;
77 | return (tab);
78 | }
79 |
--------------------------------------------------------------------------------
/libft/srcs/ft_split_n.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_split_n.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 04:20:42 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/22 04:21:03 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | static size_t count_words(char const *s, char c, int n)
16 | {
17 | int count;
18 | int is_word;
19 |
20 | is_word = 0;
21 | count = 0;
22 | while (*s)
23 | {
24 | if (*s == c)
25 | is_word = 0;
26 | else if (is_word == 0)
27 | {
28 | is_word = 1;
29 | if (count <= n)
30 | count++;
31 | }
32 | s++;
33 | }
34 | return (count);
35 | }
36 |
37 | static size_t w_s(char const *s, int pos, char c, int n)
38 | {
39 | size_t len;
40 |
41 | len = 0;
42 | while (s[pos])
43 | {
44 | if (s[pos] == c && !n)
45 | return (len);
46 | len++;
47 | pos++;
48 | }
49 | return (len);
50 | }
51 |
52 | char **ft_split_n(char const *s, char c, int n)
53 | {
54 | char **tab;
55 | int i;
56 | int j;
57 | int k;
58 |
59 | i = -1;
60 | j = 0;
61 | k = 0;
62 | if (!(tab = malloc(sizeof(char*) * (count_words(s, c, n) + 1))))
63 | return (NULL);
64 | while (s[++i])
65 | {
66 | if (s[i] != c || j == n)
67 | {
68 | if (k == 0 && j <= n)
69 | if (!(tab[j] = malloc(sizeof(char) * w_s(s, i, c, j % n) + 1)))
70 | return (NULL);
71 | tab[j][k] = s[i];
72 | tab[j][++k] = '\0';
73 | }
74 | if (s[i] == c && k > 0 && j < n && (k = 0) == 0)
75 | j++;
76 | }
77 | tab[count_words(s, c, n)] = NULL;
78 | return (tab);
79 | }
80 |
--------------------------------------------------------------------------------
/libft/srcs/ft_ssplit.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_ssplit.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/15 16:23:19 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/15 18:32:15 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | static int in_s(char c, char *str)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (str[i])
21 | {
22 | if (str[i] == c)
23 | return (1);
24 | i++;
25 | }
26 | return (0);
27 | }
28 |
29 | static size_t count_words(char const *s, char *set)
30 | {
31 | int count;
32 | int is_word;
33 |
34 | is_word = 0;
35 | count = 0;
36 | while (*s)
37 | {
38 | if (in_s(*s, set))
39 | is_word = 0;
40 | else if (is_word == 0)
41 | {
42 | is_word = 1;
43 | count++;
44 | }
45 | s++;
46 | }
47 | return (count);
48 | }
49 |
50 | static size_t w_s(char const *s, int pos, char *set)
51 | {
52 | size_t len;
53 |
54 | len = 0;
55 | while (s[pos])
56 | {
57 | if (in_s(s[pos], set))
58 | return (len);
59 | len++;
60 | pos++;
61 | }
62 | return (len);
63 | }
64 |
65 | char **ft_ssplit(char const *s, char *set)
66 | {
67 | char **tab;
68 | int i;
69 | int j;
70 | int k;
71 |
72 | i = -1;
73 | j = 0;
74 | k = 0;
75 | if (!(tab = malloc(sizeof(char*) * (count_words(s, set) + 1))))
76 | return (NULL);
77 | while (s[++i])
78 | {
79 | if (!in_s(s[i], set))
80 | {
81 | if (k == 0)
82 | if (!(tab[j] = malloc(sizeof(char) * w_s(s, i, set) + 1)))
83 | return (NULL);
84 | tab[j][k] = s[i];
85 | tab[j][++k] = '\0';
86 | }
87 | if ((in_s(s[i], set) && !in_s(s[i + 1], set) && k > 0) && (k = 0) == 0)
88 | j++;
89 | }
90 | tab[count_words(s, set)] = NULL;
91 | return (tab);
92 | }
93 |
--------------------------------------------------------------------------------
/libft/srcs/ft_str_c_count.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_str_c_count.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/19 18:10:30 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/19 18:16:35 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_str_c_count(char *str, char c)
16 | {
17 | int i;
18 | int count;
19 |
20 | i = 0;
21 | count = 0;
22 | while (str[i])
23 | {
24 | if (str[i] == c)
25 | count++;
26 | i++;
27 | }
28 | return (count);
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_str_end.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_str_end.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/23 17:40:29 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/24 12:13:12 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_str_end(char *str, char *end)
16 | {
17 | int i;
18 | int j;
19 |
20 | if (!str)
21 | return (0);
22 | i = ft_strlen(str) - 1;
23 | j = ft_strlen(end) - 1;
24 | if (i < j)
25 | return (0);
26 | while (str[i] && end[j])
27 | {
28 | if (str[i] != end[j])
29 | return (0);
30 | j--;
31 | i--;
32 | }
33 | return (1);
34 | }
35 |
--------------------------------------------------------------------------------
/libft/srcs/ft_str_isdigit.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_str_isdigit.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/22 11:47:37 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/22 11:50:05 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_str_isdigit(char *str)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (str[i])
21 | {
22 | if (!ft_isdigit(str[i]))
23 | return (0);
24 | i++;
25 | }
26 | return (1);
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strcat.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strcat.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 08:51:46 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:07:12 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strcat(char *dst, const char *src)
16 | {
17 | int i;
18 | int j;
19 |
20 | i = ft_strlen(dst);
21 | j = 0;
22 | while (src[j])
23 | {
24 | dst[i] = src[j];
25 | i++;
26 | j++;
27 | }
28 | dst[i] = '\0';
29 | return (dst);
30 | }
31 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 13:50:05 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 15:24:35 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strchr(const char *s, int c)
16 | {
17 | while (*s)
18 | {
19 | if (*s == c)
20 | return ((char*)s);
21 | s++;
22 | }
23 | if (*s == c)
24 | return ((char*)s);
25 | return (NULL);
26 | }
27 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strcmp.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strcmp.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:09:23 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:38:27 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_strcmp(const char *s1, const char *s2)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (s1[i] == s2[i] && s1[i] & s2[i])
21 | i++;
22 | return ((unsigned char)s1[i] - (unsigned char)s2[i]);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strcpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strcpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 08:55:35 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:42:12 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strcpy(char *dst, const char *src)
16 | {
17 | int i;
18 |
19 | i = 0;
20 | while (src[i])
21 | {
22 | dst[i] = src[i];
23 | i++;
24 | }
25 | dst[i] = '\0';
26 | return (dst);
27 | }
28 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strdel.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strdel.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:32:58 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 09:42:50 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_strdel(char **as)
16 | {
17 | free(*as);
18 | *as = NULL;
19 | }
20 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strdup.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strdup.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 18:38:42 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 17:45:26 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 | #include
15 |
16 | char *ft_strdup(const char *s1)
17 | {
18 | char *str;
19 | int i;
20 |
21 | i = 0;
22 | if (!(str = (char*)malloc(sizeof(char) * ft_strlen(s1) + 1)))
23 | return (NULL);
24 | while (s1[i])
25 | {
26 | str[i] = (char)s1[i];
27 | i++;
28 | }
29 | str[i] = '\0';
30 | return (str);
31 | }
32 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strequ.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strequ.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/11/06 15:37:41 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/10 23:17:07 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | int ft_strequ(char *s1, char *s2)
14 | {
15 | int i;
16 |
17 | i = 0;
18 | if (!s1 || !s2)
19 | return (0);
20 | while (s1[i] && s2[i] && s1[i] == s2[i])
21 | i++;
22 | if (!s1[i] && !s2[i])
23 | return (1);
24 | return (0);
25 | }
26 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strfree_join.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strfree_join.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: haguerni +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/26 19:11:04 by haguerni #+# #+# */
9 | /* Updated: 2020/02/28 00:27:06 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strfree_join(char const *s1, char const *s2)
16 | {
17 | char *str;
18 |
19 | if (!s2)
20 | {
21 | str = ft_strdup(s1);
22 | free((void*)s1);
23 | return (str);
24 | }
25 | if (!(str = (char*)ft_calloc(1, sizeof(char)
26 | * (ft_strlen(s1) + ft_strlen(s2) + 1))))
27 | return (NULL);
28 | ft_strcpy(str, s1);
29 | ft_strcat(str, s2);
30 | free((void*)s2);
31 | return (str);
32 | }
33 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strjoin.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strjoin.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:28:44 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:23:11 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strjoin(char const *s1, char const *s2)
16 | {
17 | char *str;
18 |
19 | if (!(str = (char*)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2)
20 | + 1))))
21 | return (NULL);
22 | ft_strcpy(str, s1);
23 | ft_strcat(str, s2);
24 | return (str);
25 | }
26 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strjoin_double_free.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strjoin_double_free.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 04:22:58 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/22 04:23:03 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strjoin_double_free(char const *s1, char const *s2)
16 | {
17 | char *str;
18 |
19 | if (!s2)
20 | {
21 | str = ft_strdup(s1);
22 | free((void*)s1);
23 | return (str);
24 | }
25 | if (!(str = (char*)ft_calloc(1, sizeof(char)
26 | * (ft_strlen(s1) + ft_strlen(s2) + 1))))
27 | return (NULL);
28 | ft_strcpy(str, s1);
29 | free((void*)s1);
30 | ft_strcat(str, s2);
31 | free((void*)s2);
32 | return (str);
33 | }
34 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strjoin_free.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strjoin_free.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/03 10:01:47 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/15 13:56:16 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strjoin_free(char const *s1, char const *s2)
16 | {
17 | char *str;
18 |
19 | if (!s2)
20 | {
21 | str = ft_strdup(s1);
22 | free((void*)s1);
23 | return (str);
24 | }
25 | if (!(str = (char*)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2)
26 | + 1))))
27 | return (NULL);
28 | ft_strcpy(str, s1);
29 | free((void*)s1);
30 | ft_strcat(str, s2);
31 | return (str);
32 | }
33 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strlcat.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlcat.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 16:43:43 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 13:41:42 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
16 | {
17 | size_t i;
18 | size_t j;
19 |
20 | i = 0;
21 | j = 0;
22 | while (dst[i])
23 | i++;
24 | if (dstsize)
25 | {
26 | while ((i + j) < (dstsize - 1) && src[j])
27 | {
28 | dst[i + j] = src[j];
29 | j++;
30 | }
31 | dst[i + j] = '\0';
32 | }
33 | while (src[j])
34 | j++;
35 | if (i < dstsize)
36 | return (i + j);
37 | return (dstsize + j);
38 | }
39 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strlcpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlcpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 14:59:04 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 17:06:00 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
16 | {
17 | size_t i;
18 | size_t src_len;
19 |
20 | i = 0;
21 | src_len = ft_strlen(src);
22 | while (src[i] && i + 1 < dstsize)
23 | {
24 | dst[i] = src[i];
25 | i++;
26 | }
27 | if (dstsize != 0)
28 | dst[i] = '\0';
29 | return (src_len);
30 | }
31 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strlen.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlen.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:35:50 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:41:46 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_strlen(const char *s)
16 | {
17 | size_t i;
18 |
19 | i = 0;
20 | while (s[i])
21 | i++;
22 | return (i);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strlen_c.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlen_c.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 11:35:50 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/02 13:44:37 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_strlen_c(char *s, char c)
16 | {
17 | size_t i;
18 |
19 | i = 0;
20 | while (s[i] && s[i] != c)
21 | i++;
22 | return (i);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strmapi.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strmapi.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 13:50:09 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:02:39 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
16 | {
17 | char *str;
18 | unsigned int i;
19 |
20 | i = 0;
21 | if (!f)
22 | return (NULL);
23 | if (!(str = (char*)malloc(sizeof(char) * ft_strlen(s) + 1)))
24 | return (NULL);
25 | while (s[i])
26 | {
27 | str[i] = (*f)(i, s[i]);
28 | i++;
29 | }
30 | str[i] = '\0';
31 | return (str);
32 | }
33 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strncat.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strncat.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:16:30 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:06:14 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strncat(char *s1, const char *s2, size_t n)
16 | {
17 | int i;
18 |
19 | i = ft_strlen(s1);
20 | while (*s2 && n)
21 | {
22 | s1[i] = *s2;
23 | i++;
24 | s2++;
25 | n--;
26 | }
27 | s1[i] = '\0';
28 | return (s1);
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strncmp.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strncmp.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 14:37:59 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 10:38:33 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_strncmp(const char *s1, const char *s2, size_t n)
16 | {
17 | size_t i;
18 |
19 | i = 0;
20 | if (n == 0)
21 | return (0);
22 | while (i + 1 < n && (s1[i] == s2[i] && (s1[i]) && (s2[i])))
23 | i++;
24 | return ((unsigned char)s1[i] - (unsigned char)s2[i]);
25 | }
26 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strncpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strncpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:19:00 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 09:36:10 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strncpy(char *dst, const char *src, size_t len)
16 | {
17 | size_t i;
18 |
19 | i = 0;
20 | while (src[i] && i < len)
21 | {
22 | dst[i] = src[i];
23 | i++;
24 | }
25 | while (i < len)
26 | {
27 | dst[i] = '\0';
28 | i++;
29 | }
30 | return (dst);
31 | }
32 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strndup.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strndup.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/03 10:03:14 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/03 10:03:20 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strndup(const char *s1, int n)
16 | {
17 | char *str;
18 | int i;
19 | int to_malloc;
20 | int len;
21 |
22 | i = 0;
23 | to_malloc = n;
24 | len = ft_strlen(s1);
25 | if (len < n)
26 | to_malloc = len;
27 | if (!(str = (char*)malloc(sizeof(char) * to_malloc + 1)))
28 | return (NULL);
29 | while (s1[i] && i < n)
30 | {
31 | str[i] = (char)s1[i];
32 | i++;
33 | }
34 | str[i] = '\0';
35 | return (str);
36 | }
37 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strnew.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strnew.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 09:30:07 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 09:47:06 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strnew(size_t size)
16 | {
17 | char *str;
18 |
19 | if (!(str = (char *)ft_memalloc(sizeof(char) * size + 1)))
20 | return (NULL);
21 | ft_memset(str, '\0', size);
22 | return (str);
23 | }
24 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strnstr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strnstr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 17:22:54 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 15:26:09 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len)
16 | {
17 | size_t i;
18 | size_t j;
19 |
20 | i = 0;
21 | if (*needle == '\0')
22 | return ((char*)haystack);
23 | while (haystack[i] && i < len)
24 | {
25 | j = 0;
26 | while (haystack[i + j] == needle[j] && i + j < len)
27 | {
28 | if (needle[j + 1] == '\0')
29 | return (&((char*)haystack)[i]);
30 | j++;
31 | }
32 | i++;
33 | }
34 | return (NULL);
35 | }
36 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strrchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strrchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 14:30:23 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 15:25:12 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strrchr(const char *s, int c)
16 | {
17 | char *p;
18 |
19 | p = NULL;
20 | while (*s)
21 | {
22 | if (*s == c)
23 | p = (char*)s;
24 | s++;
25 | }
26 | if (*s == c)
27 | return ((char*)s);
28 | return (p);
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strstr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strstr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 08:49:58 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/08 15:26:16 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strstr(const char *haystack, const char *needle)
16 | {
17 | size_t i;
18 | size_t j;
19 |
20 | i = 0;
21 | if (*needle == '\0')
22 | return ((char*)haystack);
23 | while (haystack[i])
24 | {
25 | j = 0;
26 | while (haystack[i + j] == needle[j])
27 | {
28 | if (needle[j + 1] == '\0')
29 | return (&((char*)haystack)[i]);
30 | j++;
31 | }
32 | i++;
33 | }
34 | return (NULL);
35 | }
36 |
--------------------------------------------------------------------------------
/libft/srcs/ft_strtrim.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strtrim.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:40:41 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/16 22:02:19 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_strtrim(char const *s1, char const *set)
16 | {
17 | int start;
18 | int end;
19 |
20 | start = 0;
21 | end = ft_strlen(s1);
22 | while (ft_is_in_stri(s1[start], (char*)set) >= 0)
23 | start++;
24 | if ((end - start) <= 0)
25 | return (ft_substr(s1, 0, 0));
26 | while (ft_is_in_stri(s1[end - 1], (char*)set) >= 0)
27 | end--;
28 | return (ft_substr(s1, start, end - start));
29 | }
30 |
--------------------------------------------------------------------------------
/libft/srcs/ft_substr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_substr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/08 09:11:28 by sdunckel #+# #+# */
9 | /* Updated: 2019/11/02 12:45:20 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_substr(char const *s, unsigned int start, size_t len)
16 | {
17 | char *str;
18 | int i;
19 |
20 | i = 0;
21 | if (!s)
22 | return (NULL);
23 | if (start > ft_strlen(s))
24 | return (NULL);
25 | if (!(str = ft_calloc(len + 1, sizeof(char))))
26 | return (NULL);
27 | while (s[start] && len)
28 | {
29 | str[i] = s[start];
30 | i++;
31 | start++;
32 | len--;
33 | }
34 | str[i] = '\0';
35 | return (str);
36 | }
37 |
--------------------------------------------------------------------------------
/libft/srcs/ft_tolower.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_tolower.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 13:48:48 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/07 13:49:03 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | int ft_tolower(int c)
14 | {
15 | if (c >= 65 && c <= 90)
16 | return (c + 32);
17 | return (c);
18 | }
19 |
--------------------------------------------------------------------------------
/libft/srcs/ft_toupper.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_toupper.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/07 13:37:22 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/09 11:41:05 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | int ft_toupper(int c)
16 | {
17 | if (c >= 97 && c <= 122)
18 | return (c - 32);
19 | return (c);
20 | }
21 |
--------------------------------------------------------------------------------
/libft/srcs/ft_uintlen.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_uintlen.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/24 22:39:45 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:39:58 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | size_t ft_uintlen(uintmax_t n)
16 | {
17 | size_t len;
18 |
19 | len = 1;
20 | while (n >= 10)
21 | {
22 | n = n / 10;
23 | len++;
24 | }
25 | return (len);
26 | }
27 |
--------------------------------------------------------------------------------
/libft/srcs/ft_uitoa.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_uitoa.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/24 22:38:45 by sdunckel #+# #+# */
9 | /* Updated: 2019/10/24 22:39:01 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | char *ft_uitoa(uintmax_t n)
16 | {
17 | char *str;
18 | int num_len;
19 |
20 | num_len = ft_uintlen(n);
21 | if (!(str = ft_calloc((num_len + 1), sizeof(char))))
22 | return (NULL);
23 | str[num_len] = '\0';
24 | while (num_len)
25 | {
26 | str[--num_len] = n % 10 + 48;
27 | n = n / 10;
28 | }
29 | return (str);
30 | }
31 |
--------------------------------------------------------------------------------
/libft/srcs/get_next_line.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* get_next_line.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/24 22:14:46 by sdunckel #+# #+# */
9 | /* Updated: 2020/01/22 15:54:38 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "get_next_line.h"
14 |
15 | static int handle_line(char *s[], int fd)
16 | {
17 | char *tmp;
18 |
19 | if (ft_is_in_stri('\n', s[fd]) >= 0)
20 | {
21 | tmp = s[fd];
22 | s[fd] = ft_substr(s[fd], ft_is_in_stri('\n', s[fd]) + 1,
23 | ft_strlen(s[fd]));
24 | free(tmp);
25 | }
26 | else if (ft_strlen(s[fd]) > 0)
27 | ft_strdel(&s[fd]);
28 | else
29 | {
30 | ft_strdel(&s[fd]);
31 | return (FINISH);
32 | }
33 | return (SUCCESS);
34 | }
35 |
36 | int get_next_line(int fd, char **line)
37 | {
38 | static char *s[10240];
39 | char buf[BUFFER_SIZE + 1];
40 | int ret;
41 | char *tmp;
42 |
43 | if (fd < 0 || !line || BUFFER_SIZE < 1 || read(fd, buf, 0) < 0)
44 | return (ERROR);
45 | if (!s[fd] && !(s[fd] = ft_calloc(1, sizeof(char *))))
46 | return (ERROR);
47 | while ((ft_is_in_stri('\n', s[fd])) < 0 &&
48 | (ret = read(fd, buf, BUFFER_SIZE)) > 0)
49 | {
50 | buf[ret] = '\0';
51 | tmp = s[fd];
52 | s[fd] = ft_strjoin(s[fd], buf);
53 | free(tmp);
54 | }
55 | if (s[fd])
56 | *line = ft_substr(s[fd], 0, ft_strlen_c(s[fd], '\n'));
57 | if (!handle_line(s, fd))
58 | return (FINISH);
59 | return (SUCCESS);
60 | }
61 |
--------------------------------------------------------------------------------
/srcs/commands/cd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* cd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 19:38:50 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/12 14:55:19 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void edit_pwd(t_minishell *minishell)
16 | {
17 | char *backup;
18 |
19 | backup = ft_strdup(minishell->curdir);
20 | ft_strdel(&minishell->curdir);
21 | if (get_env(minishell, "PWD") && get_env(minishell, "OLDPWD"))
22 | set_env(minishell, "OLDPWD", get_env(minishell, "PWD"));
23 | minishell->curdir = getcwd(NULL, 0);
24 | if (!minishell->curdir)
25 | {
26 | ft_dprintf(2, "cd: error retrieving current directory: getcwd: "
27 | "cannot access parent directories: No such file or directory\n");
28 | minishell->curdir = ft_strjoin_free(backup, "/.");
29 | return ;
30 | }
31 | if (get_env(minishell, "PWD"))
32 | set_env(minishell, "PWD", minishell->curdir);
33 | free(backup);
34 | }
35 |
36 | void cd_cmd(t_minishell *minishell, t_cmd *cmd)
37 | {
38 | t_token *args;
39 |
40 | args = cmd->args;
41 | if (!args)
42 | {
43 | if (chdir(get_env(minishell, "HOME")))
44 | {
45 | ft_dprintf(2, "%s: cd: HOME not set\n", minishell->name);
46 | minishell->exit = 1;
47 | return ;
48 | }
49 | }
50 | else
51 | {
52 | if (chdir(args->word))
53 | {
54 | ft_dprintf(2, "%s: cd: %s: %s\n", minishell->name, args->word,
55 | strerror(errno));
56 | minishell->exit = 1;
57 | return ;
58 | }
59 | }
60 | minishell->exit = 0;
61 | edit_pwd(minishell);
62 | }
63 |
--------------------------------------------------------------------------------
/srcs/commands/echo.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* echo.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 18:33:36 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 17:19:19 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | int ft_strlen_s(char *s)
16 | {
17 | int i;
18 | int count;
19 |
20 | i = 0;
21 | count = 0;
22 | while (s[i])
23 | {
24 | if (s[i] != ' ')
25 | count++;
26 | i++;
27 | }
28 | return (count);
29 | }
30 |
31 | void echo_cmd(t_minishell *minishell, t_cmd *cmd)
32 | {
33 | t_token *args;
34 | int endl;
35 |
36 | args = cmd->args;
37 | endl = 1;
38 | while (args && ft_strequ(args->word, "-n"))
39 | {
40 | endl = 0;
41 | args = args->next;
42 | }
43 | while (args)
44 | {
45 | ft_putstr(args->word);
46 | if (args->next)
47 | ft_putchar(' ');
48 | args = args->next;
49 | }
50 | if (endl)
51 | ft_putchar('\n');
52 | minishell->exit = 0;
53 | }
54 |
--------------------------------------------------------------------------------
/srcs/commands/env.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* env.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 20:31:25 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/23 23:29:56 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void env_cmd(t_list **begin)
16 | {
17 | int i;
18 | t_list *tmp;
19 |
20 | i = 0;
21 | tmp = *begin;
22 | if (!*begin)
23 | return ;
24 | while (tmp)
25 | {
26 | if (((t_env*)(tmp->content))->value && !((t_env*)(tmp->content))->tmp)
27 | ft_printf("%s=%s\n", ((t_env*)(tmp->content))->name,
28 | ((t_env*)(tmp->content))->value);
29 | tmp = tmp->next;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/srcs/commands/exit.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* exit.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 20:29:29 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/12 15:06:02 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void exit_cmd3(t_minishell *minishell, int status)
16 | {
17 | ft_strdel(&minishell->name);
18 | ft_strdel(&minishell->curdir);
19 | ft_strdel(&minishell->line);
20 | ft_strdel(&minishell->exit_str);
21 | clear_cmd_list(&minishell->cmd_list, free);
22 | ft_lstclear(&minishell->env_list, free_env2);
23 | clear_token_list(&minishell->token_list, free);
24 | ft_free_split(&minishell->env_array);
25 | ft_free_split(&minishell->bin);
26 | exit(status);
27 | }
28 |
29 | int exit_status(t_minishell *minishell, t_cmd *cmd)
30 | {
31 | if (cmd->args)
32 | {
33 | if ((is_only_digit(((t_token*)cmd->args)->word)
34 | || (((t_token*)cmd->args)->word[0] == '-'
35 | && ft_isdigit(((t_token*)cmd->args)->word[1])))
36 | && ft_strlen(((t_token*)cmd->args)->word) < 18)
37 | minishell->exit = ft_atoi(((t_token*)cmd->args)->word);
38 | else
39 | {
40 | ft_dprintf(2, "%s: %s: %s: %s\n", minishell->name, cmd->cmd,
41 | ((t_token*)cmd->args)->word, "numeric argument required");
42 | minishell->exit = 255;
43 | }
44 | }
45 | return ((int)minishell->exit);
46 | }
47 |
48 | void exit_cmd2(t_minishell *minishell, t_cmd *cmd, int ex)
49 | {
50 | int exit_s;
51 |
52 | exit_s = 0;
53 | !ex ? ft_dprintf(2, "exit\n") : 0;
54 | if (token_list_size(&cmd->args) > 1)
55 | {
56 | ft_dprintf(2, "%s: %s: %s\n", minishell->name, cmd->cmd,
57 | "too many arguments");
58 | minishell->exit = 1;
59 | return ;
60 | }
61 | exit_s = exit_status(minishell, cmd);
62 | exit_cmd3(minishell, exit_s);
63 | }
64 |
65 | void exit_cmd4(t_minishell *minishell)
66 | {
67 | ft_dprintf(2, "exit\n");
68 | exit_cmd3(minishell, minishell->exit);
69 | }
70 |
71 | void exit_cmd(t_minishell *minishell)
72 | {
73 | exit_cmd3(minishell, 1);
74 | }
75 |
--------------------------------------------------------------------------------
/srcs/commands/pwd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* pwd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 20:39:19 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/06 18:29:55 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void pwd_cmd(t_minishell *minishell)
16 | {
17 | ft_putendl(minishell->curdir);
18 | minishell->exit = 0;
19 | }
20 |
--------------------------------------------------------------------------------
/srcs/commands/unset.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* unset.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 20:50:10 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 16:50:24 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | int is_env(t_env *env, char *data)
16 | {
17 | if (ft_strequ(env->name, data))
18 | return (0);
19 | return (1);
20 | }
21 |
22 | int is_valid_env2(char *str)
23 | {
24 | int i;
25 |
26 | i = 0;
27 | while (str[i])
28 | {
29 | if (i == 0 && (ft_isdigit(str[i])))
30 | return (0);
31 | if (!ft_isalnum(str[i]) && str[i] != '_')
32 | return (0);
33 | i++;
34 | }
35 | return (1);
36 | }
37 |
38 | void unset_cmd(t_minishell *minishell, t_cmd *cmd)
39 | {
40 | t_token *args;
41 | char *env;
42 |
43 | args = cmd->args;
44 | minishell->exit = 0;
45 | while (args)
46 | {
47 | if (is_valid_env2(args->word))
48 | {
49 | env = get_env(minishell, args->word);
50 | if (env)
51 | ft_lst_remove_if(&minishell->env_list, args->word, is_env,
52 | free_env);
53 | if (ft_strequ(args->word, "PATH"))
54 | parse_bin(minishell);
55 | }
56 | else
57 | {
58 | ft_dprintf(2, "%s: %s: `%s': %s\n", minishell->name, cmd->cmd,
59 | args->word, "not a valid identifier");
60 | minishell->exit = 1;
61 | }
62 | args = args->next;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/srcs/env/env_utils.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* env_utils.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/14 13:25:08 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/12 12:20:12 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void set_env(t_minishell *minishell, char *env, char *value)
16 | {
17 | t_list *tmp;
18 | char *to_free;
19 | t_env *tmp2;
20 |
21 | tmp = minishell->env_list;
22 | while (tmp)
23 | {
24 | tmp2 = tmp->content;
25 | if (ft_strequ(env, tmp2->name))
26 | {
27 | to_free = tmp2->value;
28 | tmp2->value = ft_strdup(value);
29 | free(to_free);
30 | return ;
31 | }
32 | tmp = tmp->next;
33 | }
34 | }
35 |
36 | t_env *create_env(char **split, int ex)
37 | {
38 | t_env *env;
39 |
40 | if (!(env = ft_calloc(1, sizeof(t_env))))
41 | exit_cmd(g_minishell);
42 | env->name = ft_strdup(split[0]);
43 | if (split[1])
44 | env->value = ft_strndup(split[1], 4096);
45 | else if (ex)
46 | env->value = NULL;
47 | else
48 | env->value = ft_strdup("");
49 | env->tmp = 0;
50 | return (env);
51 | }
52 |
53 | void env_init(t_minishell *minishell, char **env)
54 | {
55 | int i;
56 | char **tmp_split;
57 | t_env *tmp;
58 |
59 | i = 0;
60 | while (env[i])
61 | {
62 | tmp_split = ft_split(env[i], '=');
63 | if (!(tmp = create_env(tmp_split, 0)))
64 | {
65 | ft_free_split(&tmp_split);
66 | return ;
67 | }
68 | ft_lstadd_back(&minishell->env_list, ft_lstnew(tmp));
69 | ft_free_split(&tmp_split);
70 | i++;
71 | }
72 | minishell->env_array = env_to_array(minishell);
73 | if (get_env(minishell, "PATH"))
74 | parse_bin(minishell);
75 | }
76 |
77 | char **env_to_array(t_minishell *minishell)
78 | {
79 | int i;
80 | t_list *tmp;
81 | char **array;
82 |
83 | if (!(array = ft_calloc(1, sizeof(char*) * (ft_lstsize(minishell->env_list)
84 | + 1))))
85 | exit_cmd(minishell);
86 | i = 0;
87 | tmp = minishell->env_list;
88 | while (tmp)
89 | {
90 | array[i] = ft_strjoin(((t_env*)(tmp->content))->name, "=");
91 | array[i] = ft_strjoin_free(array[i], ((t_env*)(tmp->content))->value);
92 | i++;
93 | tmp = tmp->next;
94 | }
95 | array[i] = NULL;
96 | if (minishell->env_array)
97 | ft_free_split(&minishell->env_array);
98 | return (array);
99 | }
100 |
101 | char *get_env(t_minishell *minishell, char *env)
102 | {
103 | t_list *tmp;
104 |
105 | tmp = minishell->env_list;
106 | while (tmp)
107 | {
108 | if (ft_strequ(env, ((t_env*)(tmp->content))->name))
109 | return (((t_env*)(tmp->content))->value);
110 | tmp = tmp->next;
111 | }
112 | if (ft_strequ(env, "?"))
113 | {
114 | ft_strdel(&minishell->exit_str);
115 | minishell->exit_str = ft_itoa(minishell->exit);
116 | return (minishell->exit_str);
117 | }
118 | return (NULL);
119 | }
120 |
--------------------------------------------------------------------------------
/srcs/env/env_utils2.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* env_utils2.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/23 22:52:55 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 19:52:30 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void env_cmd_export(t_list **begin)
16 | {
17 | int i;
18 | t_list *tmp;
19 |
20 | i = 0;
21 | tmp = *begin;
22 | if (!*begin)
23 | return ;
24 | while (tmp)
25 | {
26 | if (!ft_strequ(((t_env*)(tmp->content))->name, "_"))
27 | {
28 | if (((t_env*)(tmp->content))->value
29 | && !((t_env*)(tmp->content))->tmp)
30 | ft_printf("declare -x %s=\"%s\"\n",
31 | ((t_env*)(tmp->content))->name,
32 | ((t_env*)(tmp->content))->value);
33 | else if (!((t_env*)(tmp->content))->value
34 | && !((t_env*)(tmp->content))->tmp)
35 | ft_printf("declare -x %s\n",
36 | ((t_env*)(tmp->content))->name);
37 | }
38 | tmp = tmp->next;
39 | }
40 | }
41 |
42 | int env_valid_character(char *str)
43 | {
44 | int i;
45 |
46 | i = 0;
47 | if (!str)
48 | return (0);
49 | while (str[i])
50 | {
51 | if (i == 0 && ft_isdigit(str[i]))
52 | return (0);
53 | if (!ft_isalnum(str[i]) && str[i] != '_')
54 | return (0);
55 | i++;
56 | }
57 | return (1);
58 | }
59 |
60 | int is_valid_env(char *str)
61 | {
62 | int i;
63 | int eq_count;
64 |
65 | i = 0;
66 | eq_count = 0;
67 | while (str[i])
68 | {
69 | if (i == 0 && (ft_isdigit(str[i]) || str[i] == '='))
70 | return (0);
71 | if (!ft_isalnum(str[i]) && str[i] != '_' && str[i] != '='
72 | && str[i] != '+' && str[i] != '\'' && str[i] != '\"')
73 | return (0);
74 | if ((str[i] == '\'' || str[i] == '\"') && eq_count < 1)
75 | return (0);
76 | if (eq_count == 0 && str[i] == '+' && str[i + 1] != '=')
77 | return (0);
78 | if (str[i] == '=')
79 | eq_count++;
80 | i++;
81 | }
82 | if (eq_count)
83 | return (1);
84 | return (0);
85 | }
86 |
87 | t_env *create_tmp_env(char **split)
88 | {
89 | t_env *env;
90 |
91 | if (!(env = ft_calloc(1, sizeof(t_env))))
92 | exit_cmd(g_minishell);
93 | env->name = ft_strdup(split[0]);
94 | if (split[1])
95 | env->value = ft_strndup(split[1], 4096);
96 | else
97 | env->value = ft_strdup("");
98 | env->tmp = 1;
99 | return (env);
100 | }
101 |
102 | void add_tmp_env_variable(t_minishell *minishell, t_cmd *cmd)
103 | {
104 | char **split;
105 | t_list *tmp;
106 | char *to_free;
107 | int join;
108 |
109 | join = 0;
110 | tmp = cmd->env_list;
111 | while (tmp)
112 | {
113 | split = ft_split_n(tmp->content, '=', 1);
114 | if (ft_str_end(split[0], "+"))
115 | {
116 | to_free = split[0];
117 | split[0] = ft_strndup(split[0], ft_strlen(split[0]) - 1);
118 | free(to_free);
119 | join = 1;
120 | }
121 | if (!(modify_env_list(minishell, split, 2, join)))
122 | ft_lstadd_back(&minishell->env_list,
123 | ft_lstnew(create_tmp_env(split)));
124 | ft_free_split(&split);
125 | minishell->exit = 0;
126 | tmp = tmp->next;
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/srcs/parsing/bin_path.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* bin_path.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/15 16:03:58 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/03 20:30:52 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void parse_bin(t_minishell *minishell)
16 | {
17 | int i;
18 | char **bin;
19 | char *ban;
20 |
21 | i = 0;
22 | ban = NULL;
23 | ban = get_env(minishell, "PATH");
24 | bin = NULL;
25 | if (ban)
26 | bin = ft_split(ban, ':');
27 | while (bin && bin[i])
28 | {
29 | bin[i] = ft_strjoin_free(bin[i], "/");
30 | i++;
31 | }
32 | ft_free_split(&minishell->bin);
33 | minishell->bin = bin;
34 | }
35 |
36 | char *try_opendir(char *path, char *cmd)
37 | {
38 | DIR *dir;
39 | struct dirent *dent;
40 |
41 | dir = opendir(path);
42 | if (dir)
43 | {
44 | while ((dent = readdir(dir)))
45 | {
46 | if (ft_strequ(dent->d_name, cmd))
47 | {
48 | if (dir)
49 | closedir(dir);
50 | return (ft_strjoin(path, cmd));
51 | }
52 | }
53 | }
54 | if (dir)
55 | closedir(dir);
56 | return (NULL);
57 | }
58 |
59 | char *get_bin(t_minishell *minishell, char *cmd)
60 | {
61 | char *try;
62 | int i;
63 |
64 | i = 0;
65 | if (!cmd)
66 | return (NULL);
67 | if (ft_is_in_stri('/', cmd) >= 0)
68 | return (ft_strdup(cmd));
69 | if (!minishell->bin)
70 | return (ft_strjoin("./", cmd));
71 | while (minishell->bin && minishell->bin[i])
72 | {
73 | try = try_opendir(minishell->bin[i], cmd);
74 | if (try)
75 | return (try);
76 | i++;
77 | }
78 | return (NULL);
79 | }
80 |
--------------------------------------------------------------------------------
/srcs/parsing/cmds.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* cmds.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/05 12:56:11 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 00:49:29 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void add_cmd_list(t_cmd **begin, t_cmd *new)
16 | {
17 | t_cmd *tmp;
18 |
19 | if (!new || !begin)
20 | return ;
21 | new->cmd = supp_newline(new->cmd);
22 | if (*begin)
23 | {
24 | tmp = *begin;
25 | while (tmp->next)
26 | {
27 | tmp->next->prev = tmp;
28 | tmp = tmp->next;
29 | }
30 | tmp->next = new;
31 | new->prev = tmp;
32 | new->next = NULL;
33 | }
34 | else
35 | *begin = new;
36 | }
37 |
38 | char **args_to_array(t_minishell *minishell, t_cmd *cmd)
39 | {
40 | char **array;
41 | int i;
42 | t_token *tmp;
43 |
44 | i = 1;
45 | if (!(array = ft_calloc(1, sizeof(char *)
46 | * (token_list_size(&cmd->args) + 2))))
47 | exit_cmd(minishell);
48 | array[0] = cmd->cmd;
49 | tmp = cmd->args;
50 | while (tmp)
51 | {
52 | array[i] = tmp->word;
53 | tmp = tmp->next;
54 | i++;
55 | }
56 | array[i] = NULL;
57 | return (array);
58 | }
59 |
60 | void clear_cmd_list_free(t_cmd *cmd)
61 | {
62 | ft_strdel(&cmd->cmd);
63 | ft_strdel(&cmd->bin);
64 | ft_free_split(&cmd->args_array);
65 | ft_lstclear(&cmd->env_list, free);
66 | }
67 |
68 | void clear_cmd_list(t_cmd **begin, void (*del)(void *))
69 | {
70 | t_cmd *tmp;
71 | t_cmd *tmp2;
72 | t_token *args;
73 | t_token *args2;
74 |
75 | if (!begin || !del)
76 | return ;
77 | tmp = *begin;
78 | while (tmp)
79 | {
80 | clear_cmd_list_free(tmp);
81 | args = tmp->args;
82 | while (args)
83 | {
84 | ft_strdel(&args->word);
85 | args2 = args->next;
86 | del(args);
87 | args = args2;
88 | }
89 | tmp2 = tmp->next;
90 | free(tmp);
91 | tmp = tmp2;
92 | }
93 | *begin = NULL;
94 | }
95 |
--------------------------------------------------------------------------------
/srcs/parsing/parsing_cmds.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* parsing_cmds.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/06 15:13:55 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 01:21:43 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void start_parse(t_minishell *minishell, char *str)
16 | {
17 | char *token;
18 | t_token *tmp;
19 |
20 | if (!is_only_ascii(str))
21 | {
22 | ft_dprintf(2, "%s: invalid ascii characters found in string\n",
23 | minishell->name);
24 | return ;
25 | }
26 | split_tokens(minishell, str);
27 | token = iter_tokens(minishell);
28 | if (token)
29 | {
30 | ft_dprintf(2, "%s: syntax error near unexpected token `%s'\n",
31 | minishell->name, token);
32 | return ;
33 | }
34 | tmp = minishell->token_list;
35 | while (tmp)
36 | parse_tokens(minishell, &tmp);
37 | }
38 |
39 | int parse_tokens2(t_token **tmp, t_cmd *cmd)
40 | {
41 | if ((*tmp)->type == T_WORD && (cmd->cmd || ((*tmp)->prev
42 | && (*tmp)->prev->type == T_REDIRECT)))
43 | add_token_list(&cmd->args,
44 | create_arg_token((*tmp)->word, (*tmp)->type));
45 | if ((*tmp)->type == T_WORD && !cmd->cmd && (((*tmp)->prev
46 | && (*tmp)->prev->type != T_REDIRECT) || !(*tmp)->prev))
47 | cmd->cmd = ft_strdup((*tmp)->word);
48 | if ((*tmp)->type == T_REDIRECT)
49 | add_token_list(&cmd->args,
50 | create_arg_token((*tmp)->word, (*tmp)->type));
51 | if ((*tmp)->type == T_ENV && !cmd->cmd)
52 | ft_lstadd_back(&cmd->env_list, ft_lstnew(ft_strdup((*tmp)->word)));
53 | if ((*tmp)->type == T_PIPE)
54 | {
55 | cmd->type = T_PIPE;
56 | *tmp = (*tmp)->next;
57 | return (0);
58 | }
59 | if ((*tmp)->type == T_SEP)
60 | {
61 | cmd->type = T_SEP;
62 | *tmp = (*tmp)->next;
63 | return (0);
64 | }
65 | return (1);
66 | }
67 |
68 | void parse_tokens(t_minishell *minishell, t_token **tmp)
69 | {
70 | t_cmd *cmd;
71 |
72 | if ((*tmp)->type == T_NEWLINE)
73 | {
74 | *tmp = (*tmp)->next;
75 | return ;
76 | }
77 | if (!(cmd = ft_calloc(1, sizeof(t_cmd))))
78 | return ;
79 | while (*tmp)
80 | {
81 | if (!parse_tokens2(tmp, cmd))
82 | break ;
83 | if ((*tmp)->type == T_ENV && cmd->cmd)
84 | add_token_list(&cmd->args,
85 | create_arg_token((*tmp)->word, (*tmp)->type));
86 | *tmp = (*tmp)->next;
87 | }
88 | add_cmd_list(&minishell->cmd_list, cmd);
89 | }
90 |
--------------------------------------------------------------------------------
/srcs/parsing/token.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* token.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/02 16:36:48 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/23 21:26:31 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void add_token_list(t_token **begin, t_token *new)
16 | {
17 | t_token *tmp;
18 |
19 | if (!new || !begin)
20 | return ;
21 | if (*begin)
22 | {
23 | tmp = *begin;
24 | while (tmp->next)
25 | {
26 | tmp->next->prev = tmp;
27 | tmp = tmp->next;
28 | }
29 | tmp->next = new;
30 | new->prev = tmp;
31 | new->next = NULL;
32 | }
33 | else
34 | *begin = new;
35 | }
36 |
37 | int token_list_size(t_token **begin)
38 | {
39 | t_token *tmp;
40 | int i;
41 |
42 | i = 0;
43 | tmp = *begin;
44 | while (tmp)
45 | {
46 | tmp = tmp->next;
47 | i++;
48 | }
49 | return (i);
50 | }
51 |
52 | void clear_token_list(t_token **begin, void (*del)(void *))
53 | {
54 | t_token *tmp;
55 | t_token *tmp2;
56 |
57 | if (!begin || !del)
58 | return ;
59 | tmp = *begin;
60 | while (tmp)
61 | {
62 | ft_strdel(&tmp->word);
63 | tmp2 = tmp->next;
64 | free(tmp);
65 | tmp = tmp2;
66 | }
67 | *begin = NULL;
68 | }
69 |
70 | t_token *create_token(t_minishell *minishell, int i)
71 | {
72 | t_token *new;
73 |
74 | if (!minishell->count)
75 | return (NULL);
76 | if (!(new = ft_calloc(1, sizeof(t_token))))
77 | exit_cmd(minishell);
78 | new->word = ft_substr(minishell->line, i - minishell->count,
79 | minishell->count);
80 | minishell->count = 0;
81 | return (new);
82 | }
83 |
84 | t_token *create_arg_token(char *word, int type)
85 | {
86 | t_token *new;
87 |
88 | if (!(new = ft_calloc(1, sizeof(t_token))))
89 | exit_cmd(g_minishell);
90 | new->word = ft_strdup(word);
91 | new->type = type;
92 | return (new);
93 | }
94 |
--------------------------------------------------------------------------------
/srcs/parsing/token2.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* token2.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 22:08:23 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/23 22:11:24 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void add_token_front(t_token **begin, t_token *new)
16 | {
17 | if (*begin)
18 | {
19 | new->next = *begin;
20 | (*begin)->prev = new;
21 | *begin = new;
22 | }
23 | else
24 | *begin = new;
25 | }
26 |
27 | t_token *token_split_to_list(char **split)
28 | {
29 | t_token *new;
30 | int i;
31 | int count;
32 |
33 | i = 0;
34 | count = ft_count_split(split);
35 | new = NULL;
36 | while (i < count)
37 | {
38 | add_token_list(&new, create_arg_token(split[i], T_WORD));
39 | i++;
40 | }
41 | return (new);
42 | }
43 |
--------------------------------------------------------------------------------
/srcs/parsing/wildcard_bonus.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* wildcard_bonus.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 00:13:06 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 20:00:01 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | int count_slash(char *s)
16 | {
17 | int i;
18 | int count;
19 |
20 | i = 0;
21 | count = 0;
22 | while (s[i])
23 | {
24 | if (s[i] == '/' && i != 0)
25 | count++;
26 | i++;
27 | }
28 | return (count);
29 | }
30 |
31 | char *create_wildpath(char *s)
32 | {
33 | int i;
34 | char *dest;
35 |
36 | i = 0;
37 | if (!(dest = (char *)malloc(ft_strlen_c(s, '*') + 2)))
38 | return (NULL);
39 | while (s[i] && (s[i] == '/' || s[i] == '.'))
40 | {
41 | dest[i] = s[i];
42 | i++;
43 | }
44 | if (i == 0 || s[i - 1] == '/')
45 | {
46 | dest[i] = '.';
47 | i++;
48 | }
49 | dest[i] = '\0';
50 | return (dest);
51 | }
52 |
53 | int ft_strmatch(char *s1, char *s2, int i, int j)
54 | {
55 | int k;
56 |
57 | if ((!s1[i] || (!s1[i + 1] && s1[i] == '*')) && !s2[j])
58 | return (1);
59 | if (!s2[j])
60 | return (0);
61 | if (s1[i] == s2[j])
62 | return (ft_strmatch(s1, s2, i + 1, j + 1));
63 | k = j;
64 | if (s1[i] == '*')
65 | while (s2[k])
66 | if (ft_strmatch(s1, s2, i + 1, k++))
67 | return (1);
68 | if (s1[i] == '*' && s1[i + 1] == s2[j + 1])
69 | return (ft_strmatch(s1, s2, i + 1, j + 1));
70 | if (s1[i] == '*')
71 | return (ft_strmatch(s1, s2, i, j + 1));
72 | return (0);
73 | }
74 |
75 | void add_arg(t_token *arg, char *tmp)
76 | {
77 | t_token *new;
78 | char *word;
79 |
80 | tmp += 2;
81 | word = ft_strdup(tmp);
82 | new = create_arg_token(word, T_WORD);
83 | free(word);
84 | new->prev = arg;
85 | new->next = arg->next;
86 | new->next ? new->next->prev = new : 0;
87 | arg->next = new;
88 | arg->type = 11;
89 | }
90 |
91 | void process_wildcard(t_token *arg, char *path, int i, char **split)
92 | {
93 | DIR *dir;
94 | struct dirent *dent;
95 | char *tmp;
96 | int slash;
97 |
98 | slash = count_slash(path);
99 | !i ? i += ft_strlen(path) - 1 : 0;
100 | dir = opendir(path);
101 | while (arg->word[i] && arg->word[i] != '/')
102 | i++;
103 | while (split[slash] && dir && (dent = readdir(dir)))
104 | {
105 | tmp = ft_strjoin("/", dent->d_name);
106 | tmp = ft_strfree_join(path, tmp);
107 | if ((!arg->word[i] || !arg->word[i + 1]) && ft_strmatch(split[slash],
108 | dent->d_name, 0, 0) && (ft_strequ(dent->d_name, split[slash]) ||
109 | dent->d_name[0] != '.' || split[slash][0] == '.'))
110 | add_arg(arg, tmp);
111 | else if (ft_strmatch(split[slash], dent->d_name, 0, 0) &&
112 | (ft_strequ(dent->d_name, split[slash]) || dent->d_name[0] != '.' ||
113 | split[slash][0] == '.'))
114 | process_wildcard(arg, tmp, i + 1, split);
115 | free(tmp);
116 | }
117 | dir ? closedir(dir) : 0;
118 | }
119 |
--------------------------------------------------------------------------------
/srcs/termcaps/cursor.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* cursor.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/26 18:25:25 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/10 16:25:01 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void move_cursor_left(void)
16 | {
17 | if (g_tc->cur_pos == 0)
18 | return ;
19 | g_tc->cur_pos--;
20 | (g_tc->start_col + g_tc->cur_pos + g_tc->plen + 1) % g_tc->col == 0 ?
21 | g_tc->currow -= 1 : 0;
22 | (g_tc->start_col + g_tc->cur_pos + g_tc->plen + 1) % g_tc->col == 0 ?
23 | g_tc->mod_offset += 1 : 0;
24 | tputs(tgoto(g_tc->cm, (g_tc->start_col + g_tc->cur_pos + g_tc->plen) %
25 | g_tc->col, g_tc->currow), 1, putchar_tc);
26 | }
27 |
28 | void move_cursor_right(void)
29 | {
30 | if (!g_minishell->line)
31 | return ;
32 | if (g_tc->cur_pos == (int)ft_strlen(g_minishell->line))
33 | return ;
34 | g_tc->cur_pos++;
35 | (g_tc->start_col + g_tc->cur_pos + g_tc->plen) % g_tc->col == 0 ?
36 | g_tc->currow += 1 : 0;
37 | (g_tc->start_col + g_tc->cur_pos + g_tc->plen) % g_tc->col == 0 ?
38 | g_tc->mod_offset -= 1 : 0;
39 | tputs(tgoto(g_tc->cm, (g_tc->start_col + g_tc->cur_pos + g_tc->plen) %
40 | g_tc->col, g_tc->currow), 1, putchar_tc);
41 | }
42 |
43 | void get_cursor_position(int *col, int *rows)
44 | {
45 | int a;
46 | int i;
47 | char buf[255];
48 | int ret;
49 |
50 | a = 0;
51 | i = 1;
52 | write(0, "\033[6n", 4);
53 | ret = read(0, buf, 254);
54 | buf[ret] = '\0';
55 | while (buf[i])
56 | {
57 | if (buf[i] >= 48 && buf[i] <= 57)
58 | {
59 | if (a == 0)
60 | *rows = ft_atoi(&buf[i]) - 1;
61 | else
62 | *col = ft_atoi(&buf[i]) - 1;
63 | a++;
64 | i += ft_intlen(*col) - 1;
65 | }
66 | i++;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/srcs/termcaps/history.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* history.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/26 17:06:02 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/10 16:32:13 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void up_history(void)
16 | {
17 | if (!g_tc->history)
18 | return ;
19 | if (g_minishell->line && !g_tc->cur_history)
20 | g_tc->backup_cmd = ft_sdpfr(g_minishell->line, g_tc->backup_cmd);
21 | if (!g_tc->cur_history)
22 | g_tc->cur_history = g_tc->history;
23 | else if (g_tc->cur_history->next)
24 | g_tc->cur_history = g_tc->cur_history->next;
25 | if (g_minishell->line)
26 | empty_space(ft_strlen(g_minishell->line) + g_tc->plen);
27 | g_minishell->line = ft_sdpfr(g_tc->cur_history->cmd, g_minishell->line);
28 | g_tc->cur_pos = ft_strlen(g_minishell->line);
29 | g_tc->rowoffset = (g_tc->cur_pos + g_tc->plen) / g_tc->col;
30 | g_tc->mod_offset = 0;
31 | g_tc->currow = g_tc->start_row + g_tc->rowoffset;
32 | }
33 |
34 | void down_history(void)
35 | {
36 | if (!g_tc->cur_history)
37 | return ;
38 | if (g_minishell->line)
39 | empty_space(ft_strlen(g_minishell->line) + g_tc->plen);
40 | if (g_tc->cur_history->prev)
41 | {
42 | g_tc->cur_history = g_tc->cur_history->prev;
43 | g_minishell->line = ft_sdpfr(g_tc->cur_history->cmd, g_minishell->line);
44 | g_tc->cur_pos = ft_strlen(g_minishell->line);
45 | }
46 | else if (g_tc->backup_cmd)
47 | {
48 | g_minishell->line = ft_sdpfr(g_tc->backup_cmd, g_minishell->line);
49 | g_tc->cur_history = NULL;
50 | g_tc->cur_pos = ft_strlen(g_minishell->line);
51 | }
52 | else
53 | {
54 | g_tc->cur_pos = 0;
55 | g_tc->cur_history = NULL;
56 | ft_strdel(&g_minishell->line);
57 | }
58 | g_tc->rowoffset = (g_tc->cur_pos + g_tc->plen) / g_tc->col;
59 | g_tc->mod_offset = 0;
60 | g_tc->currow = g_tc->start_row + g_tc->rowoffset;
61 | }
62 |
63 | void add_cmd_to_history(char *cmd)
64 | {
65 | t_hist *new;
66 |
67 | if (!cmd)
68 | return ;
69 | if (!(new = ft_calloc(1, sizeof(t_hist))))
70 | return ;
71 | new->cmd = ft_strdup(cmd);
72 | add_history_list(&g_tc->history, new);
73 | }
74 |
75 | void add_history_list(t_hist **begin, t_hist *new)
76 | {
77 | if (*begin)
78 | {
79 | new->next = *begin;
80 | (*begin)->prev = new;
81 | *begin = new;
82 | }
83 | else
84 | *begin = new;
85 | }
86 |
87 | void clear_history_list(t_hist **begin)
88 | {
89 | t_hist *tmp;
90 | t_hist *tmp2;
91 |
92 | tmp = *begin;
93 | while (tmp)
94 | {
95 | ft_strdel(&tmp->cmd);
96 | tmp2 = tmp->next;
97 | free(tmp);
98 | tmp = tmp2;
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/srcs/termcaps/init.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* init.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/26 01:58:24 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/02 20:01:17 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void init_term(void)
16 | {
17 | char *name;
18 |
19 | if (!(name = get_env(g_minishell, "TERM")))
20 | name = "xterm";
21 | tgetent(NULL, name);
22 | setupterm(NULL, STDOUT_FILENO, NULL);
23 | tcgetattr(0, &g_tc->term);
24 | tcgetattr(0, &g_tc->term_backup);
25 | g_tc->term.c_lflag = g_tc->term.c_lflag & ~ICANON;
26 | g_tc->term.c_lflag = g_tc->term.c_lflag & ~ECHO;
27 | g_tc->term.c_cc[VMIN] = 1;
28 | g_tc->term.c_cc[VTIME] = 0;
29 | tcsetattr(0, TCSANOW, &g_tc->term);
30 | init_tc();
31 | }
32 |
33 | void cursor_win(void)
34 | {
35 | struct winsize w;
36 |
37 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
38 | g_tc->col = w.ws_col;
39 | g_tc->row = w.ws_row;
40 | }
41 |
42 | void init_tc(void)
43 | {
44 | g_tc->cm = tgetstr("cm", NULL);
45 | g_tc->ce = tgetstr("ce", NULL);
46 | g_tc->dl = tgetstr("DL", NULL);
47 | }
48 |
--------------------------------------------------------------------------------
/srcs/termcaps/move_line.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* move_line.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 15:42:16 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/28 16:49:59 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void move_cursor_begin(void)
16 | {
17 | int offset;
18 |
19 | offset = 0;
20 | if (!g_minishell->line)
21 | return ;
22 | if (g_tc->cur_pos == 0)
23 | return ;
24 | while (g_tc->cur_pos > 0)
25 | move_cursor_left();
26 | }
27 |
28 | void move_cursor_end(void)
29 | {
30 | int offset;
31 | int len;
32 |
33 | offset = 0;
34 | if (!g_minishell->line)
35 | return ;
36 | len = ft_strlen(g_minishell->line);
37 | if (g_tc->cur_pos == len)
38 | return ;
39 | while (g_tc->cur_pos < len)
40 | move_cursor_right();
41 | }
42 |
43 | void move_prev_line(void)
44 | {
45 | int start_col;
46 | int start_row;
47 | int cur_row;
48 | int cur_col;
49 |
50 | start_col = 0;
51 | start_row = 0;
52 | cur_row = 0;
53 | cur_col = 0;
54 | get_cursor_position(&start_col, &start_row);
55 | while (g_tc->cur_pos > 0)
56 | {
57 | get_cursor_position(&cur_col, &cur_row);
58 | if (cur_row != start_row && start_col == cur_col)
59 | break ;
60 | move_cursor_left();
61 | }
62 | }
63 |
64 | void move_next_line(void)
65 | {
66 | int start_col;
67 | int start_row;
68 | int cur_row;
69 | int cur_col;
70 | int len;
71 |
72 | start_col = 0;
73 | start_row = 0;
74 | cur_row = 0;
75 | cur_col = 0;
76 | if (!g_minishell->line)
77 | return ;
78 | len = ft_strlen(g_minishell->line);
79 | get_cursor_position(&start_col, &start_row);
80 | while (g_tc->cur_pos < len)
81 | {
82 | get_cursor_position(&cur_col, &cur_row);
83 | if (cur_row != start_row && start_col == cur_col)
84 | break ;
85 | move_cursor_right();
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/srcs/termcaps/move_word.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* move_word.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 15:41:13 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/28 15:41:30 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void move_prev_word(void)
16 | {
17 | int is_word;
18 |
19 | if (g_tc->cur_pos == 0)
20 | return ;
21 | is_word = 0;
22 | if (!g_minishell->line)
23 | return ;
24 | g_tc->cur_pos--;
25 | while (g_tc->cur_pos > 0)
26 | {
27 | if (!is_word && g_minishell->line[g_tc->cur_pos] != ' ')
28 | is_word = 1;
29 | if (is_word && g_minishell->line[g_tc->cur_pos - 1] == ' ')
30 | break ;
31 | move_cursor_left();
32 | }
33 | }
34 |
35 | void move_next_word(void)
36 | {
37 | int len;
38 | int is_word;
39 |
40 | is_word = 0;
41 | if (!g_minishell->line)
42 | return ;
43 | len = ft_strlen(g_minishell->line);
44 | while (g_tc->cur_pos < len)
45 | {
46 | if (!is_word && g_minishell->line[g_tc->cur_pos] != ' ')
47 | is_word = 1;
48 | if (is_word && g_minishell->line[g_tc->cur_pos] == ' ')
49 | break ;
50 | move_cursor_right();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/srcs/termcaps/utils.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* utils.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 14:02:30 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 14:51:35 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void empty_space(int len)
16 | {
17 | int i;
18 | int j;
19 | char space[1028];
20 |
21 | g_tc->start_row + g_tc->rowoffset >= g_tc->row ?
22 | g_tc->start_row = g_tc->currow - g_tc->rowoffset : 0;
23 | tputs(tgoto(g_tc->cm, g_tc->start_col, g_tc->start_row), 1, putchar_tc);
24 | tputs(g_tc->ce, 1, putchar_tc);
25 | i = 0;
26 | j = 0;
27 | while (j < len)
28 | {
29 | space[i] = ' ';
30 | i++;
31 | j++;
32 | if (i == g_tc->col)
33 | {
34 | write(1, space, i);
35 | i = 0;
36 | }
37 | }
38 | write(1, space, i);
39 | }
40 |
41 | int putchar_tc(int tc)
42 | {
43 | write(1, &tc, 1);
44 | return (0);
45 | }
46 |
47 | void print_char(long c)
48 | {
49 | char c2[2];
50 |
51 | c2[0] = c;
52 | c2[1] = '\0';
53 | if (g_minishell->line)
54 | g_minishell->line = ft_strjoin_middle(g_minishell->line, c2,
55 | g_tc->cur_pos + 1);
56 | else
57 | g_minishell->line = ft_strdup(c2);
58 | g_tc->cur_pos++;
59 | }
60 |
61 | void delete_char(void)
62 | {
63 | char *str;
64 | int len;
65 |
66 | if (!g_minishell->line)
67 | return ;
68 | len = ft_strlen(g_minishell->line);
69 | if (len == 1 && g_tc->cur_pos == 1)
70 | {
71 | ft_strdel(&g_minishell->line);
72 | g_tc->cur_pos--;
73 | return ;
74 | }
75 | if (!(str = (char*)ft_calloc(1, sizeof(char) * (len))))
76 | return ;
77 | ft_strlcpy(str, g_minishell->line, g_tc->cur_pos);
78 | ft_strncat(str, g_minishell->line + g_tc->cur_pos, len - g_tc->cur_pos);
79 | ft_strdel(&g_minishell->line);
80 | g_minishell->line = str;
81 | g_tc->cur_pos--;
82 | }
83 |
84 | char *ft_sdpfr(const char *s1, char *s2)
85 | {
86 | char *str;
87 | int i;
88 |
89 | i = 0;
90 | if (!(str = (char*)malloc(sizeof(char) * ft_strlen(s1) + 1)))
91 | return (NULL);
92 | while (s1[i])
93 | {
94 | str[i] = (char)s1[i];
95 | i++;
96 | }
97 | str[i] = '\0';
98 | free(s2);
99 | s2 = NULL;
100 | return (str);
101 | }
102 |
--------------------------------------------------------------------------------
/srcs/utils/command_process2.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* command_process2.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 04:18:02 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/10 02:27:17 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void add_more_args(t_cmd *cmd, char **split)
16 | {
17 | int i;
18 | char *tmp;
19 |
20 | i = ft_count_split(split) - 1;
21 | tmp = cmd->cmd;
22 | cmd->cmd = ft_strdup(split[0]);
23 | free(tmp);
24 | while (i > 0)
25 | {
26 | add_token_front(&cmd->args, create_arg_token(split[i], T_WORD));
27 | i--;
28 | }
29 | }
30 |
31 | t_token *add_more_args2(t_cmd *cmd, t_token **token, char **split)
32 | {
33 | t_token *new;
34 | t_token *end_new;
35 | t_token *next;
36 |
37 | new = token_split_to_list(split);
38 | end_new = new;
39 | while (end_new->next)
40 | end_new = end_new->next;
41 | next = (*token)->next;
42 | end_new->next = next;
43 | if ((*token)->next)
44 | (*token)->next->prev = end_new;
45 | if ((*token)->prev)
46 | (*token)->prev->next = new;
47 | else
48 | {
49 | cmd->args = new;
50 | free((*token)->word);
51 | free(*token);
52 | return (end_new);
53 | }
54 | free((*token)->word);
55 | free(*token);
56 | ft_free_split(&split);
57 | return (end_new);
58 | }
59 |
60 | void process_args2(t_cmd *cmd)
61 | {
62 | int env;
63 | char **split;
64 |
65 | env = 0;
66 | if (cmd->cmd && cmd->cmd[0] == '$')
67 | env = 1;
68 | if (cmd->cmd)
69 | cmd->cmd = handle_quotes(cmd->cmd, 1);
70 | if (env)
71 | {
72 | split = ft_ssplit(cmd->cmd, " \n");
73 | if (ft_count_split(split) > 1)
74 | add_more_args(cmd, split);
75 | ft_free_split(&split);
76 | }
77 | if (env && ft_strlen(cmd->cmd) == 0)
78 | ft_strdel(&cmd->cmd);
79 | }
80 |
81 | void process_args_env(t_cmd *cmd)
82 | {
83 | t_list *tmp;
84 |
85 | tmp = cmd->env_list;
86 | while (tmp)
87 | {
88 | tmp->content = handle_quotes(tmp->content, 1);
89 | tmp = tmp->next;
90 | }
91 | }
92 |
93 | void process_args(t_cmd *cmd)
94 | {
95 | t_token *tmp;
96 | char **split;
97 | int env;
98 |
99 | tmp = cmd->args;
100 | while (tmp)
101 | {
102 | env = 0;
103 | if (tmp->word && tmp->word[0] == '$')
104 | env = 1;
105 | tmp->word = handle_quotes(tmp->word, 1);
106 | if (env)
107 | {
108 | split = ft_ssplit(tmp->word, " \n");
109 | if (ft_count_split(split) > 1)
110 | tmp = add_more_args2(cmd, &tmp, split);
111 | ft_free_split(&split);
112 | }
113 | if (env && ft_strlen(tmp->word) == 0)
114 | remove_redirect(tmp, &cmd->args);
115 | tmp = tmp->next;
116 | }
117 | process_args2(cmd);
118 | }
119 |
--------------------------------------------------------------------------------
/srcs/utils/command_process2_bonus.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* command_process2_bonus.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 00:21:50 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/09 18:10:14 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void add_more_args(t_cmd *cmd, char **split)
16 | {
17 | int i;
18 | char *tmp;
19 |
20 | i = ft_count_split(split) - 1;
21 | tmp = cmd->cmd;
22 | cmd->cmd = ft_strdup(split[0]);
23 | free(tmp);
24 | while (i > 0)
25 | {
26 | add_token_front(&cmd->args, create_arg_token(split[i], T_WORD));
27 | i--;
28 | }
29 | }
30 |
31 | t_token *add_more_args2(t_cmd *cmd, t_token **token, char **split)
32 | {
33 | t_token *new;
34 | t_token *end_new;
35 | t_token *next;
36 |
37 | new = token_split_to_list(split);
38 | end_new = new;
39 | while (end_new->next)
40 | end_new = end_new->next;
41 | next = (*token)->next;
42 | end_new->next = next;
43 | if ((*token)->next)
44 | (*token)->next->prev = end_new;
45 | if ((*token)->prev)
46 | (*token)->prev->next = new;
47 | else
48 | {
49 | cmd->args = new;
50 | free((*token)->word);
51 | free(*token);
52 | return (end_new);
53 | }
54 | free((*token)->word);
55 | free(*token);
56 | ft_free_split(&split);
57 | return (end_new);
58 | }
59 |
60 | void process_args2(t_cmd *cmd)
61 | {
62 | int env;
63 | char **split;
64 |
65 | env = 0;
66 | if (cmd->cmd && cmd->cmd[0] == '$')
67 | env = 1;
68 | if (cmd->cmd)
69 | cmd->cmd = handle_quotes(cmd->cmd, 1);
70 | if (env)
71 | {
72 | split = ft_ssplit(cmd->cmd, " \n");
73 | if (ft_count_split(split) > 1)
74 | add_more_args(cmd, split);
75 | ft_free_split(&split);
76 | }
77 | }
78 |
79 | void process_args_env(t_cmd *cmd)
80 | {
81 | t_list *tmp;
82 |
83 | tmp = cmd->env_list;
84 | while (tmp)
85 | {
86 | tmp->content = handle_quotes(tmp->content, 1);
87 | tmp = tmp->next;
88 | }
89 | }
90 |
91 | void process_args(t_cmd *cmd)
92 | {
93 | t_token *tmp;
94 | char **split;
95 | int env;
96 |
97 | tmp = cmd->args;
98 | while (tmp && (env = 0) == 0)
99 | {
100 | if (ft_is_in_stri('*', tmp->word) > -1)
101 | handle_wild(tmp);
102 | tmp->type == 11 ? remove_redirect(tmp, &cmd->args) : 0;
103 | if (tmp->word && tmp->word[0] == '$')
104 | env = 1;
105 | if (!tmp->prev || !ft_strequ(tmp->prev->word, "<<"))
106 | tmp->word = handle_quotes(tmp->word, 1);
107 | if (env)
108 | {
109 | split = ft_ssplit(tmp->word, " \n");
110 | if (ft_count_split(split) > 1)
111 | tmp = add_more_args2(cmd, &tmp, split);
112 | ft_free_split(&split);
113 | }
114 | tmp = tmp->next;
115 | }
116 | process_args2(cmd);
117 | }
118 |
--------------------------------------------------------------------------------
/srcs/utils/errors.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* errors.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: haguerni +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/08 18:49:44 by haguerni #+# #+# */
9 | /* Updated: 2020/03/03 19:07:50 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void handle_errno(t_minishell *minishell, char *cmd, int type)
16 | {
17 | if (type == 14)
18 | ft_dprintf(2, "%s: %s: %s\n", minishell->name, cmd,
19 | "command not found");
20 | else
21 | ft_dprintf(2, "%s: %s: %s\n", minishell->name, cmd, strerror(errno));
22 | minishell->exit = 127;
23 | }
24 |
--------------------------------------------------------------------------------
/srcs/utils/free_utils.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* free_utils.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 04:14:18 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/04 00:35:55 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void free_cmd(void *cmd)
16 | {
17 | t_cmd *tmp;
18 |
19 | tmp = cmd;
20 | ft_strdel(&tmp->cmd);
21 | free(cmd);
22 | }
23 |
24 | void free_env(void *lst)
25 | {
26 | t_env *env;
27 |
28 | env = ((t_list*)lst)->content;
29 | if (env->name)
30 | ft_strdel(&env->name);
31 | if (env->value)
32 | ft_strdel(&env->value);
33 | free(env);
34 | free(lst);
35 | }
36 |
37 | void free_env2(void *lst)
38 | {
39 | t_env *env;
40 |
41 | env = lst;
42 | ft_strdel(&env->name);
43 | ft_strdel(&env->value);
44 | free(env);
45 | }
46 |
47 | void free_redirect(t_token *tmp)
48 | {
49 | ft_strdel(&tmp->word);
50 | free(tmp);
51 | }
52 |
53 | void nothing(void *cmd)
54 | {
55 | (void)cmd;
56 | }
57 |
--------------------------------------------------------------------------------
/srcs/utils/gnl_no_eof.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* gnl_no_eof.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: haguerni +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/15 19:38:21 by haguerni #+# #+# */
9 | /* Updated: 2020/03/10 18:06:05 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | static int handle_line(char *s[], int fd)
16 | {
17 | char *tmp;
18 |
19 | if (ft_is_in_stri('\n', s[fd]) >= 0)
20 | {
21 | tmp = s[fd];
22 | s[fd] = ft_substr(s[fd], ft_is_in_stri('\n', s[fd]) + 1,
23 | ft_strlen(s[fd]));
24 | free(tmp);
25 | }
26 | else if (ft_strlen(s[fd]) > 0)
27 | ft_strdel(&s[fd]);
28 | else
29 | {
30 | ft_strdel(&s[fd]);
31 | return (FINISH);
32 | }
33 | ft_strdel(&s[fd]);
34 | return (SUCCESS);
35 | }
36 |
37 | int ctrl_d_exit(int b, char *s)
38 | {
39 | if (b == 1)
40 | {
41 | ft_dprintf(2, " %s: unexpected EOF while looking for matching"
42 | " `\"\'\n%s: syntax error: unexpected end of file\n",
43 | g_minishell->name, g_minishell->name);
44 | g_minishell->quit = 1;
45 | g_minishell->quit2 = 1;
46 | }
47 | if (b == 2)
48 | {
49 | g_minishell->quit = 2;
50 | g_minishell->quit2 = 1;
51 | }
52 | if (!b)
53 | {
54 | ft_strdel(&s);
55 | exit_cmd4(g_minishell);
56 | }
57 | return (0);
58 | }
59 |
60 | void set_quit(char **str, int *b)
61 | {
62 | if (g_minishell->quit == 1)
63 | {
64 | ft_strdel(str);
65 | if (!(*str = ft_calloc(1, sizeof(char *))))
66 | exit_cmd(g_minishell);
67 | if (*b == 1)
68 | *b = 0;
69 | }
70 | }
71 |
72 | int get_next_line_no_eof(int fd, char **line, int b)
73 | {
74 | static char *s[2];
75 | char buf[2];
76 | int ret;
77 | char *tmp;
78 |
79 | if (fd < 0 || !line || BUFFER_SIZE < 1 || read(fd, buf, 0) < 0
80 | || (!s[fd] && !(s[fd] = ft_calloc(1, sizeof(char *)))))
81 | return (ERROR);
82 | while ((ft_is_in_stri('\n', s[fd]) < 0 && (ret = read(fd, buf, 1)) >= 0))
83 | {
84 | set_quit(&s[fd], &b);
85 | if ((buf[ret] = '\0') == 0 && ret == 0 && ft_strlen(s[fd]) == 0
86 | && g_minishell->quit != 4)
87 | ctrl_d_exit(b, s[fd]);
88 | ft_dprintf(2, " \b\b");
89 | tmp = s[fd];
90 | s[fd] = ft_strjoin(s[fd], buf);
91 | free(tmp);
92 | if (g_minishell->quit != 0 && (g_minishell->quit = 2) == 2)
93 | return (0);
94 | }
95 | s[fd] ? *line = ft_substr(s[fd], 0, ft_strlen_c(s[fd], '\n')) : 0;
96 | if (!handle_line(s, fd))
97 | return (FINISH);
98 | return (SUCCESS);
99 | }
100 |
--------------------------------------------------------------------------------
/srcs/utils/quotes.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* quotes.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/31 17:07:06 by sdunckel #+# #+# */
9 | /* Updated: 2020/02/24 15:55:21 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | int is_escaped(char *s, int pos)
16 | {
17 | int n;
18 |
19 | n = 0;
20 | while (pos >= 0 && s[pos] == '\\')
21 | {
22 | n++;
23 | pos--;
24 | }
25 | return (n % 2);
26 | }
27 |
28 | int last_pipe(char *s, int pos)
29 | {
30 | while (pos > 0 && (s[pos] == ' ' || s[pos] == '\n'))
31 | pos--;
32 | if (pos > 0 && s[pos] == '|' && !is_escaped(s, pos - 1))
33 | {
34 | pos = 0;
35 | while (s[pos] && (s[pos] == ' ' || s[pos] == '\n'))
36 | pos++;
37 | if (s[pos] != '|')
38 | {
39 | while (s[pos] && (s[pos] != '|' || is_escaped(s, pos) ||
40 | in_bracket(s, pos)))
41 | pos++;
42 | if (!s[pos] || !s[pos + 1])
43 | return (1);
44 | else
45 | pos++;
46 | while (s[pos] && (s[pos] == ' ' || s[pos] == '\n'))
47 | pos++;
48 | if (s[pos] != '|')
49 | return (1);
50 | }
51 | }
52 | return (0);
53 | }
54 |
55 | int bracket_odd(char *s, int ret)
56 | {
57 | int bracket1;
58 | int bracket2;
59 | int i;
60 |
61 | bracket1 = 0;
62 | bracket2 = 0;
63 | i = 0;
64 | while (s[i])
65 | {
66 | if (s[i] == 34 && (i == 0 || !is_escaped(s, i - 1))
67 | && bracket2 % 2 == 0)
68 | bracket1++;
69 | if (s[i] == 39 && (i == 0 || bracket2 % 2 != 0 || !is_escaped(s, i - 1))
70 | && bracket1 % 2 == 0)
71 | bracket2++;
72 | i++;
73 | }
74 | if (bracket1 % 2 != 0 || bracket2 % 2 != 0)
75 | return (1);
76 | return (ret ? last_pipe(s, i - 1) : 0);
77 | }
78 |
79 | void next_bracket(t_minishell *minishell)
80 | {
81 | char *tmp;
82 |
83 | g_minishell->quit2 = 0;
84 | write(2, "> ", 2);
85 | if (get_next_line_no_eof(0, &tmp, 1))
86 | {
87 | minishell->line = ft_strjoin_free(minishell->line, "\n");
88 | minishell->line = ft_strjoin_free(minishell->line, tmp);
89 | ft_strdel(&tmp);
90 | }
91 | if (g_minishell->quit == 2)
92 | {
93 | ft_strdel(&minishell->line);
94 | minishell->line = ft_strjoin("", "");
95 | g_minishell->quit = 1;
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/srcs/utils/redirect.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* redirect.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/19 19:47:25 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/06 15:35:07 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | t_token *remove_redirect(t_token *args, t_token **begin)
16 | {
17 | t_token *tmp;
18 | t_token *next;
19 |
20 | tmp = *begin;
21 | if (*begin == args)
22 | {
23 | next = (*begin)->next;
24 | free_redirect(*begin);
25 | *begin = next;
26 | next ? next->prev = NULL : 0;
27 | return (next);
28 | }
29 | while (tmp)
30 | {
31 | if (tmp == args && tmp->prev)
32 | {
33 | next = tmp->next;
34 | tmp->prev->next = next;
35 | tmp->next ? tmp->next->prev = tmp->prev : 0;
36 | free_redirect(args);
37 | return (next);
38 | }
39 | tmp = tmp->next;
40 | }
41 | return (NULL);
42 | }
43 |
44 | int create_redirect2(t_minishell *minishell, t_cmd *cmd, t_token **args,
45 | int flags)
46 | {
47 | int fd;
48 |
49 | if ((fd = open((*args)->next->word, flags, 0644)) < 0)
50 | {
51 | ft_dprintf(2, "%s: %s: %s\n", g_minishell->name,
52 | (*args)->next->word, strerror(errno));
53 | minishell->exit = 1;
54 | }
55 | if ((ft_strequ((*args)->word, ">") || ft_strequ((*args)->word, ">>"))
56 | && cmd->out)
57 | close(cmd->out);
58 | if (ft_strequ((*args)->word, "<") && cmd->in)
59 | close(cmd->in);
60 | *args = remove_redirect(*args, &cmd->args);
61 | *args = remove_redirect(*args, &cmd->args);
62 | return (fd);
63 | }
64 |
65 | void create_redirect(t_minishell *minishell, t_cmd *cmd)
66 | {
67 | t_token *args;
68 |
69 | args = cmd->args;
70 | while (args && args->next)
71 | {
72 | if (ft_strequ(args->word, ">") && args->type == T_REDIRECT
73 | && cmd->out != -1)
74 | cmd->out = create_redirect2(minishell, cmd, &args,
75 | O_TRUNC | O_RDWR | O_CREAT);
76 | else if (ft_strequ(args->word, ">>") && args->type == T_REDIRECT
77 | && cmd->out != -1)
78 | cmd->out = create_redirect2(minishell, cmd, &args,
79 | O_RDWR | O_CREAT | O_APPEND);
80 | else if (ft_strequ(args->word, "<") && args->type == T_REDIRECT
81 | && cmd->in != -1)
82 | cmd->in = create_redirect2(minishell, cmd, &args, O_RDONLY);
83 | else
84 | args = args->next;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/srcs/utils/signals.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* signals.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 22:10:43 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/13 06:24:58 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void sighandler(int sig_num)
16 | {
17 | ft_printf("\b\b \b\b");
18 | if (sig_num == SIGINT)
19 | {
20 | ft_printf("\n");
21 | if (!g_minishell->forked)
22 | g_minishell->exit = 1;
23 | else
24 | g_minishell->exit = 130;
25 | print_prompt(g_minishell);
26 | g_minishell->quit = 1;
27 | }
28 | if (sig_num == SIGQUIT)
29 | g_minishell->quit = 4;
30 | }
31 |
32 | void degage_frr(int sig_num)
33 | {
34 | if (sig_num == 3)
35 | ft_dprintf(2, "Quit: %d\n", sig_num);
36 | g_minishell->quit = 4;
37 | g_minishell->quit2 = 1;
38 | if (sig_num == SIGQUIT)
39 | g_minishell->exit = 131;
40 | if (sig_num == SIGINT)
41 | {
42 | g_minishell->exit = 130;
43 | ft_dprintf(2, "\n");
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/srcs/utils/signals_bonus.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* signals_bonus.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/22 22:10:43 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/13 06:27:17 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void sighandler(int sig_num)
16 | {
17 | if (sig_num == SIGINT)
18 | {
19 | ft_printf("\n");
20 | if (!g_minishell->forked)
21 | g_minishell->exit = 1;
22 | else
23 | g_minishell->exit = 130;
24 | g_tc->cur_pos = 0;
25 | g_tc->mod_offset = 0;
26 | g_tc->rowoffset = 0;
27 | print_prompt(g_minishell);
28 | ft_strdel(&g_minishell->line);
29 | g_minishell->quit = 1;
30 | }
31 | if (sig_num == SIGQUIT)
32 | g_minishell->quit = 4;
33 | }
34 |
35 | void degage_frr(int sig_num)
36 | {
37 | if (sig_num == 3)
38 | ft_dprintf(2, "Quit: %d\n", sig_num);
39 | g_minishell->quit = 4;
40 | g_minishell->quit2 = 1;
41 | if (sig_num == SIGQUIT)
42 | g_minishell->exit = 131;
43 | if (sig_num == SIGINT)
44 | {
45 | g_minishell->exit = 130;
46 | ft_dprintf(2, "\n");
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/srcs/utils/subshell_bonus.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* subshell_bonus.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 20:23:01 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/09 18:26:24 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void and_or_subshell(t_minishell *minishell, t_cmd **tmp)
16 | {
17 | int prev_scope;
18 |
19 | prev_scope = 0;
20 | if (!(*tmp)->prev)
21 | return ;
22 | if ((*tmp)->prev->scope != (*tmp)->scope)
23 | {
24 | prev_scope = (*tmp)->prev->scope;
25 | if ((*tmp)->prev->type == T_OR && !minishell->exit)
26 | {
27 | while ((*tmp) && (*tmp)->scope > prev_scope)
28 | *tmp = (*tmp)->next;
29 | }
30 | else if ((*tmp)->prev->type == T_AND && minishell->exit)
31 | {
32 | while ((*tmp) && (*tmp)->scope > prev_scope)
33 | *tmp = (*tmp)->next;
34 | }
35 | if (!*tmp)
36 | return ;
37 | }
38 | while (*tmp && (*tmp)->prev->type == T_AND && minishell->exit)
39 | *tmp = (*tmp)->next;
40 | while (*tmp && (*tmp)->prev->type == T_OR && !minishell->exit)
41 | *tmp = (*tmp)->next;
42 | }
43 |
44 | void handle_wild(t_token *tmp)
45 | {
46 | char **split;
47 | char *tmp2;
48 |
49 | split = ft_split(tmp->word, '/');
50 | tmp2 = create_wildpath(tmp->word);
51 | process_wildcard(tmp, tmp2, 0, split);
52 | free(tmp2);
53 | ft_free_split(&split);
54 | }
55 |
56 | void lol_mdr(char *tmp, char *line, int notenv, int pip[2])
57 | {
58 | ft_strdel(&tmp);
59 | if (ft_is_in_stri('$', line) > -1 && !notenv)
60 | line = replace_env(line, 0);
61 | g_minishell->quit != 2 ? ft_putstr_fd(line, pip[1]) : 0;
62 | ft_strdel(&line);
63 | close(pip[1]);
64 | }
65 |
--------------------------------------------------------------------------------
/srcs/utils/utils.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* utils.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/01/14 13:25:43 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/09 18:16:13 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | char **join_args(t_cmd *cmd)
16 | {
17 | char **args;
18 | t_token *tmp;
19 | int i;
20 |
21 | i = 1;
22 | if (!cmd->cmd)
23 | return (NULL);
24 | if (!(args = (char **)ft_calloc(1, sizeof(char *) *
25 | (token_list_size(&cmd->args) + 2))))
26 | exit_cmd(g_minishell);
27 | args[0] = ft_strdup(cmd->cmd);
28 | tmp = cmd->args;
29 | while (tmp)
30 | {
31 | args[i] = ft_strdup(tmp->word);
32 | tmp = tmp->next;
33 | i++;
34 | }
35 | args[i] = NULL;
36 | return (args);
37 | }
38 |
39 | char *handle_quotes(char *src, int env)
40 | {
41 | int i;
42 | char *dest;
43 |
44 | if (src == NULL)
45 | return (src);
46 | i = 0;
47 | dest = ft_strdup("");
48 | while (src[i])
49 | {
50 | if (src[i] == '\'' && !is_escaped(src, i - 1))
51 | dest = ft_strjoin_double_free(dest, simple_quotes(src, &i));
52 | else if (src[i] == '\"' && !is_escaped(src, i - 1))
53 | {
54 | i++;
55 | dest = ft_strjoin_double_free(dest, double_quotes(src, &i, 0, env));
56 | }
57 | else
58 | {
59 | dest = ft_strjoin_double_free(dest, no_quotes(src, &i, 0, env));
60 | continue ;
61 | }
62 | src[i] ? i++ : 0;
63 | }
64 | ft_strdel(&src);
65 | return (dest);
66 | }
67 |
68 | char *supp_newline(char *src)
69 | {
70 | int i;
71 | int j;
72 | char *dest;
73 |
74 | if (!src)
75 | return (NULL);
76 | if (ft_is_in_stri('\n', src) == -1)
77 | return (src);
78 | i = 0;
79 | j = 0;
80 | if (!(dest = (char *)ft_calloc(1, ft_strlen(src))))
81 | exit_cmd(g_minishell);
82 | while (src[i])
83 | {
84 | if (src[i] != '\n')
85 | {
86 | dest[j] = src[i];
87 | j++;
88 | }
89 | i++;
90 | }
91 | dest[j] = '\0';
92 | return (dest);
93 | }
94 |
95 | void ft_sort_list(t_list **begin_list, int (*cmp)())
96 | {
97 | t_list *ptr;
98 | t_list *ptr2;
99 | t_list *next;
100 |
101 | ptr = *begin_list;
102 | while (ptr)
103 | {
104 | ptr2 = *begin_list;
105 | while (ptr2->next)
106 | {
107 | if ((*cmp)(((t_env*)(ptr2->content))->name,
108 | ((t_env*)(ptr2->next->content))->name) > 0)
109 | {
110 | next = ptr2->content;
111 | ptr2->content = ptr2->next->content;
112 | ptr2->next->content = next;
113 | }
114 | ptr2 = ptr2->next;
115 | }
116 | ptr = ptr->next;
117 | }
118 | }
119 |
120 | int is_only_ascii(char *str)
121 | {
122 | int i;
123 |
124 | i = 0;
125 | while (str[i])
126 | {
127 | if (!ft_isascii(str[i]))
128 | return (0);
129 | i++;
130 | }
131 | return (1);
132 | }
133 |
--------------------------------------------------------------------------------
/srcs/utils/utils2.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* utils2.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: haguerni +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/14 17:30:17 by haguerni #+# #+# */
9 | /* Updated: 2020/03/10 15:35:11 by haguerni ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | char *replace_env(char *str, int i)
16 | {
17 | int k;
18 | int j;
19 | char *tmp;
20 | char *new;
21 |
22 | k = ft_quotelen(str, 4, 1);
23 | if (!(new = (char *)ft_calloc(1, k + 1)))
24 | exit(1);
25 | j = 0;
26 | while (str[i] && j < k)
27 | {
28 | if (str[i] == '$' && !is_escaped(str, i - 1))
29 | {
30 | tmp = replace_env2(str, &i);
31 | j = ft_strlcat(new, tmp, k);
32 | free(tmp);
33 | continue ;
34 | }
35 | if ((str[i] != '\\' || is_escaped(str, i - 1)) && j < k)
36 | new[j++] = str[i];
37 | i++;
38 | }
39 | free(str);
40 | return (new);
41 | }
42 |
43 | char *replace_env2(char *str, int *i)
44 | {
45 | int count;
46 | char buf[4096];
47 | char *new;
48 |
49 | new = ft_strdup("");
50 | count = 0;
51 | (*i)++;
52 | ft_bzero(buf, sizeof(buf));
53 | while (str[*i] && (ft_isalnum(str[*i]) || is_char_str(str[*i], "?_")))
54 | {
55 | buf[count] = str[*i];
56 | *i = *i + 1;
57 | if (str[*i - 1] == '?' && count == 0)
58 | break ;
59 | if (ft_isdigit(str[*i - 1]) && count == 0)
60 | return (new);
61 | count++;
62 | }
63 | new = ft_strjoin_free(new, get_env(g_minishell, buf));
64 | return (new);
65 | }
66 |
67 | int in_bracket(char *s, int pos)
68 | {
69 | int bracket1;
70 | int bracket2;
71 | int i;
72 |
73 | bracket1 = 0;
74 | bracket2 = 0;
75 | i = 0;
76 | while (i <= pos)
77 | {
78 | if (s[i] == 34 && (i == 0 || !is_escaped(s, i - 1))
79 | && bracket2 % 2 == 0)
80 | bracket1++;
81 | if (s[i] == 39 && (i == 0 || bracket2 % 2 != 0 || !is_escaped(s, i - 1))
82 | && bracket1 % 2 == 0)
83 | bracket2++;
84 | i++;
85 | }
86 | if (bracket1 % 2 != 0 || bracket2 % 2 != 0)
87 | return (1);
88 | return (0);
89 | }
90 |
91 | int is_only_digit(char *str)
92 | {
93 | int i;
94 |
95 | i = 0;
96 | while (str[i])
97 | {
98 | if (!ft_isdigit(str[i]))
99 | return (0);
100 | i++;
101 | }
102 | return (1);
103 | }
104 |
105 | int is_char_str(char c, char *str)
106 | {
107 | if (ft_is_in_stri(c, str) >= 0)
108 | return (1);
109 | return (0);
110 | }
111 |
--------------------------------------------------------------------------------
/srcs/utils/wait_commmand_bonus.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* wait_commmand_bonus.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: sdunckel +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2020/02/28 20:24:06 by sdunckel #+# #+# */
9 | /* Updated: 2020/03/06 15:45:41 by sdunckel ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "minishell.h"
14 |
15 | void wait_for_command_tty(t_minishell *minishell)
16 | {
17 | while (1)
18 | {
19 | signal(SIGQUIT, sighandler);
20 | signal(SIGINT, sighandler);
21 | if (g_minishell->quit == 0 || g_minishell->quit2)
22 | print_prompt(minishell);
23 | g_minishell->quit = 0;
24 | g_minishell->quit2 = 0;
25 | minishell->forked = 0;
26 | if (get_next_line_no_eof(0, &minishell->line, 0))
27 | {
28 | while (g_minishell->quit == 0 && bracket_odd(minishell->line, 1))
29 | next_bracket(minishell);
30 | start_parse(minishell, minishell->line);
31 | if (minishell->cmd_list && (g_minishell->quit == 0
32 | || g_minishell->quit == 4))
33 | exec_commands(minishell);
34 | if (ft_strlen(minishell->line) && ft_strlen_s(minishell->line) == 0)
35 | minishell->exit = 0;
36 | clear_token_list(&minishell->token_list, free);
37 | clear_cmd_list(&minishell->cmd_list, free);
38 | }
39 | ft_strdel(&minishell->line);
40 | }
41 | }
42 |
43 | void reset_tc(void)
44 | {
45 | g_tc->rowoffset = 0;
46 | g_tc->cur_history = NULL;
47 | g_tc->cur_pos = 0;
48 | }
49 |
50 | void wait_for_command(t_minishell *minishell)
51 | {
52 | while (1)
53 | {
54 | signal(SIGQUIT, sighandler);
55 | signal(SIGINT, sighandler);
56 | get_cursor_position(&g_tc->start_col, &g_tc->start_row);
57 | print_prompt(minishell);
58 | get_cursor_position(&g_tc->col, &g_tc->start_row);
59 | g_tc->mod_offset = 0;
60 | if (termcaps_loop())
61 | {
62 | tcsetattr(0, TCSANOW, &g_tc->term_backup);
63 | add_cmd_to_history(minishell->line);
64 | if (minishell->line)
65 | start_parse(minishell, minishell->line);
66 | minishell->cmd_list ? exec_commands(minishell) : 0;
67 | if (!minishell->cmd_list)
68 | minishell->exit = 0;
69 | clear_token_list(&minishell->token_list, free);
70 | clear_cmd_list(&minishell->cmd_list, free);
71 | tcsetattr(0, TCSANOW, &g_tc->term);
72 | }
73 | ft_strdel(&g_tc->backup_cmd);
74 | reset_tc();
75 | ft_strdel(&minishell->line);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------