diff --git a/Makefile b/Makefile index cce1e77..07a8614 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC := gcc LD := $(CC) LDFLAGS := CFLAGS := $(shell cat compile_flags.txt | sed -z "s/\n/ /g") -SRC := $(wildcard src/*.c lib/**/*.c) +SRC := $(wildcard lib/**/*.c src/*.c) OBJ_DIR := build OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRC))) DIR := . diff --git a/compile_flags.txt b/compile_flags.txt index 8b67f31..ce6412a 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,6 +1,6 @@ --includeinc/main.h -includelib/libft/libft.h -includelib/ft_printf/ft_printf.h +-includeinc/main.h -Wall -Wextra -std=c23 diff --git a/inc/main.h b/inc/main.h index a65b80d..eddb189 100644 --- a/inc/main.h +++ b/inc/main.h @@ -3,13 +3,13 @@ #include typedef struct { - i64 l; - i64 a; - i64 R; - i64 r; - i64 t; + bool l; + bool a; + bool R; + bool r; + bool t; int error[2]; } flags_t; -flags_t *get_flags(char **argv); +flags_t get_flags(char **argv); void display_flags(flags_t flags); diff --git a/lib/ft_printf/ft_printf.c b/lib/ft_printf/ft_printf.c index 41eeecb..c8730eb 100644 --- a/lib/ft_printf/ft_printf.c +++ b/lib/ft_printf/ft_printf.c @@ -18,50 +18,59 @@ static u32 ft_is_format(const char c) || c == 'u' || c == 'x' || c == 'X' || c == '%' || c == 'b'); } -static u32 ft_post_character(u32 fd, char format, va_list *args) +static u32 ft_post_character(u32 fd, char format, va_list args) { if (format == 'c') - return ft_putchar_fd(va_arg(*args, int), fd); + return ft_putchar_fd(va_arg(args, int), fd); else if (format == 's') - return ft_putstr_fd(va_arg(*args, char *), fd); + return ft_putstr_fd(va_arg(args, char *), fd); else if (format == 'd' || format == 'i' || format == 'u') - return ft_putnbr_fd(va_arg(*args, i64), fd); + return ft_putnbr_fd(va_arg(args, i64), fd); else if (format == 'x') - return ft_putnbrhex_fd(va_arg(*args, u32), fd); + return ft_putnbrhex_fd(va_arg(args, u32), fd); else if (format == 'X') - return ft_putnbrhex_upper_fd(va_arg(*args, u32), fd); + return ft_putnbrhex_upper_fd(va_arg(args, u32), fd); else if (format == '%') return ft_putchar_fd('%', fd); else if (format == 'p') - return ft_putvd_fd(va_arg(*args, void *), fd); + return ft_putvd_fd(va_arg(args, void *), fd); else if (format == 'b') - return ft_putstr_fd(va_arg(*args, i32) ? "true" : "false", fd); + return ft_putstr_fd(va_arg(args, i32) ? "true" : "false", fd); return 0; } -u32 ft_printf_fd(u32 fd, const char *str, ...) -{ - va_list args; +u32 ft_vdprintf(u32 fd, const char *str, va_list args) { + u32 count = 0; if (str == NULL) return (-1); - va_start(args, str); for (u32 i = 0; str[i]; i++) { if (str[i] == '%' && ft_is_format(str[i + 1])) - count += ft_post_character(fd, str[(i++) + 1], &args); + count += ft_post_character(fd, str[(i++) + 1], args); else { ft_putchar_fd(str[i], fd); count++; } } - va_end(args); return (count); } +u32 ft_printf_fd(u32 fd, const char *str, ...) +{ + va_list args; + va_start(args, str); + u32 count = ft_vdprintf(fd, str, args); + va_end(args); + return count; +} + u32 ft_printf(const char *str, ...) { va_list args; - return ft_printf_fd(0, str, args); + va_start(args, str); + u32 count = ft_vdprintf(0, str, args); + va_end(args); + return count; } diff --git a/lib/ft_printf/ft_printf.h b/lib/ft_printf/ft_printf.h index 7be8ea8..8f01f02 100644 --- a/lib/ft_printf/ft_printf.h +++ b/lib/ft_printf/ft_printf.h @@ -16,8 +16,9 @@ # include # include -uint32_t ft_printf(const char *str, ...); -uint32_t ft_printf_fd(uint32_t fd, const char *str, ...); -uint32_t ft_putnbrhex_fd(uint32_t v, uint32_t fd); -uint32_t ft_putnbrhex_upper_fd(uint32_t v, uint32_t fd); -uint32_t ft_putvd_fd(void *v, uint32_t fd); +u32 ft_printf(const char *str, ...); +u32 ft_printf_fd(u32 fd, const char *str, ...); +u32 ft_vdprintf(u32 fd, const char *str, va_list args); +u32 ft_putnbrhex_fd(u32 v, u32 fd); +u32 ft_putnbrhex_upper_fd(u32 v, u32 fd); +u32 ft_putvd_fd(void *v, u32 fd); diff --git a/lib/ft_printf/ft_putnbrhex_fd.c b/lib/ft_printf/ft_putnbrhex_fd.c index 61df9d3..3ec0913 100644 --- a/lib/ft_printf/ft_putnbrhex_fd.c +++ b/lib/ft_printf/ft_putnbrhex_fd.c @@ -12,10 +12,10 @@ #include "ft_printf.h" -uint32_t ft_putnbrhex_fd(uint32_t v, uint32_t fd) +u32 ft_putnbrhex_fd(u32 v, u32 fd) { - uint32_t tmp = 0; - uint32_t count = 0; + i32 tmp = 0; + i32 count = 0; char hex[100]; if (v == 0) diff --git a/lib/ft_printf/ft_putnbrhex_upper_fd.c b/lib/ft_printf/ft_putnbrhex_upper_fd.c index 758cd0b..ed43756 100644 --- a/lib/ft_printf/ft_putnbrhex_upper_fd.c +++ b/lib/ft_printf/ft_putnbrhex_upper_fd.c @@ -12,10 +12,10 @@ #include "ft_printf.h" -int ft_putnbrhex_upper_fd(unsigned int v, int fd) +u32 ft_putnbrhex_upper_fd(u32 v, u32 fd) { - int tmp = 0; - int count = 0; + u32 tmp = 0; + u32 count = 0; char hex[100]; if (v == 0) diff --git a/lib/ft_printf/ft_putvd_fd.c b/lib/ft_printf/ft_putvd_fd.c index 2c6a8b1..b969593 100644 --- a/lib/ft_printf/ft_putvd_fd.c +++ b/lib/ft_printf/ft_putvd_fd.c @@ -12,9 +12,9 @@ #include "ft_printf.h" -int ft_putvd_fd(void *v, int fd) +u32 ft_putvd_fd(void *v, u32 fd) { - int count; + u32 count; unsigned long u; if (v == NULL) diff --git a/lib/libft/Makefile b/lib/libft/Makefile deleted file mode 100644 index e507ce0..0000000 --- a/lib/libft/Makefile +++ /dev/null @@ -1,154 +0,0 @@ -# **************************************************************************** # -# # -# ::: :::::::: # -# Makefile :+: :+: :+: # -# +:+ +:+ +:+ # -# By: erey-bet +#+ +:+ +#+ # -# +#+#+#+#+#+ +#+ # -# Created: 2022/09/27 04:19:30 by erey-bet #+# #+# # -# Updated: 2022/10/19 17:45:46 by erey-bet ### ########.fr # -# # -# **************************************************************************** # - -SRCS = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c \ -ft_strlen.c ft_memset.c ft_bzero.c ft_memcpy.c ft_memmove.c ft_strlcpy.c \ -ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c ft_strrchr.c ft_strncmp.c \ -ft_memchr.c ft_memcmp.c ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c \ -ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \ -ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ -ft_get_size.c ft_power.c ft_atoi_check.c -OBJS = ${SRCS:.c=.o} -BONUS_SRCS = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c \ -ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c -BONUS_OBJS = ${BONUS_SRCS:.c=.o} -CC = gcc -CFLAGS = -Wall -Wextra -Werror -NAME = libft.a - -ifdef BONUS - SRCS += ${BONUS_SRCS} -endif - -all: ${NAME} - -${NAME}: ${OBJS} - ar -rc ${NAME} ${OBJS} - -%.o:%.c - ${CC} ${CFLAGS} -c -o $@ $< - -clean: - rm -f ${OBJS} ${BONUS_OBJS} - -fclean: clean - rm -f ${NAME} - -re: fclean all - -bonus: - @make BONUS=1 - -coffee: - @clear - @echo "" - @echo " (" - @echo " ) (◦" - @echo " ___...(-------)-....___◦" - @echo ' .-"" ) ( ""-.' - @echo " .-''''|-._ ) _.-|" - @echo ' / .--.| `""---...........---""` |' - @echo " / / | |" - @echo " | | | |" - @echo " \ \ | |" - @echo " '\ '\ | |" - @echo " '\ '| |" - @echo " _/ /\ /" - @echo " (__/ \ /" - @echo ' _..---""` \ /`""---.._' - @echo " .-' \ / '-." - @echo ": '-.__ __.-' :" - @echo ': ) ""---...---"" ( :' - @echo '\._ "--...___...--" _."' - @echo ' \""--..__ __..--""/' - @echo ' "._ """----.....______.....----""" _."' - @echo ' ""--..,,_____ _____,,..--"""' - @echo ' """------"""' - @sleep 0.2 - @clear - @echo "" - @echo " (◦" - @echo " ◦ ◦ ) ( " - @echo " ___..(.------)--....___" - @echo ' .-"" ) ( ""-.' - @echo " .-''''|-._ ( ) _.-|" - @echo ' / .--.| `""---...........---""` |' - @echo " / / | |" - @echo " | | | |" - @echo " \ \ | |" - @echo " '\ '\ | |" - @echo " '\ '| |" - @echo " _/ /\ /" - @echo " (__/ \ /" - @echo ' _..---""` \ /`""---.._' - @echo " .-' \ / '-." - @echo ": '-.__ __.-' :" - @echo ': ) ""---...---"" ( :' - @echo '\._ "--...___...--" _."' - @echo ' \""--..__ __..--""/' - @echo ' "._ """----.....______.....----""" _."' - @echo ' ""--..,,_____ _____,,..--"""' - @echo ' """------"""' - @sleep 0.2 - @clear - @echo " ◦" - @echo " ( ◦" - @echo " ◦ ) ( " - @echo " ___..(.------)--....___" - @echo ' .-"" ) ( ""-.' - @echo " .-''''|-._ ( ) _.-|" - @echo ' / .--.| `""---...........---""` |' - @echo " / / | |" - @echo " | | | |" - @echo " \ \ | |" - @echo " '\ '\ | |" - @echo " '\ '| |" - @echo " _/ /\ /" - @echo " (__/ \ /" - @echo ' _..---""` \ /`""---.._' - @echo " .-' \ / '-." - @echo ": '-.__ __.-' :" - @echo ': ) ""---...---"" ( :' - @echo '\._ "--...___...--" _."' - @echo ' \""--..__ __..--""/' - @echo ' "._ """----.....______.....----""" _."' - @echo ' ""--..,,_____ _____,,..--"""' - @echo ' """------"""' - @sleep 0.2 - @clear - @echo "" - @echo " ( ) ◦" - @echo " ◦ ) (◦" - @echo " ___)...----)----....___" - @echo ' .-"" ) ( ""-.' - @echo " .-''''|-._ ( ) _.-|" - @echo ' / .--.| `""---...........---""` |' - @echo " / / | |" - @echo " | | | |" - @echo " \ \ | |" - @echo " '\ '\ | |" - @echo " '\ '| |" - @echo " _/ /\ /" - @echo " (__/ \ /" - @echo ' _..---""` \ /`""---.._' - @echo " .-' \ / '-." - @echo ": '-.__ __.-' :" - @echo ': ) ""---...---"" ( :' - @echo '\._ "--...___...--" _."' - @echo ' \""--..__ __..--""/' - @echo ' "._ """----.....______.....----""" _."' - @echo ' ""--..,,_____ _____,,..--"""' - @echo ' """------"""' - @sleep 0. - ${MAKE} coffee - -.PHONY: all clean fclean re bonus coffee diff --git a/lib/libft/ft_strslen.c b/lib/libft/ft_strslen.c new file mode 100644 index 0000000..bfb0025 --- /dev/null +++ b/lib/libft/ft_strslen.c @@ -0,0 +1,5 @@ +u32 ft_strslen(char **strs) { + u32 count = 0; + for(; strs[count] ;count++); + return count; +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h index ee94671..92056fe 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -83,4 +83,5 @@ void ft_lstdelone(t_list *lst, void (*del)(void*)); void ft_lstclear(t_list **lst, void (*del)(void*)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); -int ft_atoi_check(const char *nptr); +int ft_atoi_check(const char *nptr); +u32 ft_strslen(char **strs); diff --git a/src/flags.c b/src/flags.c deleted file mode 100644 index f589ac1..0000000 --- a/src/flags.c +++ /dev/null @@ -1,34 +0,0 @@ -flags_t *get_flags(char **argv) { - flags_t *flags = ft_calloc(1, sizeof(flags_t)); - if (flags == nullptr) - return nullptr; - flags->t = 0; - flags->error[0] = -1; - - for (int i = 1; argv[i] != NULL; i++) - if (argv[i][0] == '-') - for (int j = 1; argv[i][j] != '\0'; j++) { - if (argv[i][j] == 'l') - flags->l = 1; - else if (argv[i][j] == 'a') - flags->a = 1; - else if (argv[i][j] == 'R') - flags->R = 1; - else if (argv[i][j] == 'r') - flags->r = 1; - else if (argv[i][j] == 't') - flags->t = 1; - else { - flags->error[0] = i; - flags->error[1] = j; - return flags; - } - } - - return flags; -} - -void display_flags(flags_t flags) { - ft_printf("l: %d; a: %d; R: %d; r: %d; t: %d\n", - flags.l, flags.a, flags.R, flags.r, flags.t); -} diff --git a/src/main.c b/src/main.c index 49802aa..4e1f7d4 100644 --- a/src/main.c +++ b/src/main.c @@ -3,13 +3,15 @@ int main(int argc, char **argv) { if (argc < 2) return 1; - flags_t *flags = get_flags(argv); - if (flags->error[0] != -1) { + flags_t flags = get_flags(argv); + if (flags.error[0] != -1) { ft_printf_fd(2, "ls: invalid line width: \'%s\'", - argv[flags->error[0]][flags->error[1]]); + &argv[flags.error[0]][flags.error[1]]); return 2; } - display_flags(*flags); + display_flags(flags); + + //ft_ls(flags); return 0; } diff --git a/src/parsing.c b/src/parsing.c new file mode 100644 index 0000000..8cc4466 --- /dev/null +++ b/src/parsing.c @@ -0,0 +1,44 @@ +flags_t get_flags(char **argv) { + flags_t flags = {false, false, false, false, false, + {-1, -1}}; + + for (int i = 1; argv[i] != NULL; i++) { + if (argv[i][0] == '-') { + for (int j = 1; argv[i][j] != '\0'; j++) { + if (argv[i][j] == 'l') + flags.l = true; + else if (argv[i][j] == 'a') + flags.a = true; + else if (argv[i][j] == 'R') + flags.R = true; + else if (argv[i][j] == 'r') + flags.r = true; + else if (argv[i][j] == 't') + flags.t = true; + else { + flags.error[0] = i; + flags.error[1] = j; + return flags; + } + } + } + } + + return flags; +} + +void display_flags(flags_t flags) { + ft_printf("l: %b; a: %b; R: %b; r: %b; t: %b\n", + flags.l, flags.a, flags.R, flags.r, flags.t); +} + +char **get_files(char **argv) { + + char **files = ft_calloc(ft_strslen(argv), sizeof(char*)); + + for (int i = 1; argv[i] != NULL; i++) { + if (argv[i][0] == '-') { + } + } +} +