From f28e6dbd4ad6566dd1cc477a4c0b26f1268c388d Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 00:36:41 +0000 Subject: [PATCH 01/11] new README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 11ab88e..8cb0d25 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ First Holberton School group project in the Low Level Programming track. ## Authors -Fernando González-Morales, Stefan Silverio +`Fernando González-Morales, Stefan Silverio` From 15c65d4bc4295899a834fb0682fbfc4fe175be77 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Sun, 21 Oct 2018 00:46:32 +0000 Subject: [PATCH 02/11] Added _putchar.c --- _putchar.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 _putchar.c diff --git a/_putchar.c b/_putchar.c new file mode 100644 index 0000000..b859fa9 --- /dev/null +++ b/_putchar.c @@ -0,0 +1,12 @@ +#include + +/** + * _putchar - writes the character c to stdout + * @c: The character to print + * Return: 1 on SUCCESS, + * -1 on error and errno is set appropriately. + */ +int _putchar(char c) +{ + return (write(1, &c, 1)); +} From 0ddb772a8ad79633428e0b741925582edf7fa4e9 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 17:45:37 +0000 Subject: [PATCH 03/11] example test file --- printf_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 printf_tests.c diff --git a/printf_tests.c b/printf_tests.c new file mode 100644 index 0000000..abe6158 --- /dev/null +++ b/printf_tests.c @@ -0,0 +1,44 @@ +#include +#include +#include "holberton.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int len; + int len2; + unsigned int ui; + void *addr; + + len = _printf("Let's try to printf a simple sentence.\n"); + len2 = printf("Let's try to printf a simple sentence.\n"); + ui = (unsigned int)INT_MAX + 1024; + addr = (void *)0x7ffe637541f0; + _printf("Length:[%d, %i]\n", len, len); + printf("Length:[%d, %i]\n", len2, len2); + _printf("Negative:[%d]\n", -762534); + printf("Negative:[%d]\n", -762534); + _printf("Unsigned:[%u]\n", ui); + printf("Unsigned:[%u]\n", ui); + _printf("Unsigned octal:[%o]\n", ui); + printf("Unsigned octal:[%o]\n", ui); + _printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + _printf("Character:[%c]\n", 'H'); + printf("Character:[%c]\n", 'H'); + _printf("String:[%s]\n", "I am a string !"); + printf("String:[%s]\n", "I am a string !"); + _printf("Address:[%p]\n", addr); + printf("Address:[%p]\n", addr); + len = _printf("Percent:[%%]\n"); + len2 = printf("Percent:[%%]\n"); + _printf("Len:[%d]\n", len); + printf("Len:[%d]\n", len2); + _printf("Unknown:[%r]\n"); + printf("Unknown:[%r]\n"); + return (0); +} From 2fbf33b6ea1037133008b45418488d6f2b1430c6 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 17:46:42 +0000 Subject: [PATCH 04/11] delete files --- printf_tests.c | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 printf_tests.c diff --git a/printf_tests.c b/printf_tests.c deleted file mode 100644 index abe6158..0000000 --- a/printf_tests.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include "holberton.h" - -/** - * main - Entry point - * - * Return: Always 0 - */ -int main(void) -{ - int len; - int len2; - unsigned int ui; - void *addr; - - len = _printf("Let's try to printf a simple sentence.\n"); - len2 = printf("Let's try to printf a simple sentence.\n"); - ui = (unsigned int)INT_MAX + 1024; - addr = (void *)0x7ffe637541f0; - _printf("Length:[%d, %i]\n", len, len); - printf("Length:[%d, %i]\n", len2, len2); - _printf("Negative:[%d]\n", -762534); - printf("Negative:[%d]\n", -762534); - _printf("Unsigned:[%u]\n", ui); - printf("Unsigned:[%u]\n", ui); - _printf("Unsigned octal:[%o]\n", ui); - printf("Unsigned octal:[%o]\n", ui); - _printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); - printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); - _printf("Character:[%c]\n", 'H'); - printf("Character:[%c]\n", 'H'); - _printf("String:[%s]\n", "I am a string !"); - printf("String:[%s]\n", "I am a string !"); - _printf("Address:[%p]\n", addr); - printf("Address:[%p]\n", addr); - len = _printf("Percent:[%%]\n"); - len2 = printf("Percent:[%%]\n"); - _printf("Len:[%d]\n", len); - printf("Len:[%d]\n", len2); - _printf("Unknown:[%r]\n"); - printf("Unknown:[%r]\n"); - return (0); -} From 0b8553620651ad868718b8cd92a3bf3b54522008 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 17:48:52 +0000 Subject: [PATCH 05/11] printf test file --- printf_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 printf_tests.c diff --git a/printf_tests.c b/printf_tests.c new file mode 100644 index 0000000..abe6158 --- /dev/null +++ b/printf_tests.c @@ -0,0 +1,44 @@ +#include +#include +#include "holberton.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int len; + int len2; + unsigned int ui; + void *addr; + + len = _printf("Let's try to printf a simple sentence.\n"); + len2 = printf("Let's try to printf a simple sentence.\n"); + ui = (unsigned int)INT_MAX + 1024; + addr = (void *)0x7ffe637541f0; + _printf("Length:[%d, %i]\n", len, len); + printf("Length:[%d, %i]\n", len2, len2); + _printf("Negative:[%d]\n", -762534); + printf("Negative:[%d]\n", -762534); + _printf("Unsigned:[%u]\n", ui); + printf("Unsigned:[%u]\n", ui); + _printf("Unsigned octal:[%o]\n", ui); + printf("Unsigned octal:[%o]\n", ui); + _printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + _printf("Character:[%c]\n", 'H'); + printf("Character:[%c]\n", 'H'); + _printf("String:[%s]\n", "I am a string !"); + printf("String:[%s]\n", "I am a string !"); + _printf("Address:[%p]\n", addr); + printf("Address:[%p]\n", addr); + len = _printf("Percent:[%%]\n"); + len2 = printf("Percent:[%%]\n"); + _printf("Len:[%d]\n", len); + printf("Len:[%d]\n", len2); + _printf("Unknown:[%r]\n"); + printf("Unknown:[%r]\n"); + return (0); +} From c69f0b8b3ff8800adfd5bbbaa84e0d49a4c34395 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Sun, 21 Oct 2018 19:21:27 +0000 Subject: [PATCH 06/11] Moved printf_tests.c to a tests subfolder. --- tests/printf_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/printf_tests.c diff --git a/tests/printf_tests.c b/tests/printf_tests.c new file mode 100644 index 0000000..abe6158 --- /dev/null +++ b/tests/printf_tests.c @@ -0,0 +1,44 @@ +#include +#include +#include "holberton.h" + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int len; + int len2; + unsigned int ui; + void *addr; + + len = _printf("Let's try to printf a simple sentence.\n"); + len2 = printf("Let's try to printf a simple sentence.\n"); + ui = (unsigned int)INT_MAX + 1024; + addr = (void *)0x7ffe637541f0; + _printf("Length:[%d, %i]\n", len, len); + printf("Length:[%d, %i]\n", len2, len2); + _printf("Negative:[%d]\n", -762534); + printf("Negative:[%d]\n", -762534); + _printf("Unsigned:[%u]\n", ui); + printf("Unsigned:[%u]\n", ui); + _printf("Unsigned octal:[%o]\n", ui); + printf("Unsigned octal:[%o]\n", ui); + _printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); + _printf("Character:[%c]\n", 'H'); + printf("Character:[%c]\n", 'H'); + _printf("String:[%s]\n", "I am a string !"); + printf("String:[%s]\n", "I am a string !"); + _printf("Address:[%p]\n", addr); + printf("Address:[%p]\n", addr); + len = _printf("Percent:[%%]\n"); + len2 = printf("Percent:[%%]\n"); + _printf("Len:[%d]\n", len); + printf("Len:[%d]\n", len2); + _printf("Unknown:[%r]\n"); + printf("Unknown:[%r]\n"); + return (0); +} From 65d6142946442b47b46efe5be41705b420994be3 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 19:36:51 +0000 Subject: [PATCH 07/11] delete files --- printf_tests.c | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 printf_tests.c diff --git a/printf_tests.c b/printf_tests.c deleted file mode 100644 index abe6158..0000000 --- a/printf_tests.c +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include "holberton.h" - -/** - * main - Entry point - * - * Return: Always 0 - */ -int main(void) -{ - int len; - int len2; - unsigned int ui; - void *addr; - - len = _printf("Let's try to printf a simple sentence.\n"); - len2 = printf("Let's try to printf a simple sentence.\n"); - ui = (unsigned int)INT_MAX + 1024; - addr = (void *)0x7ffe637541f0; - _printf("Length:[%d, %i]\n", len, len); - printf("Length:[%d, %i]\n", len2, len2); - _printf("Negative:[%d]\n", -762534); - printf("Negative:[%d]\n", -762534); - _printf("Unsigned:[%u]\n", ui); - printf("Unsigned:[%u]\n", ui); - _printf("Unsigned octal:[%o]\n", ui); - printf("Unsigned octal:[%o]\n", ui); - _printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); - printf("Unsigned hexadecimal:[%x, %X]\n", ui, ui); - _printf("Character:[%c]\n", 'H'); - printf("Character:[%c]\n", 'H'); - _printf("String:[%s]\n", "I am a string !"); - printf("String:[%s]\n", "I am a string !"); - _printf("Address:[%p]\n", addr); - printf("Address:[%p]\n", addr); - len = _printf("Percent:[%%]\n"); - len2 = printf("Percent:[%%]\n"); - _printf("Len:[%d]\n", len); - printf("Len:[%d]\n", len2); - _printf("Unknown:[%r]\n"); - printf("Unknown:[%r]\n"); - return (0); -} From e46d70766df059c37534a02065021ca2a0f79953 Mon Sep 17 00:00:00 2001 From: stefansilverio <494@holbertonschool.com> Date: Sun, 21 Oct 2018 22:02:32 +0000 Subject: [PATCH 08/11] new headerfile --- holberton.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 holberton.h diff --git a/holberton.h b/holberton.h new file mode 100644 index 0000000..22d9c78 --- /dev/null +++ b/holberton.h @@ -0,0 +1,6 @@ +#ifndef __HOLBERTON_H__ +#define __HOLBERTON_H__ + +int _putchar(char c); + +#endif From 3965ea7031e41270d89e72a29c8085c765621c7e Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 16:22:40 +0000 Subject: [PATCH 09/11] Adds printer struct and closes #7 --- holberton.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/holberton.h b/holberton.h index 22d9c78..5d751f6 100644 --- a/holberton.h +++ b/holberton.h @@ -1,6 +1,18 @@ #ifndef __HOLBERTON_H__ #define __HOLBERTON_H__ +/** + * struct printer - format printer struct + * @spec: the format specifier + * @fn: the function that handles spec + */ +typedef struct printer +{ + char *spec; + int (*fn) (va_list); +} print_t; + + int _putchar(char c); #endif From 63aa057cccfb38b9c778fe2a320ca8be595e18ab Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 17:55:16 +0000 Subject: [PATCH 10/11] Closes #6 and #8. Includes small test in main function. --- printf.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 printf.c diff --git a/printf.c b/printf.c new file mode 100644 index 0000000..a4117c2 --- /dev/null +++ b/printf.c @@ -0,0 +1,102 @@ +#include /* for testing only */ +#include /* va_list, va_start, va_arg, va_end macros */ +#include /* NULL macro */ +#include "holberton.h" /* _putchar */ + +/** + * print_char - prints a char parameter from a va_list + * @ap: va_list from calling function + * Return: integer count of characters printed + */ +int print_char(va_list ap) +{ + return (_putchar(va_arg(ap, int))); +} + +/** + * print_string - prints a string parameter from a va_list + * @ap: va_list from calling function + * Return: integer count of characters printed + */ +int print_string(va_list ap) +{ + char *str = va_arg(ap, char *); + int count = 0; + + while (str[count] != '\0') + count += _putchar(str[count]); + + return (count); +} + + +/** + * _printf - prints to stdout according to a format string + * @format: constant string containing zero or more directives + * Return: int number of characters printed (excluding terminating null-byte) + */ +int _printf(const char *format, ...) +{ + int i, j, count = 0; + va_list ap; + print_t funcs[] = { + {"c", print_char}, + {"s", print_string}, + {NULL, NULL} + }; + + va_start(ap, format); + for (i = 0; format != NULL && format[i] != '\0'; i++) + { + if (format[i] != '%') + { + count += _putchar(format[i]); + continue; + } + switch (format[++i]) + { + case '%': + count += _putchar('%'); + break; + case 'c': + case 's': + for (j = 0; funcs[j].spec != NULL; j++) + { + if (format[i] == funcs[j].spec[0]) + { + count += funcs[j].fn(ap); + break; + } + } + break; + default: + exit(-1); + } + } + va_end(ap); + return (count); +} + +/* TODO: REMOVE BEFORE PUSHING TO MASTER! */ +/** + * main - tests _printf against stdio::printf + * Return: 0 on SUCCESS + */ +int main(void) +{ + int a, b; + + a = printf("a\n"); + b = _printf("a\n"); + printf("(%d, %d)\n", a, b); + + a = printf("%c\n", 'A'); + b = _printf("%c\n", 'A'); + printf("(%d, %d)\n", a, b); + + a = printf("%s\n", "Holberton"); + b = _printf("%s\n", "Holberton"); + printf("(%d, %d)\n", a, b); + + return (0); +} From dd7eb9d8f8b8b7ba56a6b3f7e64f57a1419b064c Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez-Morales <542@holbertonschool.com> Date: Mon, 22 Oct 2018 21:40:42 +0000 Subject: [PATCH 11/11] Closes #14. Ready for check 0 --- holberton.h | 1 + printf.c | 24 ------------------------ tests/small_tests.c | 25 +++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 tests/small_tests.c diff --git a/holberton.h b/holberton.h index 5d751f6..eafd1e6 100644 --- a/holberton.h +++ b/holberton.h @@ -14,5 +14,6 @@ typedef struct printer int _putchar(char c); +int _printf(const char *format, ...); #endif diff --git a/printf.c b/printf.c index a4117c2..fe4b8d3 100644 --- a/printf.c +++ b/printf.c @@ -1,4 +1,3 @@ -#include /* for testing only */ #include /* va_list, va_start, va_arg, va_end macros */ #include /* NULL macro */ #include "holberton.h" /* _putchar */ @@ -77,26 +76,3 @@ int _printf(const char *format, ...) return (count); } -/* TODO: REMOVE BEFORE PUSHING TO MASTER! */ -/** - * main - tests _printf against stdio::printf - * Return: 0 on SUCCESS - */ -int main(void) -{ - int a, b; - - a = printf("a\n"); - b = _printf("a\n"); - printf("(%d, %d)\n", a, b); - - a = printf("%c\n", 'A'); - b = _printf("%c\n", 'A'); - printf("(%d, %d)\n", a, b); - - a = printf("%s\n", "Holberton"); - b = _printf("%s\n", "Holberton"); - printf("(%d, %d)\n", a, b); - - return (0); -} diff --git a/tests/small_tests.c b/tests/small_tests.c new file mode 100644 index 0000000..c0e1ff8 --- /dev/null +++ b/tests/small_tests.c @@ -0,0 +1,25 @@ +#include +#include "../holberton.h" + +/** + * main - tests _printf against stdio::printf + * Return: 0 on SUCCESS + */ +int main(void) +{ + int a, b; + + a = printf("a\n"); + b = _printf("a\n"); + printf("(%d, %d)\n", a, b); + + a = printf("%c\n", 'A'); + b = _printf("%c\n", 'A'); + printf("(%d, %d)\n", a, b); + + a = printf("%s\n", "Holberton"); + b = _printf("%s\n", "Holberton"); + printf("(%d, %d)\n", a, b); + + return (0); +}