From 98d40c12eb0b8be1fe9ce3b2d5c6874b6d494a49 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 21:39:24 +0300 Subject: [PATCH 01/20] prototypes pog --- main.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.h b/main.h index fe386f6..208cb4d 100644 --- a/main.h +++ b/main.h @@ -1,6 +1,9 @@ #ifndef MAIN_H_ #define MAIN_H_ int _printf(const char *format, ...); +int _contains(char *str); +int _strlen(char *str); +char *_strrev(char *str); #endif From af9400db5040cefd5a2bcf8cf704c9142f92e46f Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 21:42:08 +0300 Subject: [PATCH 02/20] pp --- main.h | 1 - 1 file changed, 1 deletion(-) diff --git a/main.h b/main.h index 208cb4d..55d4145 100644 --- a/main.h +++ b/main.h @@ -3,7 +3,6 @@ int _printf(const char *format, ...); int _contains(char *str); int _strlen(char *str); -char *_strrev(char *str); #endif From ed9384e11f402ba76353804d470a3908d54f7ee2 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 21:50:57 +0300 Subject: [PATCH 03/20] _strlen (docs needed) --- strings.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 strings.c diff --git a/strings.c b/strings.c new file mode 100644 index 0000000..6795fd5 --- /dev/null +++ b/strings.c @@ -0,0 +1,12 @@ +#include "main.h" + +/** + * _strlen - nuts + */ +int _strlen(char *str) +{ + if (*str) + return (1 + _strlen(str++)); + else + return (0); +} From 6a7403a7c41f70c7ca10a93cb2fd735376a5211e Mon Sep 17 00:00:00 2001 From: Supermjork Date: Sat, 19 Aug 2023 22:01:55 +0300 Subject: [PATCH 04/20] Documented '_strlen' --- strings.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/strings.c b/strings.c index 6795fd5..d2c5855 100644 --- a/strings.c +++ b/strings.c @@ -1,7 +1,11 @@ #include "main.h" /** - * _strlen - nuts + * _strlen - Takes string and return its length + * + * @str: Address to the head of the string (Array of Characters) (Lost btw) + * + * Returns: Length of String */ int _strlen(char *str) { From 92738bbd3d76bb2e91745f8adb52062d27e531d6 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 22:27:49 +0300 Subject: [PATCH 05/20] contains implemented --- main.h | 2 +- strings.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/main.h b/main.h index 55d4145..ed076e9 100644 --- a/main.h +++ b/main.h @@ -1,7 +1,7 @@ #ifndef MAIN_H_ #define MAIN_H_ int _printf(const char *format, ...); -int _contains(char *str); +int _contains(char *str, char c); int _strlen(char *str); diff --git a/strings.c b/strings.c index d2c5855..00365f2 100644 --- a/strings.c +++ b/strings.c @@ -14,3 +14,24 @@ int _strlen(char *str) else return (0); } + +/** + * _contains - gets number of unique identifiers + * + * @str: da string + * @c: basically almost always '%' + * + * Return: number of unique cases of "%*" that aren't "%%" + */ +int _contains(char *str, char c) +{ + if (*str) + { + if (*str == c && *(str + 1) != c && *(str - 1) != c) + return (1 + _contains(str + 1, c)); + else + return (_contains(str + 1, c)); + } + else + return (0); +} From 51f1e32eb2992257beac5706c922fe400e584103 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 23:04:35 +0300 Subject: [PATCH 06/20] why does this segfault? --- main.h | 2 +- strings.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main.h b/main.h index ed076e9..4955293 100644 --- a/main.h +++ b/main.h @@ -1,7 +1,7 @@ #ifndef MAIN_H_ #define MAIN_H_ int _printf(const char *format, ...); -int _contains(char *str, char c); +int _contains(const char *str, char c); int _strlen(char *str); diff --git a/strings.c b/strings.c index 00365f2..253796d 100644 --- a/strings.c +++ b/strings.c @@ -9,7 +9,7 @@ */ int _strlen(char *str) { - if (*str) + if (str) return (1 + _strlen(str++)); else return (0); @@ -23,7 +23,7 @@ int _strlen(char *str) * * Return: number of unique cases of "%*" that aren't "%%" */ -int _contains(char *str, char c) +int _contains(const char *str, char c) { if (*str) { From 179f8618f4e22f9f8bbc41431b2972adbeb44f38 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 23:13:06 +0300 Subject: [PATCH 07/20] ++str not str++ --- strings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings.c b/strings.c index 253796d..c0af1f6 100644 --- a/strings.c +++ b/strings.c @@ -9,8 +9,8 @@ */ int _strlen(char *str) { - if (str) - return (1 + _strlen(str++)); + if (*str) + return (1 + _strlen(++str)); else return (0); } From 901478b06033bf5be6587a9264682e1ee2dd520d Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 23:32:13 +0300 Subject: [PATCH 08/20] test for strlen (no longer segfaults) --- tests/strlen.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/strlen.c diff --git a/tests/strlen.c b/tests/strlen.c new file mode 100644 index 0000000..3fe6fca --- /dev/null +++ b/tests/strlen.c @@ -0,0 +1,19 @@ +#include "../main.h" + +/** + * main - test strlen + * Return: 0 if yes, 1 if no + */ +int main(void) +{ + char *str; + int len; + + str = "hello"; + len =_strlen(str); + if (len == 5) + return (0); + else + return (1); + +} From b2fca0820dc53a393d6d9d9027544c351527042c Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sat, 19 Aug 2023 23:36:00 +0300 Subject: [PATCH 09/20] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c6127b3..366a2f5 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,4 @@ modules.order Module.symvers Mkfile.old dkms.conf +/tests/a From 1c607342b4634adfff914d47ea9550ce960f2dc4 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 20 Aug 2023 14:39:25 +0300 Subject: [PATCH 10/20] Im nice --- printf.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 printf.c diff --git a/printf.c b/printf.c new file mode 100644 index 0000000..370fc5b --- /dev/null +++ b/printf.c @@ -0,0 +1,23 @@ +#include "main.h" +#include + +int _printf(const char *format, ...) +{ + unsigned int thingies; /*how many %* that aren't %*/ + char *buffer; + int i; + va_list stuff; + + thingies = _contains(format, '%'); + va_start(stuff, format); + + if (!format) + return (0); + while(format) + { + if (*format) + } + + + +} From 64070c815252ad27bdcd470fccdff9ae0aeca3b0 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Sun, 20 Aug 2023 15:38:49 +0300 Subject: [PATCH 11/20] lawd help meh --- main.h | 2 +- printf.c | 48 +++++++++++++++++++++++++++++++++++++---------- strings.c | 2 +- tests/str_print.c | 19 +++++++++++++++++++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 tests/str_print.c diff --git a/main.h b/main.h index 4955293..feaa492 100644 --- a/main.h +++ b/main.h @@ -2,7 +2,7 @@ #define MAIN_H_ int _printf(const char *format, ...); int _contains(const char *str, char c); -int _strlen(char *str); +int _strlen(const char *str); #endif diff --git a/printf.c b/printf.c index 370fc5b..cec8fee 100644 --- a/printf.c +++ b/printf.c @@ -1,23 +1,51 @@ #include "main.h" -#include +#include int _printf(const char *format, ...) { - unsigned int thingies; /*how many %* that aren't %*/ - char *buffer; - int i; - va_list stuff; + int i = 0; + int BUFF_SIZE = _strlen(format); + unsigned int identifiers; /* how many %* that aren't %% */ + va_list args; - thingies = _contains(format, '%'); - va_start(stuff, format); + BUFF_SIZE = _strlen(format); + + char* buffer[BUFF_SIZE]; + + /* Number of Identifiers */ + identifiers = _contains(format, '%'); + + va_start(args, format); if (!format) return (0); + while(format) { - if (*format) + if (*format == '%') + { + switch (*(format + 1)) + { + case 's': + break; + case 'c': + break; + case '%': + break; + } + /* + * something something increment i + * by size of argument value + * also no forgor about realloc + */ + } + else + { + /* ERROR SCREAMING AT ME PLS HELP */ + *(buffer + i) = (format + i); + i++; + } } - - + return (identifiers); } diff --git a/strings.c b/strings.c index c0af1f6..0f1422e 100644 --- a/strings.c +++ b/strings.c @@ -7,7 +7,7 @@ * * Returns: Length of String */ -int _strlen(char *str) +int _strlen(const char *str) { if (*str) return (1 + _strlen(++str)); diff --git a/tests/str_print.c b/tests/str_print.c new file mode 100644 index 0000000..951dd74 --- /dev/null +++ b/tests/str_print.c @@ -0,0 +1,19 @@ +#include<../main.h> +#include + +/** + * main - Tests if '%s' works within our printf. + * + * Return: 0 On Failure, 1 Otherwise Success + */ + +int main(void) +{ + char test_0[] = "Hej"; + char test_1[] = "H,e,j"; +/* + * Brain ded to think bout test cond + */ + + return (0); +} From cc9901e2810d45ad489e2030289fb0f02d0ca6b7 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Sun, 20 Aug 2023 17:45:17 +0300 Subject: [PATCH 12/20] Skeleton %s --- printf.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/printf.c b/printf.c index cec8fee..a40068e 100644 --- a/printf.c +++ b/printf.c @@ -3,34 +3,73 @@ int _printf(const char *format, ...) { + /* For any Iterative Operation */ int i = 0; - int BUFF_SIZE = _strlen(format); - unsigned int identifiers; /* how many %* that aren't %% */ - va_list args; - BUFF_SIZE = _strlen(format); - - char* buffer[BUFF_SIZE]; + /* how many %* that aren't %% */ + unsigned int identifiers; /* Number of Identifiers */ identifiers = _contains(format, '%'); + /* Primary Buffer Size */ + int BUFF_SIZE = _strlen(format) - (identifiers * 2); + + /* Instantiating Buffer */ + char* buffer[BUFF_SIZE]; + + /* Shoving args in list */ + va_list args; va_start(args, format); + /* If string is nonexist, die */ if (!format) return (0); + /* While string is alive or smth*/ while(format) { - if (*format == '%') + /* Detecting identifier in code + * whilst ensuring next char exists + */ + if ((*format == '%') && (*(format + 1) != '\0')) { + /* Handling depending on char after identifier*/ switch (*(format + 1)) { case 's': + /* Something _strlen() string arg, realloc + * Increment i by strlen or smth + */ + + /* Trying to get length of passed string */ + int pass_str_len = _strlen(va_arg(args, char*)); + + /* Incrememnting buffersize to fit new string*/ + BUFF_SIZE += pass_str_len; + + /* Shoving str values into buffer*/ + + + /* Realloc? pls review*/ + free(buffer); + buffer = malloc(BUFF_SIZE * sizeof(char)); + + /* Incrementing iterator i by string length + * So we'd be able to insert at proper + * index in buffer + */ + i += pass_str_len; break; case 'c': + /* Something add 1B to buffer and realloc + * Increment i by 1 + */ break; case '%': + /* Something add 1B, shove %, realloc + * increment i by 1 + */ break; } /* @@ -42,7 +81,7 @@ int _printf(const char *format, ...) else { /* ERROR SCREAMING AT ME PLS HELP */ - *(buffer + i) = (format + i); + *(buffer + i) = (char *)(format + i); i++; } } From e6d8091dafc3da444e1583a872f3dd5fd2cf84b1 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 20 Aug 2023 20:16:46 +0300 Subject: [PATCH 13/20] I can't even begin to address what was wrong here 1. space missing in #include 2. moved declarations above all so betty approves 3. No more mid code variables 4. malloced the buffer initially so its actually on the heap before freeing 5. added a missing * for the error that was upsetting in the bottom 6. Returning the value of strlen instead of identifiers --- printf.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/printf.c b/printf.c index a40068e..be5c0e3 100644 --- a/printf.c +++ b/printf.c @@ -1,31 +1,30 @@ #include "main.h" -#include +#include +#include int _printf(const char *format, ...) { /* For any Iterative Operation */ - int i = 0; + int i, pass_str_len; + unsigned int identifiers, BUFF_SIZE; + char *buffer; + va_list args; - /* how many %* that aren't %% */ - unsigned int identifiers; - - /* Number of Identifiers */ - identifiers = _contains(format, '%'); - - /* Primary Buffer Size */ - int BUFF_SIZE = _strlen(format) - (identifiers * 2); - - /* Instantiating Buffer */ - char* buffer[BUFF_SIZE]; + /* Instantiating Buffer Size */ + BUFF_SIZE = _strlen(format) - (identifiers * 2); /* Shoving args in list */ - va_list args; va_start(args, format); + /* Number of Identifiers that aren't "%%" */ + identifiers = _contains(format, '%'); + /* If string is nonexist, die */ if (!format) return (0); + buffer = malloc(BUFF_SIZE); + /* While string is alive or smth*/ while(format) { @@ -43,7 +42,7 @@ int _printf(const char *format, ...) */ /* Trying to get length of passed string */ - int pass_str_len = _strlen(va_arg(args, char*)); + pass_str_len = _strlen(va_arg(args, char*)); /* Incrememnting buffersize to fit new string*/ BUFF_SIZE += pass_str_len; @@ -80,11 +79,11 @@ int _printf(const char *format, ...) } else { - /* ERROR SCREAMING AT ME PLS HELP */ - *(buffer + i) = (char *)(format + i); + /* Dereferencing with '*' */ + *(buffer + i) = *(char *)(format + i); i++; } } - return (identifiers); + return (_strlen(buffer)); } From 0df9b1789c121e4d10ede7b6be2d72af49bc8481 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 20 Aug 2023 23:02:01 +0300 Subject: [PATCH 14/20] this segfaults idk why --- printf.c | 68 ++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/printf.c b/printf.c index be5c0e3..172d493 100644 --- a/printf.c +++ b/printf.c @@ -1,72 +1,56 @@ #include "main.h" #include #include +#include +/** + * _printf - printf but worse + * @format: format string + * @...: ??? + * Return: characters written + **/ int _printf(const char *format, ...) { - /* For any Iterative Operation */ - int i, pass_str_len; + int i, j; unsigned int identifiers, BUFF_SIZE; - char *buffer; + char *buffer, *next; va_list args; - /* Instantiating Buffer Size */ - BUFF_SIZE = _strlen(format) - (identifiers * 2); - /* Shoving args in list */ va_start(args, format); - /* Number of Identifiers that aren't "%%" */ identifiers = _contains(format, '%'); + BUFF_SIZE = _strlen(format) - (identifiers * 2); - /* If string is nonexist, die */ - if (!format) + if (!format) /* No string. No laundry */ return (0); - buffer = malloc(BUFF_SIZE); - /* While string is alive or smth*/ - while(format) + i = 0; /*TODO:gotta rename those*/ + j = 0; + buffer = ""; + while (format) { - /* Detecting identifier in code - * whilst ensuring next char exists - */ - if ((*format == '%') && (*(format + 1) != '\0')) + if ((*format == '%') && (*(format + 1))) { - /* Handling depending on char after identifier*/ switch (*(format + 1)) { case 's': - /* Something _strlen() string arg, realloc - * Increment i by strlen or smth - */ + next = va_arg(args, char*); /*Store string temporarily*/ + _strcpy(&buffer[j], next); + j += _strlen(next); + BUFF_SIZE += _strlen(next); - /* Trying to get length of passed string */ - pass_str_len = _strlen(va_arg(args, char*)); - - /* Incrememnting buffersize to fit new string*/ - BUFF_SIZE += pass_str_len; - - /* Shoving str values into buffer*/ - - - /* Realloc? pls review*/ - free(buffer); - buffer = malloc(BUFF_SIZE * sizeof(char)); - - /* Incrementing iterator i by string length - * So we'd be able to insert at proper - * index in buffer - */ - i += pass_str_len; break; case 'c': - /* Something add 1B to buffer and realloc + /* + * You don't add 1Byte because %c becomes a single char so less to reallocate * Increment i by 1 */ break; case '%': - /* Something add 1B, shove %, realloc + /* + * Something add 1B, shove %, realloc * increment i by 1 */ break; @@ -79,11 +63,11 @@ int _printf(const char *format, ...) } else { - /* Dereferencing with '*' */ - *(buffer + i) = *(char *)(format + i); + *(buffer + i) = *(format + i); i++; } } + write(1, buffer, BUFF_SIZE); return (_strlen(buffer)); } From ac332f48ac6fb5998b93e467d73f745fa6e1cdf6 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Sun, 20 Aug 2023 23:08:19 +0300 Subject: [PATCH 15/20] please help --- printf.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/printf.c b/printf.c index 172d493..8d74049 100644 --- a/printf.c +++ b/printf.c @@ -18,22 +18,18 @@ int _printf(const char *format, ...) va_start(args, format); - identifiers = _contains(format, '%'); BUFF_SIZE = _strlen(format) - (identifiers * 2); if (!format) /* No string. No laundry */ return (0); - - i = 0; /*TODO:gotta rename those*/ j = 0; - buffer = ""; while (format) { if ((*format == '%') && (*(format + 1))) { - switch (*(format + 1)) + switch (*(format + 1)) /*this needs to shrink*/ { case 's': next = va_arg(args, char*); /*Store string temporarily*/ @@ -42,24 +38,11 @@ int _printf(const char *format, ...) BUFF_SIZE += _strlen(next); break; - case 'c': - /* - * You don't add 1Byte because %c becomes a single char so less to reallocate - * Increment i by 1 - */ + case 'c': /* add 1 byte and i++ */ break; - case '%': - /* - * Something add 1B, shove %, realloc - * increment i by 1 - */ + case '%': /*add 1 byte*/ break; } - /* - * something something increment i - * by size of argument value - * also no forgor about realloc - */ } else { From 64d38b771dac6754b0bb7476aeb0e59b73c65dbf Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Mon, 21 Aug 2023 11:22:58 +0300 Subject: [PATCH 16/20] fixed some stuff (100%) --- main.h | 1 + printf.c | 19 +++++++++---------- strings.c | 28 ++++++++++++++-------------- tests/str_print.c | 7 +++---- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/main.h b/main.h index feaa492..bcf973d 100644 --- a/main.h +++ b/main.h @@ -3,6 +3,7 @@ int _printf(const char *format, ...); int _contains(const char *str, char c); int _strlen(const char *str); +char *_strcpy(char *dest, char *src); #endif diff --git a/printf.c b/printf.c index 8d74049..13f533c 100644 --- a/printf.c +++ b/printf.c @@ -11,7 +11,7 @@ **/ int _printf(const char *format, ...) { - int i, j; + int buff_idx; unsigned int identifiers, BUFF_SIZE; char *buffer, *next; va_list args; @@ -20,22 +20,21 @@ int _printf(const char *format, ...) va_start(args, format); identifiers = _contains(format, '%'); BUFF_SIZE = _strlen(format) - (identifiers * 2); + buffer = malloc(BUFF_SIZE); if (!format) /* No string. No laundry */ return (0); - i = 0; /*TODO:gotta rename those*/ - j = 0; + buff_idx = 0; while (format) { - if ((*format == '%') && (*(format + 1))) + if ((*format == '%') && (*(format + 1))) /*hello %s*/ { switch (*(format + 1)) /*this needs to shrink*/ { case 's': next = va_arg(args, char*); /*Store string temporarily*/ - _strcpy(&buffer[j], next); - j += _strlen(next); - BUFF_SIZE += _strlen(next); + buffer = _strcpy(buffer, next); + BUFF_SIZE += _strlen(buffer); break; case 'c': /* add 1 byte and i++ */ @@ -46,11 +45,11 @@ int _printf(const char *format, ...) } else { - *(buffer + i) = *(format + i); - i++; + *(buffer + buff_idx) = *(format + buff_idx); + buff_idx++; } } - write(1, buffer, BUFF_SIZE); + write(1, &buffer, BUFF_SIZE); return (_strlen(buffer)); } diff --git a/strings.c b/strings.c index 0f1422e..da4b1c7 100644 --- a/strings.c +++ b/strings.c @@ -5,14 +5,14 @@ * * @str: Address to the head of the string (Array of Characters) (Lost btw) * - * Returns: Length of String + * Return: Length of String */ int _strlen(const char *str) { - if (*str) - return (1 + _strlen(++str)); - else - return (0); + if (*str) + return (1 + _strlen(++str)); + else + return (0); } /** @@ -25,13 +25,13 @@ int _strlen(const char *str) */ int _contains(const char *str, char c) { - if (*str) - { - if (*str == c && *(str + 1) != c && *(str - 1) != c) - return (1 + _contains(str + 1, c)); - else - return (_contains(str + 1, c)); - } - else - return (0); + if (*str) + { + if (*str == c && *(str + 1) != c && *(str - 1) != c) + return (1 + _contains(str + 1, c)); + else + return (_contains(str + 1, c)); + } + else + return (0); } diff --git a/tests/str_print.c b/tests/str_print.c index 951dd74..0585c23 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -1,5 +1,5 @@ -#include<../main.h> -#include +#include "../main.h" +#include /** * main - Tests if '%s' works within our printf. @@ -10,10 +10,9 @@ int main(void) { char test_0[] = "Hej"; - char test_1[] = "H,e,j"; /* * Brain ded to think bout test cond */ + return (_printf("%s", test_0)); - return (0); } From 5df61fff5bf2744d7382c6c732cba5e38ca1bb64 Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Mon, 21 Aug 2023 11:23:56 +0300 Subject: [PATCH 17/20] strcpy and its tests :))) --- .gitignore | 1 + strcpy.c | 28 ++++++++++++++++++++++++++++ tests/strcpy.c | 17 +++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 strcpy.c create mode 100644 tests/strcpy.c diff --git a/.gitignore b/.gitignore index 366a2f5..0fa4b0f 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,4 @@ Module.symvers Mkfile.old dkms.conf /tests/a +/*.org diff --git a/strcpy.c b/strcpy.c new file mode 100644 index 0000000..3f43ee2 --- /dev/null +++ b/strcpy.c @@ -0,0 +1,28 @@ +#include "main.h" +#include + +/** + * _strcpy - strcpy with an n + * @dest: the buffer we copying to + * @src: the idot we ctrl+c-ed + * Return: dest + */ +char *_strcpy(char *dest, char *src) +{ + int s_idx, i, strlen; + char *new_me; + + new_me = malloc(_strlen(dest) + _strlen(src) + 1); + strlen = _strlen(src); + + s_idx = -1; + while (*(dest + ++s_idx)) + *(new_me + s_idx) = *(dest + s_idx); + + for (i = 0; i < strlen; i++) + *(new_me + s_idx++) = *(src + i); + *(new_me + _strlen(new_me)) = '\0'; + + + return (new_me); +} diff --git a/tests/strcpy.c b/tests/strcpy.c new file mode 100644 index 0000000..d2edc4b --- /dev/null +++ b/tests/strcpy.c @@ -0,0 +1,17 @@ +#include "../main.h" +#include +#include +/** + * main - why does this not work! I know! + * Return: 0...unless? + */ +int main(void) +{ + char *str, *str2; + + str = "hello"; + str2 = " world!\n"; + str = _strcpy(str, str2); + printf("%s", str); + return (0); +} From 6c756ea867ebf0f05ba933fec047ed5c94e495ee Mon Sep 17 00:00:00 2001 From: LinlyBoi Date: Mon, 21 Aug 2023 11:39:20 +0300 Subject: [PATCH 18/20] yes --- printf.c | 14 +++++++++----- strcpy.c | 2 ++ tests/hello.c | 3 ++- tests/str_print.c | 5 +++-- tests/test.sh | 3 +++ 5 files changed, 19 insertions(+), 8 deletions(-) create mode 100755 tests/test.sh diff --git a/printf.c b/printf.c index 13f533c..fc86c4b 100644 --- a/printf.c +++ b/printf.c @@ -11,7 +11,7 @@ **/ int _printf(const char *format, ...) { - int buff_idx; + int buff_idx, fmt_idx; unsigned int identifiers, BUFF_SIZE; char *buffer, *next; va_list args; @@ -25,7 +25,8 @@ int _printf(const char *format, ...) if (!format) /* No string. No laundry */ return (0); buff_idx = 0; - while (format) + fmt_idx = 0; + while (*(format + fmt_idx)) { if ((*format == '%') && (*(format + 1))) /*hello %s*/ { @@ -34,7 +35,9 @@ int _printf(const char *format, ...) case 's': next = va_arg(args, char*); /*Store string temporarily*/ buffer = _strcpy(buffer, next); - BUFF_SIZE += _strlen(buffer); + BUFF_SIZE = _strlen(buffer); + buff_idx += _strlen(next); + fmt_idx += 2; break; case 'c': /* add 1 byte and i++ */ @@ -45,11 +48,12 @@ int _printf(const char *format, ...) } else { - *(buffer + buff_idx) = *(format + buff_idx); + *(buffer + buff_idx) = *(format + fmt_idx); buff_idx++; + fmt_idx++; } } - write(1, &buffer, BUFF_SIZE); + write(1, buffer, BUFF_SIZE); return (_strlen(buffer)); } diff --git a/strcpy.c b/strcpy.c index 3f43ee2..ed0fd48 100644 --- a/strcpy.c +++ b/strcpy.c @@ -23,6 +23,8 @@ char *_strcpy(char *dest, char *src) *(new_me + s_idx++) = *(src + i); *(new_me + _strlen(new_me)) = '\0'; + free(dest); + return (new_me); } diff --git a/tests/hello.c b/tests/hello.c index 368a804..d63f930 100644 --- a/tests/hello.c +++ b/tests/hello.c @@ -8,5 +8,6 @@ int main(void) { int len; - len = _printf("Hello world"); + len = _printf("Hello world\n"); + return (len); } diff --git a/tests/str_print.c b/tests/str_print.c index 0585c23..4f865ee 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -9,10 +9,11 @@ int main(void) { - char test_0[] = "Hej"; + char *test_0 = "Hej"; /* * Brain ded to think bout test cond */ - return (_printf("%s", test_0)); + _printf("%s \n", test_0); + return (_printf("hey, %s", test_0)); } diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..363f67a --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,3 @@ +#!/usr/bin/bash +gcc -Wall -pedantic -Werror -Wextra -std=gnu89 $1 ../*.c -o a +./a From 960ef386ee76111dc4cdd9d726093cb99a3474bb Mon Sep 17 00:00:00 2001 From: Supermjork Date: Mon, 21 Aug 2023 12:19:24 +0300 Subject: [PATCH 19/20] betty passes, write + 1, NO RETURN _PRTINF PLS --- printf.c | 18 ++++++------------ strcpy.c | 3 --- tests/str_print.c | 5 +++-- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/printf.c b/printf.c index fc86c4b..c96a1e9 100644 --- a/printf.c +++ b/printf.c @@ -2,7 +2,6 @@ #include #include #include - /** * _printf - printf but worse * @format: format string @@ -11,24 +10,21 @@ **/ int _printf(const char *format, ...) { - int buff_idx, fmt_idx; - unsigned int identifiers, BUFF_SIZE; + int buff_idx = 0; + int fmt_idx = 0; + unsigned int identifiers = _contains(format, '%'); + unsigned int BUFF_SIZE = _strlen(format) - (identifiers * 2); char *buffer, *next; va_list args; - va_start(args, format); - identifiers = _contains(format, '%'); - BUFF_SIZE = _strlen(format) - (identifiers * 2); buffer = malloc(BUFF_SIZE); if (!format) /* No string. No laundry */ return (0); - buff_idx = 0; - fmt_idx = 0; while (*(format + fmt_idx)) { - if ((*format == '%') && (*(format + 1))) /*hello %s*/ + if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1))) { switch (*(format + 1)) /*this needs to shrink*/ { @@ -38,7 +34,6 @@ int _printf(const char *format, ...) BUFF_SIZE = _strlen(buffer); buff_idx += _strlen(next); fmt_idx += 2; - break; case 'c': /* add 1 byte and i++ */ break; @@ -53,7 +48,6 @@ int _printf(const char *format, ...) fmt_idx++; } } - - write(1, buffer, BUFF_SIZE); + write(1, buffer, BUFF_SIZE + 1); return (_strlen(buffer)); } diff --git a/strcpy.c b/strcpy.c index ed0fd48..c37a3ca 100644 --- a/strcpy.c +++ b/strcpy.c @@ -23,8 +23,5 @@ char *_strcpy(char *dest, char *src) *(new_me + s_idx++) = *(src + i); *(new_me + _strlen(new_me)) = '\0'; - free(dest); - - return (new_me); } diff --git a/tests/str_print.c b/tests/str_print.c index 4f865ee..d27eb2b 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -10,10 +10,11 @@ int main(void) { char *test_0 = "Hej"; + char *test_1 = "pls"; /* * Brain ded to think bout test cond */ - _printf("%s \n", test_0); - return (_printf("hey, %s", test_0)); + _printf("%s\n%s\n", test_0, test_1); + return (0); } From 71d791acdbef6e459ec711c266321e43eb2223e6 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Mon, 21 Aug 2023 12:27:51 +0300 Subject: [PATCH 20/20] IT WORKS ON MY MACHINE --- tests/str_print.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/str_print.c b/tests/str_print.c index d27eb2b..ac327b8 100644 --- a/tests/str_print.c +++ b/tests/str_print.c @@ -14,7 +14,7 @@ int main(void) /* * Brain ded to think bout test cond */ - _printf("%s\n%s\n", test_0, test_1); + _printf(" suh %s\n%s\n", test_0, test_1); return (0); }