42sh 1.0.0
Create a shell in C
Loading...
Searching...
No Matches
my.h
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2023
3** Libmy
4** File description:
5** The header file of the libmy
6*/
13#include <stdlib.h>
14#include <stddef.h>
15#include <stdarg.h>
16#include <unistd.h>
17#include <stdio.h>
18#include <errno.h>
19#include <string.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <sys/wait.h>
24#include <time.h>
25#include <dirent.h>
26#include <signal.h>
27#include <glob.h>
28
29#ifndef MY_H_
30 #define MY_H_
31
32// MACROS :
33
34 // Constants :
35
36 #define MAX_INT 2147483647
37 #define MIN_INT -2147483648
38 #define MAX_LONG 9223372036854775807
39 #define MIN_LONG -9223372036854775808
40 #define MAX_SHORT 32767
41 #define MIN_SHORT -32768
42 #define MAX_SHORT_SHORT 127
43 #define MIN_SHORT_SHORT -128
44 #define MAX_SIZE_T 18446744073709551615
45
46 // Basics macros :
47
48 #define ABS(value) ((value < 0) ? - value : value)
49 #define MAX(a, b) ((a > b) ? a : b)
50 #define MIN(a, b) ((a < b) ? a : b)
51 #define STR_CONTAINS(str, find) (my_strstr(str, find) != NULL) ? 1 : 0
52 #define NB number_settings_t
53
54 // str_to_word_array macros :
55
56 #define STR2ARRAY(str) my_str_to_word_array(str)
57 #define STR2ARRAY_SEP(str, sep) my_str_to_word_array_select(str, sep)
58 #define STR2ARRAY_STR(str, sep) my_str_to_word_array_string(str, sep)
59
60 // Free macros :
61
62 #define FREE(ptr) ptr = my_free_ptr(ptr)
63 #define FREE_WORD_ARRAY(ptr) my_free_array((void **)ptr)
64
65
66
67// TYPEDEFS :
68
69/* A reproduction of the boolean */
70typedef enum my_bool {
71 FALSE = 0, /* FALSE */
72 TRUE = 1 /* TRUE */
73} my_bool_t;
74
75extern int my_errno; /* The error number */
76
77/* The information of the my_params_to_array */
78struct info_param {
79 int length; /*The length of the string */
80 char *str; /* The string */
81 char *copy; /* The copy of the string */
82 char **word_array; /* The array of words */
83};
84
85/* The settings of the my_super_number */
86typedef struct number_settings_s {
87 my_bool_t multiple_signe; /* Allow multiples signes in the string */
88 my_bool_t letter_before; /* Allow letters before the number */
89 my_bool_t letter_after; /* Allow letters after the number */
90 my_bool_t overflow; /* Allow overflow in the number */
92
93 #define NB number_settings_t
94
95
96
97// FUNCTIONS PROTOTYPES :
98
99 // Write functions :
100
107void my_putchar(char c);
108
116void my_isneg(int nb);
117
124void my_putnbr(int nb);
125
133int my_putstr(char const *str);
134
142void my_showstr(char const *str);
143
151void my_showmem(char const *str, int size);
152
161void my_print_combn(int n);
162
170void my_putnbr_base(int nbr, char const *base);
171
178void my_show_word_array(char *const *tab);
179
187void my_print_params(int argc, char **argv);
188
195int my_putstr_error(char const *str);
196
205int my_putstr_sized(char const *str, int size);
206
215int my_putstr_fd(char const *str, int fd);
216
225int my_putstr_fd_free(char *str, int fd);
226
235int my_printf(char const *format, ...);
236
245int my_fprintf(int fd, char const *format, ...);
246
247
248
249 // Char functions :
250
257int my_char_is_alpha(char const c);
258
265int my_char_is_num(char const c);
266
273int my_char_is_printable(char const c);
274
282int my_char_is(char c, const char *char_list);
283
292char *my_add_chr(char *str, char chr);
293
300char my_get_last_char(char *str);
301
302
303
304 // String functions :
305
313int my_str_isalpha(char const *str);
314
321int my_str_isnum(char const *str);
322
329int my_str_islower(char const *str);
330
337int my_str_isupper(char const *str);
338
345int my_str_isprintable(char const *str);
346
355int my_str_is(char *str, const char *char_list);
356
366int my_str_contains(char *str, char *char_list);
367
374int my_strlen(char const *str);
375
385char *my_strcpy(char *dest, char const *src);
386
397char *my_strncpy(char *dest, char const *src, int n);
398
406char *my_revstr(char *str);
407
417char *my_strstr(char *str, char const *to_find);
418
427int my_strcmp(char const *s1, char const *s2);
428
438int my_strncmp(char const *s1, char const *s2, int n);
439
446char *my_strupcase(char *str);
447
454char *my_strlowcase(char *str);
455
463char *my_strcapitalize(char *str);
464
473char *my_strcat(char *dest, char const *src);
474
484char *my_strncat(char *dest, char const *src, int nb);
485
492char *my_strdup(const char *src);
493
502char *my_strndup(const char *src, int n);
503
510char **my_strdup_word_array(char **array);
511
518const char *my_strerror(int error);
519
527int my_count_letter(char const *str, char c);
528
537int my_get_char_index(char *str, char c, int occurrence);
538
546void my_replace_char(char *str, char to_replace, char replace_by);
547
548
549
550 // Int functions :
551
558int my_nbrlen(int nb);
559
567void my_swap(int *a, int *b);
568
576void my_sort_int_array(int *tab, int size);
577
587char *my_convert_base(char const *nbr,
588 char const *base_from, char const *base_to);
589
599char *my_convert_base_unsigned(char const *nbr,
600 char const *base_from, char const *base_to);
601
611char *my_convert_base_size_t(char const *nbr,
612 char const *base_from, char const *base_to);
613
622void my_round_float_str(char *float_nb, char last_char, int i, int enable);
623
624
625
626 // Array functions :
627
634int my_array_len(void **array);
635
642char **my_str_to_word_array(char const *str);
643
652char **my_str_to_word_array_select(char const *str, char const *separator);
653
662char **my_str_to_word_array_string(char const *str, char const *separator);
663
672char **my_super_array(char *str, char *sep);
673
680void my_free_array(void **array);
681
688void *my_free_ptr(void *ptr);
689
690
691
692 // Get number functions :
693
702int my_getnbr(char const *str);
703
712int my_find_nbr(char const *str);
713
723float my_getnbr_float(char const *str);
724
735int my_getnbr_base(char const *str, char const *base);
736
745int my_strict_getnbr(char const *str);
746
755float my_strict_getnbr_float(char const *str);
756
765int my_strict_find_nbr(char const *str);
766
777int my_super_number(char *number, number_settings_t settings);
778
779
780
781 // Calculs functions :
782
790int my_compute_power_rec(int nb, int power);
791
800size_t my_compute_power_rec_size_t(int nb, int p);
801
808int my_compute_square_root(int nb);
809
817int my_compute_factorial_rec(int nb);
818
825int my_is_prime(int nb);
826
834int my_find_prime_sup(int nb);
835
843int my_find_prime_inf(int nb);
844
845
846
847 // Params functions :
848
856struct info_param *my_params_to_array(int ac, char **av);
857
864void my_show_param_array(struct info_param const *par);
865
872void my_rev_params(int argc, char **argv);
873
881void my_sort_params(int argc, char **argv);
882
890char *my_concat_params(int argc, char **argv);
891
892
893
894 // Str nbr functions :
895
902char *my_str_nbr(int nb);
903
910char *my_str_nbr_short(short int nb);
911
918char *my_str_nbr_short_short(signed char nb);
919
928char *my_str_nbr_base_unsigned(unsigned int nbr, char const *base);
929
938char *my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base);
939
948char *my_str_nbr_base_unsigned_short_short(unsigned char nbr,
949 char const *base);
950
959char *my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base);
960
969char *my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base);
970
979char *my_str_nbr_base_long_long_int(long long int nbr, char const *base);
980
987char *my_str_nbr_unsigned(unsigned int nb);
988
995char *my_str_nbr_unsigned_long(unsigned long int nb);
996
1003char *my_str_nbr_long_long(long long nb);
1004
1011char *my_str_nbr_size_t(size_t nb);
1012
1013#endif /* MY_H_ */
char * my_str_nbr_base_unsigned_long(unsigned long nbr, char const *base)
Returns the string conversion of an unsigned long number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_long.c:30
char * my_str_nbr(int nb)
Returns the string conversion of a number (nb)
Definition my_str_nbr.c:31
void my_sort_params(int argc, char **argv)
Sorts the parameters (argc and argv) in ascending order.
Definition my_sort_params.c:52
void my_show_word_array(char *const *tab)
Prints all word in an array (tab)
Definition my_show_word_array.c:15
void my_showstr(char const *str)
Prints a string (str) but replaces non-printable characters with their hexadecimal number.
Definition my_showstr.c:22
int my_putstr_fd_free(char *str, int fd)
Writes a string (str) in a file descriptor (fd), free it and returns the length of the printed string...
Definition my_putstr_fd_free.c:16
int my_str_islower(char const *str)
Checks if a string (str) is lowercase.
Definition my_str_islower.c:16
void my_show_param_array(struct info_param const *par)
Prints the information of a parameter array (par)
Definition my_show_param_array.c:16
int my_compute_power_rec(int nb, int power)
Returns the power (p) of the number (nb)
Definition my_compute_power_rec.c:15
int my_strcmp(char const *s1, char const *s2)
Compares two strings (s1) and (s2)
Definition my_strcmp.c:16
char * my_strndup(const char *src, int n)
Duplicates a string (src) with a defined size (n) and returns the new string.
Definition my_strndup.c:15
void my_putnbr_base(int nbr, char const *base)
Prints a number (nb) in the requested base (base) in stdout.
Definition my_putnbr_base.c:28
int my_find_prime_inf(int nb)
Returns the previous prime number before a number (nb)
Definition my_find_prime_inf.c:15
char * my_strcpy(char *dest, char const *src)
Copies a string (src) and paste it on another string (dest)
Definition my_strcpy.c:15
void my_putnbr(int nb)
Prints a number (nb) in stdout.
Definition my_putnbr.c:28
int my_strict_getnbr(char const *str)
Returns an int number starting from a string number (str)
Definition my_strict_getnbr.c:16
int my_compute_square_root(int nb)
Returns the square root of a number (nb)
Definition my_compute_square_root.c:15
void my_print_params(int argc, char **argv)
Prints all the parameters of the program.
Definition my_print_params.c:15
void my_sort_int_array(int *tab, int size)
Sorts an array (array) with a defined size (size) of integers in ascending order.
Definition my_sort_int_array.c:25
void my_swap(int *a, int *b)
Swaps two integers (a) and (b)
Definition my_swap.c:15
void my_replace_char(char *str, char to_replace, char replace_by)
Replace a char in a string by another char.
Definition my_replace_char.c:15
int my_putstr_fd(char const *str, int fd)
Writes a string (str) in a file descriptor (fd) and returns the length of the printed string.
Definition my_putstr_fd.c:16
int my_is_prime(int nb)
Returns 1 if the number (nb) is prime and 0 otherwise.
Definition my_is_prime.c:15
char * my_str_nbr_unsigned_long(unsigned long int nb)
Returns the string conversion of an unsigned long number (nb)
Definition my_str_nbr_unsigned_long.c:30
int my_strlen(char const *str)
Returns the length of a string (str)
Definition my_strlen.c:15
int my_char_is(char c, const char *char_list)
Checks if a char (c) is in a list of chars (char_list)
Definition my_char_is.c:16
char * my_strncpy(char *dest, char const *src, int n)
Copies a string (src) and paste it on another string (dest) with a defined size (n)
Definition my_strncpy.c:16
char * my_str_nbr_short_short(signed char nb)
Returns the string conversion of a short short number (nb)
Definition my_str_nbr_short_short.c:31
char * my_strupcase(char *str)
Replaces lowcase by upcase of a string (str)
Definition my_strupcase.c:15
int my_find_nbr(char const *str)
Find the first number (number) found in a string.
Definition my_find_nbr.c:15
int my_strncmp(char const *s1, char const *s2, int n)
Compares two strings (s1) and (s2) with a defined size (n)
Definition my_strncmp.c:16
int my_str_is(char *str, const char *char_list)
Checks if a string (str) is composed of characters in the characters list (char_list) or 0 if not.
Definition my_str_is.c:16
int my_array_len(void **array)
Returns the length of an array (array)
Definition my_array_len.c:15
int my_str_isprintable(char const *str)
Checks if a string (str) is printable.
Definition my_str_isprintable.c:15
void my_print_combn(int n)
Prints all the numbers composed by (n) different digits numbers All digits in the number are differen...
Definition my_print_combn.c:56
float my_getnbr_float(char const *str)
Returns a float number starting from a string number (str)
Definition my_getnbr_float.c:24
char ** my_str_to_word_array_select(char const *str, char const *separator)
Returns an array of words delimited by a list of separator (separator) from a string (str)
Definition my_str_to_word_array_select.c:76
char ** my_strdup_word_array(char **array)
Duplicates a word array (array) and returns the new array.
Definition my_strdup_word_array.c:15
char * my_strcapitalize(char *str)
Capitalizes the first letter of each word in a string (str) and lowercase the other letters.
Definition my_strcapitalize.c:15
int my_str_isnum(char const *str)
Checks if a string (str) is a number.
Definition my_str_isnum.c:15
void my_rev_params(int argc, char **argv)
Prints all the arguments (argc and argv) in reverse order.
Definition my_rev_params.c:15
char * my_strstr(char *str, char const *to_find)
Search a string (to_find) on another (str) and returns the address of the first occurrence.
Definition my_strstr.c:16
int my_get_char_index(char *str, char c, int occurrence)
Get the index of a char in a str according to its occurrence.
Definition my_get_char_index.c:15
int my_count_letter(char const *str, char c)
Counts the number of times a letter (c) is in a string (str)
Definition my_count_letter.c:15
int my_str_contains(char *str, char *char_list)
Checks if a string (str) contains a character in a list of characters (char_list)
Definition my_str_contains.c:25
size_t my_compute_power_rec_size_t(int nb, int p)
Returns the power (p) of the number (nb)
Definition my_compute_power_rec_size_t.c:15
char * my_add_chr(char *str, char chr)
Adds a char (chr) at the end of a string (str)
Definition my_add_chr.c:15
int my_str_isupper(char const *str)
Checks if a string (str) is uppercase.
Definition my_str_isupper.c:16
char * my_str_nbr_base_long_long_int(long long int nbr, char const *base)
Returns the string conversion of an long long number (nbr) in specific base (base)
Definition my_str_nbr_base_long_long_int.c:30
char * my_convert_base_unsigned(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a unsigned number (nbr) in a specific base (base_from) to ano...
Definition my_convert_base_unsigned.c:84
char * my_str_nbr_base_unsigned_short_short(unsigned char nbr, char const *base)
Returns the string conversion of an unsigned short short number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_short_short.c:30
char * my_strncat(char *dest, char const *src, int nb)
Concatenates two strings (dest) and (src) with a defined size (nb)
Definition my_strncat.c:16
char * my_strlowcase(char *str)
Replaces upcase by lowcase of a string (str)
Definition my_strlowcase.c:15
char * my_str_nbr_base_unsigned(unsigned int nbr, char const *base)
Returns the string conversion of an unsigned number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned.c:30
int my_getnbr(char const *str)
Returns an int number starting from a string number (str)
Definition my_getnbr.c:24
int my_compute_factorial_rec(int nb)
Returns the factorial of a number (nb)
Definition my_compute_factorial_rec.c:15
int my_char_is_alpha(char const c)
Checks if a char (c) is a letter.
Definition my_char_is_alpha.c:15
int my_printf(char const *format,...)
Prints a string (format) with possible flags and format in stdout and returns the length the printed ...
Definition my_printf.c:87
int my_putstr_error(char const *str)
Prints a string (str) in stderr.
Definition my_putstr_error.c:15
char * my_str_nbr_size_t(size_t nb)
Returns the string conversion of an size_t number (nb)
Definition my_str_nbr_size_t.c:29
void my_isneg(int nb)
Prints 'P' if the number (n) is positive or 'N' if the number (n) is negative.
Definition my_isneg.c:16
void * my_free_ptr(void *ptr)
Frees a pointer (ptr)
Definition my_free_ptr.c:15
char * my_str_nbr_short(short int nb)
Returns the string conversion of a short number (nb)
Definition my_str_nbr_short.c:31
struct info_param * my_params_to_array(int ac, char **av)
Returns a info_params struct of the argc (ac) and the argv (av)
Definition my_params_to_array.c:15
char * my_concat_params(int argc, char **argv)
Returns a string with all the arguments (argc and argv) concatenated.
Definition my_concat_params.c:15
char * my_strcat(char *dest, char const *src)
Concatenates two strings (dest) and (src)
Definition my_strcat.c:16
char * my_convert_base(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a number (nbr) in a specific base (base_from) to another base...
Definition my_convert_base.c:52
char ** my_super_array(char *str, char *sep)
Returns an array of words delimited by a list of separator (separator) from a string (str)
Definition my_super_array.c:74
int my_putstr_sized(char const *str, int size)
Prints a string (str) in stdout with a defined size (size) and returns the length of the printed stri...
Definition my_putstr_sized.c:16
void my_showmem(char const *str, int size)
Prints a memory dump of a string (str) with a defined size.
Definition my_showmem.c:81
void my_free_array(void **array)
Frees an array (array)
Definition my_free_array.c:15
char ** my_str_to_word_array_string(char const *str, char const *separator)
Returns an array of words delimited by a separator (separator) from a string (str)
Definition my_str_to_word_array_string.c:74
char * my_strdup(const char *src)
Duplicates a string (src) and returns the new string.
Definition my_strdup.c:15
char * my_str_nbr_unsigned(unsigned int nb)
Returns the string conversion of an unsigned number (nb)
Definition my_str_nbr_unsigned.c:29
float my_strict_getnbr_float(char const *str)
Returns a float number starting from a string number (str)
Definition my_strict_getnbr_float.c:16
const char * my_strerror(int error)
Returns the error message of the error number (error)
Definition my_strerror.c:149
char my_get_last_char(char *str)
Returns the last char of a string (str)
Definition my_get_last_char.c:15
void my_putchar(char c)
Prints a char (c) in the stdout.
Definition my_putchar.c:15
char ** my_str_to_word_array(char const *str)
Returns an array of words from a string (str)
Definition my_str_to_word_array.c:68
int my_char_is_num(char const c)
Checks if a char (c) is a number.
Definition my_char_is_num.c:15
int my_putstr(char const *str)
Writes a string (str) in stdout and returns the length of the printed string.
Definition my_putstr.c:16
char * my_convert_base_size_t(char const *nbr, char const *base_from, char const *base_to)
Returns the result of the conversion of a size_t number (nbr) in a specific base (base_from) to anoth...
Definition my_convert_base_size_t.c:84
char * my_str_nbr_base_unsigned_short(unsigned short nbr, char const *base)
Returns the string conversion of an unsigned short number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_short.c:30
int my_char_is_printable(char const c)
Checks if a char (c) is printable.
Definition my_char_is_printable.c:15
int my_fprintf(int fd, char const *format,...)
Writes a string (format) with possible flags and format in a file descriptor (fd) and returns the len...
Definition my_fprintf.c:87
char * my_str_nbr_base_unsigned_size_t(size_t nbr, char const *base)
Returns the string conversion of an size_t number (nbr) in specific base (base)
Definition my_str_nbr_base_unsigned_size_t.c:30
char * my_str_nbr_long_long(long long nb)
Returns the string conversion of an long long number (nb)
int my_getnbr_base(char const *str, char const *base)
Returns a number starting from a string number (str) in the requested base (base)
Definition my_getnbr_base.c:45
int my_str_isalpha(char const *str)
Checks if a string (str) is alphabetical.
Definition my_str_isalpha.c:15
int my_super_number(char *number, number_settings_t settings)
Returns an int number starting from a string number (number) and change my_errno if an error occurs d...
Definition my_super_number.c:45
char * my_revstr(char *str)
Reverses the characters in a string (str) and returns the string (str)
Definition my_revstr.c:15
void my_round_float_str(char *float_nb, char last_char, int i, int enable)
Modify a string (float) and round it.
Definition my_round_float_str.c:29
int my_strict_find_nbr(char const *str)
Find the first number (number) found in a string.
Definition my_strict_find_nbr.c:16
int my_nbrlen(int nb)
Returns the length of a number (nb)
Definition my_nbrlen.c:15
int my_find_prime_sup(int nb)
Returns the next prime number after a number (nb)
Definition my_find_prime_sup.c:15
Definition my.h:78
Definition my.h:86