feat(ft_strdup|ft_strlen)

This commit is contained in:
gbrochar 2024-03-13 15:25:43 +01:00
commit e6177d9429
5 changed files with 142 additions and 0 deletions

57
.gitignore vendored Normal file
View File

@ -0,0 +1,57 @@
# vim
*.swp
# ---> C
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

52
Makefile Normal file
View File

@ -0,0 +1,52 @@
NAME = libft.a
SRC_FILES = ft_strdup.c \
ft_strlen.c \
INC_FILES = libft.h \
OBJ_FILES = $(SRC_FILES:.c=.o)
SRC_DIR = src/
INC_DIR = inc/
OBJ_DIR = obj/
SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
INC = $(addprefix $(INC_DIR), $(INC_FILES))
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
CC = gcc
CFLAGS = -Wall -Werror -Wextra
RED = \033[31m
GREEN = \033[32m
YELLOW = \033[33m
BLUE = \033[34m
CYAN = \033[36m
WHITE = \033[0m
all: $(NAME)
$(NAME): $(OBJ) $(INC)
@$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC)
@mv $(OBJ_FILES) $(OBJ_DIR)
@ar rc $(NAME) $(OBJ)
@echo "$(GREEN)[OK]$(WHITE) $(NAME)"
$(OBJ_DIR)%.o: $(SRC_DIR)%.c
@if [ ! -d ./obj ]; then \
mkdir -p ./obj; \
fi;
@$(CC) $(CFLAGS) -I $(INC_DIR) -o $@ -c $<
@echo "$(CYAN)[CC]$(WHITE) $<"
clean:
@rm -rf $(OBJ)
fclean: clean
@rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

10
inc/libft.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _LIBFT_H
# define _LIBFT_H
# include <stddef.h>
# include <stdlib.h>
char *ft_strdup(const char *s);
size_t ft_strlen(const char *s);
#endif

14
src/ft_strdup.c Normal file
View File

@ -0,0 +1,14 @@
#include "libft.h"
char *ft_strdup(const char *s) {
size_t len = ft_strlen(s);
char *dest = malloc(len + 1);
if (!dest) {
return NULL;
}
dest[len] = '\0';
for (size_t i = 0; i < len; i++) {
dest[i] = s[i];
}
return dest;
}

9
src/ft_strlen.c Normal file
View File

@ -0,0 +1,9 @@
#include "libft.h"
size_t ft_strlen(const char *s) {
size_t len = 0;
while (s[len]) {
len = len + 1;
}
return len;
}