Skip to content

Commit

Permalink
some util function from ft_traceroute
Browse files Browse the repository at this point in the history
- ansi:
  - add dimmed + rst modifier
  - add RAINBOW_C

- network:
  - ipv4:
    - add ft_getip_str() (need to refactor)

  - packet:
    - udp:
      - add ori_data to t_icmphdr_time_exceeded
  • Loading branch information
Pixailz committed May 23, 2024
1 parent 41c4ddf commit 58ecb7f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
15 changes: 15 additions & 0 deletions inc/libft_ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,25 @@
/* MODIFIER */
# define RST _ANSI_M("0") // RESET
# define BOL _ANSI_M("1") // BOLD
# define DIM _ANSI_M("2") // DIMMED
# define ITA _ANSI_M("3") // ITALIC
# define UND _ANSI_M("4") // UNDERLINE
# define BLI _ANSI_M("5") // BLINKING

/* MODIFIER RESET */
# define RBOL _ANSI_M("21") // RESET BOLD
# define RDIM _ANSI_M("22") // RESET DIMMED
# define RITA _ANSI_M("23") // RESET ITALIC
# define RUND _ANSI_M("24") // RESET UNDERLINE
# define RBLI _ANSI_M("25") // RESET BLINKING

/* RESET AND MODIFIER RESET */
# define RSTBOL _ANSI_M("0;21") // RESET AND RESET BOLD
# define RSTDIM _ANSI_M("0;22") // RESET AND RESET DIMMED
# define RSTITA _ANSI_M("0;23") // RESET AND RESET ITALIC
# define RSTUND _ANSI_M("0;24") // RESET AND RESET UNDERLINE
# define RSTBLI _ANSI_M("0;25") // RESET AND RESET BLINKING

/* CURSOR */
# define CUR_COL(C) CSI C "G"

Expand Down
1 change: 1 addition & 0 deletions inc/libft_network/ipv4.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ void ft_ntop(t_bin pf, t_int4 ip, char *ip_str);

// network/ipv4/ft_putip_fd.c
t_size ft_putip_fd(t_int4 n, int fd);
char *ft_getip_str(t_int4 n);

/* ########################################################################## */

Expand Down
3 changes: 2 additions & 1 deletion inc/libft_network/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 00:48:48 by brda-sil #+# #+# */
/* Updated: 2024/05/12 17:57:11 by brda-sil ### ########.fr */
/* Updated: 2024/05/24 00:27:52 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -170,6 +170,7 @@ typedef struct __attribute__((__packed__)) s_icmphdr_time_exceed
t_uint32 unused;

t_iphdr ori_iphdr;
t_uint8 ori_data[8];
} t_icmphdr_time_exceed;

/* ########################################################################## */
Expand Down
6 changes: 5 additions & 1 deletion inc/libft_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ void ft_printf_type_padding_addr(t_fmt_conf *conf);
void ft_printf_type_padding_char(t_fmt_conf *conf);

// print/ft_printf/padding/hex.c
void ft_printf_type_padding_hex_hashtag(t_fmt_conf *conf, t_size str_len);
void ft_printf_type_padding_hex_hashtag(t_fmt_conf *conf, \
t_size str_len);
void ft_printf_type_padding_hex_post(t_fmt_conf *conf, char sign, \
t_size str_len);
void ft_printf_type_padding_hex_pad(t_fmt_conf *conf, t_size begin, \
Expand Down Expand Up @@ -208,6 +209,9 @@ size_t ft_putnstr_fd(char const *s, t_size n, int fd);
// print/ft_putunbr_fd.c
size_t ft_putunbr_fd(unsigned long n, int fd);

// print/ft_rainbow_c.c
char *RAINBOW_C(int i);

// print/ft_vdprintf.c
t_size ft_vdprintf(int fd, const char *format, va_list args);

Expand Down
1 change: 1 addition & 0 deletions mk/srcs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ SRC_PRT := print/ft_dprintf.c \
print/ft_putnbr_fd.c \
print/ft_putstr_fd.c \
print/ft_putunbr_fd.c \
print/ft_rainbow_c.c \
print/ft_vdprintf.c \
print/ft_vprintf.c

Expand Down
43 changes: 42 additions & 1 deletion src/network/ipv4/ft_putip_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/07 03:36:06 by brda-sil #+# #+# */
/* Updated: 2023/07/31 18:25:09 by brda-sil ### ########.fr */
/* Updated: 2024/05/24 00:39:25 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,3 +25,44 @@ t_size ft_putip_fd(t_int4 n, int fd)
printed = ft_putunbr_fd(ft_int4_dcomp(n, 0), fd);
return (printed);
}

// static set_part()

char *ft_getip_str(t_int4 n)
{
char *ptr;
char *tmp_n;
t_size index;
t_size len_tmp_n;

ptr = (char *)ft_calloc(15, sizeof(char));

tmp_n = ft_itoa(ft_int4_dcomp(n, 3));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr, tmp_n, len_tmp_n);
free(tmp_n);
index = len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 2));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 1));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;
ptr[index++] = '.';

tmp_n = ft_itoa(ft_int4_dcomp(n, 0));
len_tmp_n = ft_strlen(tmp_n);
ft_memcpy(ptr + index, tmp_n, len_tmp_n);
free(tmp_n);
index += len_tmp_n;

return ptr;
}
37 changes: 37 additions & 0 deletions src/print/ft_rainbow_c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rainbow_c.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brda-sil <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/24 00:24:30 by brda-sil #+# #+# */
/* Updated: 2024/05/24 00:26:41 by brda-sil ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft_print.h"

# define LEN_RAINBOW_C 7

char *RAINBOW_C(int i)
{
switch (i % LEN_RAINBOW_C)
{
case (0):
return (PUR);
case (1):
return (BLU);
case (2):
return (CYA);
case (3):
return (GRE);
case (4):
return (YEL);
case (5):
return (ORA);
case (6):
return (RED);
}
return "";
}

0 comments on commit 58ecb7f

Please sign in to comment.