diff --git a/.gitignore b/.gitignore index c6127b3..0fa4b0f 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,5 @@ modules.order Module.symvers Mkfile.old dkms.conf +/tests/a +/*.org diff --git a/main.h b/main.h index fe386f6..bcf973d 100644 --- a/main.h +++ b/main.h @@ -1,6 +1,9 @@ #ifndef MAIN_H_ #define MAIN_H_ 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 new file mode 100644 index 0000000..c96a1e9 --- /dev/null +++ b/printf.c @@ -0,0 +1,53 @@ +#include "main.h" +#include +#include +#include +/** + * _printf - printf but worse + * @format: format string + * @...: ??? + * Return: characters written + **/ +int _printf(const char *format, ...) +{ + 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); + buffer = malloc(BUFF_SIZE); + + if (!format) /* No string. No laundry */ + return (0); + while (*(format + fmt_idx)) + { + if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1))) + { + switch (*(format + 1)) /*this needs to shrink*/ + { + case 's': + next = va_arg(args, char*); /*Store string temporarily*/ + buffer = _strcpy(buffer, next); + BUFF_SIZE = _strlen(buffer); + buff_idx += _strlen(next); + fmt_idx += 2; + break; + case 'c': /* add 1 byte and i++ */ + break; + case '%': /*add 1 byte*/ + break; + } + } + else + { + *(buffer + buff_idx) = *(format + fmt_idx); + buff_idx++; + fmt_idx++; + } + } + write(1, buffer, BUFF_SIZE + 1); + return (_strlen(buffer)); +} diff --git a/strcpy.c b/strcpy.c new file mode 100644 index 0000000..c37a3ca --- /dev/null +++ b/strcpy.c @@ -0,0 +1,27 @@ +#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/strings.c b/strings.c new file mode 100644 index 0000000..da4b1c7 --- /dev/null +++ b/strings.c @@ -0,0 +1,37 @@ +#include "main.h" + +/** + * _strlen - Takes string and return its length + * + * @str: Address to the head of the string (Array of Characters) (Lost btw) + * + * Return: Length of String + */ +int _strlen(const char *str) +{ + if (*str) + return (1 + _strlen(++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(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); +} 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 new file mode 100644 index 0000000..ac327b8 --- /dev/null +++ b/tests/str_print.c @@ -0,0 +1,20 @@ +#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 = "pls"; +/* + * Brain ded to think bout test cond + */ + _printf(" suh %s\n%s\n", test_0, test_1); + return (0); + +} 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); +} 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); + +} 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