Skip to content

Commit

Permalink
improved Makefile, added functions from minishell, added obj folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphacharge committed Feb 22, 2023
1 parent 44177f5 commit 86d71c0
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 41 deletions.
73 changes: 47 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,74 @@
# By: rbetz <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/03/25 14:21:20 by rbetz #+# #+# #
# Updated: 2022/09/19 11:33:55 by rbetz ### ########.fr #
# Updated: 2023/02/22 13:31:09 by rbetz ### ########.fr #
# #
# **************************************************************************** #

NAME = libft.a
SRC = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \
ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c ft_memchr.c \
ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_putchar_fd.c \
ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c ft_split.c \
ft_strchr.c ft_strdup.c ft_striteri.c ft_strjoin.c ft_strlcat.c \
ft_strlcpy.c ft_strlen.c ft_strmapi.c ft_strncmp.c ft_strnstr.c \
ft_strrchr.c ft_strtrim.c ft_substr.c ft_tolower.c ft_toupper.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_atol.c ft_ltoa.c ft_strstr.c ft_strreplace.c ft_strisdigit.c \
ft_printf.c ft_printflibft.c \
get_next_line_bonus.c get_next_line_utils_bonus.c \
ft_first_word.c

SRCOBJ=$(SRC:.c=.o)

CC = "cc"
CFLAGS = -Wall -Wextra -Werror
NAME := libft.a

### ### COMPILER ### ###
CC := "cc"
CFLAGS := -Wall -Wextra -Werror

### ### SOURCES ### ###
SRC := ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c
SRC += ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c ft_memchr.c
SRC += ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_putchar_fd.c
SRC += ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c ft_split.c
SRC += ft_strchr.c ft_strdup.c ft_striteri.c ft_strjoin.c ft_strlcat.c
SRC += ft_strlcpy.c ft_strlen.c ft_strmapi.c ft_strncmp.c ft_strnstr.c
SRC += ft_strrchr.c ft_strtrim.c ft_substr.c ft_tolower.c ft_toupper.c
SRC += ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c
SRC += ft_lstiter.c ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c
SRC += ft_atol.c ft_ltoa.c ft_strstr.c ft_strreplace.c ft_strisdigit.c
SRC += ft_printf.c ft_printflibft.c
SRC += get_next_line_bonus.c get_next_line_utils_bonus.c
SRC += ft_first_word.c ft_last_word.c
SRC += ft_arraycount.c ft_free_array.c ft_multijoin_array.c
SRC += ft_close.c ft_strcmp.c
SRC += ft_free_multiple.c ft_multijoin.c

### ### OBJECTS ### ###
OBJ_D := ./obj
SRCOBJ := $(patsubst %.c,$(OBJ_D)/%.o,$(SRC))

### ### COLORS ### ###
RED = \033[1;31m
GREEN = \033[1;32m
YELL = \033[1;33m
BLUE = \033[1;34m
WHITE = \033[0m

### ### RULES ### ###
all: $(NAME)

$(NAME): $(SRCOBJ)
@ar -rucs $(NAME) $^
$(NAME): $(OBJ_D) $(SRCOBJ)
@ar -rcs $(NAME) $@
@echo "$(RED)--->$(BLUE)Libft is compiled.$(WHITE)"

$(OBJ_D)/%.o: %.c message
@$(CC) $(CFLAGS) -c $< -o $@

message:
@echo "$(BLUE)--->$(GREEN)Compiling C Files .....$(WHITE)"

$(OBJ_D):
@mkdir $@

clean:
@echo "$(RED)Cleaning Objects:\n$(WHITE)$(SRCOBJ)"
@/bin/rm -f $(SRCOBJ) libft.h.gch
@echo "$(YELL)--->$(RED)Cleaning Objects .....$(WHITE)"
@/bin/rm -rf $(OBJ_D) libft.h.gch

fclean: clean
@if [ -f "$(NAME)" ]; then \
/bin/rm -f $(NAME); \
echo "$(YELL)Cleaning Lib:\n$(WHITE)$(NAME)";else \
echo "$(GREEN)--->$(YELL)Cleaning Lib .....$(WHITE)";else \
echo "No $(NAME) to remove."; \
fi;

re: fclean all

.PHONY: all clean fclean re
.PHONY: all clean fclean re

.INTERMEDIATE: message
26 changes: 26 additions & 0 deletions ft_arraycount.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arraycount.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/22 14:53:27 by fkernbac #+# #+# */
/* Updated: 2023/02/22 11:06:36 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*Returns argc value for argv.*/
int ft_arraycount(char **array)
{
int i;

i = 0;
if (array == NULL)
return (0);
while (array[i] != NULL)
i++;
return (i);
}
27 changes: 27 additions & 0 deletions ft_close.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_close.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 11:11:55 by rbetz #+# #+# */
/* Updated: 2023/02/22 11:30:36 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

void ft_close_and_neg(int *fd)
{
close(*fd);
*fd = FD_UNUSED;
}

void ft_close_pipe_fds(int pipe[2])
{
if (pipe[READ] != FD_UNUSED)
ft_close_and_neg(&pipe[READ]);
if (pipe[WRITE] != FD_UNUSED)
ft_close_and_neg(&pipe[WRITE]);
}
31 changes: 31 additions & 0 deletions ft_free_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 11:05:26 by rbetz #+# #+# */
/* Updated: 2023/02/22 11:05:55 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*Frees an array and every pointer inside.*/
void *ft_free_array(void **array)
{
int i;

i = 0;
if (array == NULL)
return (NULL);
while (array[i] != NULL)
{
free(array[i]);
array[i++] = NULL;
}
free(array);
array = NULL;
return (NULL);
}
36 changes: 36 additions & 0 deletions ft_free_multiple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_multiple.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 11:04:24 by rbetz #+# #+# */
/* Updated: 2023/02/22 11:18:24 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*Frees memory of n arguments if they are not pointing to NULL.*/
void ft_free_multiple(int n, ...)
{
va_list args;
void **pointer;

pointer = NULL;
if (n <= 0)
return ;
va_start(args, n);
while (n > 0)
{
pointer = va_arg(args, void **);
if (*pointer != NULL)
{
free(*pointer);
*pointer = NULL;
}
n--;
}
va_end(args);
}
42 changes: 42 additions & 0 deletions ft_last_word.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_last_word.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 11:28:23 by rbetz #+# #+# */
/* Updated: 2023/02/22 11:06:50 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/*Returns a malloced string to last occurens of c in str from end*/
/*or if rev = 1 from start to the lim*/
char *ft_last_word(const char *str, char c, int rev)
{
int i;
int len;
char *ret;

len = ft_strlen(str);
i = len;
if (str == NULL)
return (NULL);
if (c == '\0')
c = ' ';
if (rev != 1)
rev = 0;
while (i >= 0 && str[i] != c)
i--;
if (rev == 0)
ret = ft_calloc(len - i + 1, sizeof(char));
else
ret = ft_calloc(i, sizeof(char));
if (rev == 0)
ft_memcpy(ret, &str[i + 1], len - i);
else
ft_memcpy(ret, &str[0], i);
return (ret);
}
75 changes: 75 additions & 0 deletions ft_multijoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_multijoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbetz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/02 16:08:04 by rbetz #+# #+# */
/* Updated: 2023/02/22 10:57:21 by rbetz ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

static char *copy_input(bool tofr, int n, char **input)
{
int i;
int len;
char *str;

i = 0;
len = 0;
while (i < n)
len += ft_strlen(input[i++]);
str = ft_calloc(len + 1, sizeof(char));
i = 0;
len = 0;
while (str != NULL && i < n)
{
ft_memcpy(&str[len], input[i], ft_strlen(input[i]));
len += ft_strlen(input[i]);
if (tofr == true)
{
free(input[i]);
input[i] = NULL;
}
i++;
}
return (str);
}

static char **read_input(int n, va_list args)
{
int i;
char **input;

i = 0;
input = ft_calloc(n + 1, sizeof(char *));
input[n] = NULL;
while (i < n)
{
input[i] = va_arg(args, char *);
i++;
}
return (input);
}

/*tofr, frees the input if true, n is the amount of strings,*/
/*accepts endless strings to join together*/
char *ft_multijoin(bool tofr, int n, ...)
{
va_list args;
char **input;
char *str;

if (n < 2)
return (NULL);
va_start(args, n);
input = read_input(n, args);
str = copy_input(tofr, n, input);
va_end(args);
free(input);
input = NULL;
return (str);
}
Loading

0 comments on commit 86d71c0

Please sign in to comment.