diff --git a/man_3_printf b/man_3_printf new file mode 100644 index 0000000..db253d3 --- /dev/null +++ b/man_3_printf @@ -0,0 +1,65 @@ +.\" Manpage for _printf. +.TH man 8 "23 October 2018" "1.0" "_printf man page" +.SH NAME +_printf - format and write a string, character, or integer to stdout +.SH SYNOPSIS +#include "holberton.h" +.br +#include +.br +#include +.br +.br +int _printf(const char *format, ...) + +.SH DESCRIPTION +The function _printf() produces output according to a format as described below. The functions printf() writes output to stdout, the standard output stream. + +.SS Return value +Upon successful return, these functions return the number of characters +printed (excluding the null byte used to end output to strings). + +If an output error is encountered, a negative value is returned. + +.SS The format string: +.br +The format string is a character string, composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream. Each conversion specification is introduced by the character %, and ends with the conversion specifier. + +.SS The conversion specifiers: +.br +A character that specifies the type of conversion to be applied. The conversion specifier and their meanings are: +.br +d, i - The int argument is converted to signed decimal notation. +.br +c - The int argument is converted to an unsigned char, and the resulting character is written. +.br +s - The const char * argument is expected to be a pointer to an array of character type (pointer to a string). + +.SH EXAMPLE +.br +Print a string literal: +.br +int a = 0; +.br +char *a_string = "cat"; +.br +a = _printf("test %s\n", a_string); +.br +printf("%d\n", a); +.br +return (0); +.br +.SS General Formatting: +.br +_printf("example format string [ % [ (c) (s) (d) (i) ]]", [variable] [string literal) +.br +.SH SEE ALSO +printf(1), asprintf(3), dprintf(3), scanf(3) +.br +.SH BUGS +No known bugs. +.SS COLOPHON +.br +This page was produced is collaboration with Roc-A-Fella Records. A description of the project can be found at jada jada jada. +.SH AUTHOR +.B Fernando Gonzalez-Morales and Stef Silverio diff --git a/printf.1 b/printf.1 new file mode 100644 index 0000000..39870c0 --- /dev/null +++ b/printf.1 @@ -0,0 +1,32 @@ +.TH PRINTF 1 +.SH NAME +printf \- display characters, strings, and integers on stdout +.SH SYNOPSIS +.B printf +[OPTIONS] +.IR file +[%c characters] [%s strings] [%i or %d integers] +.LP +.B printf +[OPTIONS] +.IR file +[%c characters] [%s strings] [%i or %d integers] +.SH DESCRIPTION +.B printf +printf writes data entered by user to standard output +.br +.SH 1 \- PRINTF +.TP +.BR Example +.br +.SAMPLE + + + +.SH OPTIONS +.TP +.BR \ %s ", " \ %c ", " \ %i ", " \ %d +"s" indicates that user will enter a string. +"c" indicates that user will enter a character. +"i" indicates that user will enter a integer. +"d" indicates that user will enter a interger. diff --git a/printf.c b/printf.c index 7a9fa70..218107a 100644 --- a/printf.c +++ b/printf.c @@ -40,13 +40,11 @@ int _printf(const char *format, ...) count += call_print_fn(format[i], funcs, ap); break; default: - if (format[i] >= 7 && format[i] <= 13) - { - count += _putchar('%'); - count += _putchar(format[i]); - break; - } - return (-1); + if (!format[i]) + return (-1); + count += _putchar('%'); + count += _putchar(format[i]); + break; } } va_end(ap); diff --git a/tests/ms.c b/tests/ms.c new file mode 100755 index 0000000..c086df5 --- /dev/null +++ b/tests/ms.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include "../holberton.h" +#include + +/** + * main - Entry point + * + * Return: Always 0 + */ +int main(void) +{ + int i = 0, j = 0; + char *p = "hello"; /* *l_str = "fTbhfUxWnHIo7E6JeIu8bYeu5Vs3Su4Wlxn4h8D5E1mO7Gb2pPssTQZ3tYYCYct0FtcucSjeG77krmZOITQJ4J8jq8KGSBIPbtIqubYkHPcq2wrM1Ompy5VFlUuIFVnIP7nVLXXVFOjnMv2OGGhAqsaOWYOQG3rjbe3CkiolYyHjWtizAfzKev2mm5c00KCBzK5QzSY4bX93S8hjAoQBzaWKY2XBgzxatlhGPrnWyGpJCur0TkNKgvFacE2g0zp4dag51bf9QANmvnexBv0L8ZqZCOAMkLisr6oYhIGc3G7KRzQGeeOFNILu9WHpFEHVGtQfhlQP1yinFldB1lomSP9211QhqJaedSQE3u8hegAXUqAGUZGo0phgCIPqv8LyrPIau883A9OJqrbT57Z5Fbnn60MF1EAgHSplxNREvymZFL4ydYKn0YAW7gwgIzd4pvslkMmQbmzT42g8TSshFCA8Ge2V24bVBsJ9pUH1YlJggx56zvsD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K2WCnV0siNFGq0FMGjNvKAxLbNP8iuESxL5uodi1KjatxgFmvMxr6EO9UElTPUZfUaNM1rvHboHrfQgeCLSj2wDjO3icEwMDzsWBLEVmTaQ30qRQt4sYkbtHN0XplMRGi0Tfgtk6CwLqoqmWH7XdDIAdTwfrA4DITGfu8tiG6pYNGGoCeI10JRP7m9ZHpKi17Yn1F4KxAZgUFNHSjIlcXFcLS9UM1Dswsx2U2gIswGqhA6MLmyQELuxrFjZmPyZN8yGXhITXqjJZOuRhORMVz5JlJf1IjzXqU4BSZBX9z6iA24VRISNYvHbBHrFYllk2ojO9FUwRrlmk5ppQAudWyv1uTibGGjf6bZtced6cIZLCYkaujPLF01qWZnsscf7bcrHUP34AZya1ct90RTaQXTM1FsSlPTBXCF6PJHFd6I7LL8G9WWdWi8wLzNnTTTa4n3mVtwHEx6B1J";*/ + char q = 'H'; + + i = _printf("Let's try to printf a simple sentence.\n"); + j = printf("Let's try to printf a simple sentence.\n"); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf a %c sentence %s.\n", '4', "complex"); + j = printf("Let's try to printf a %c sentence %s.\n", '4', "complex"); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf a %s sentence %c.\n", "simple", '8'); + j = printf("Let's try to printf a %s sentence %c.\n", "simple", '8'); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Hello Moto % %"); + j = printf("Hello Moto % %"); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf a %d sentence.\n", 50); + j = printf("Let's try to printf a %d sentence.\n", 50); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf a %i sentence.\n", 50); + j = printf("Let's try to printf a %i sentence.\n", 50); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf a %i \"\' sentence.\n", 50); + j = printf("Let's try to printf a %i \"\' sentence.\n", 50); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Let's try to printf: a [z[%bgs][%s]x] woof\n more [%c]sentence.\n", p, q); + j = printf("Let's try to printf: a [z[%bgs][%s]x] woof\n more [%c]sentence.\n", p, q); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf(p); + j = printf(p); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Hey there [z %d%s%c x]\n", INT_MIN, p, 'h'); + j = printf("Hey there [z %d%s%c x]\n", INT_MIN, p, 'h'); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Hey there [%i]\n", INT_MAX); + j = printf("Hey there [%i]\n", INT_MAX); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("Hey there [%d]\n", 1 + 1); + j = printf("Hey there [%d]\n", 1 + 1); + _printf("%d %d\n", i, j); + printf("%d %d\n", i, j); + i = _printf("%c%c hi %bc%s - - ***\n", q, q, p); + j = printf("%c%c hi %bc%s - - ***\n", q, q, p); + _printf("%d %d\n", i, j); + printf("%d %d\n", i , j); + i = printf("\n"); + j = _printf("\n"); + printf("%d %d\n", i, j); + _printf("%d %d\n", i , j); + i = printf("abc%cfgh%s\n",q, p); + j = _printf("abc%cfgh%s\n",q, p); + printf("%d %d\n", i, j); + _printf("%d %d\n", i , j); + i = printf("[%%cc] [%sss]\n", p); + j = _printf("[%%cc] [%sss]\n", p); + printf("%d %d\n", i, j); + _printf("%d %d\n", i , j); + i = printf("%%% %s%d%%c %%%% %Ybs\n", p, 10, q, p); + j = _printf("%%% %s%d%%c %%%% %Ybs\n", p, 10, q, p); + printf("%d %d\n", i, j); + _printf("%d %d\n", i , j); +/* i = printf("%s\n", l_str, 10, q, p); + j = _printf("%s\n", l_str, 10, q, p); +*/ printf("%d %d\n", i, j); + _printf("%d %d\n", i , j); + i = printf("%d% %\n", 50); + j = _printf("%d% %\n", 50); + printf("%d %d\n", i, j); + _printf("%d %d\n", i, j); +/* i = printf("%d%d%d\n", printf("%s", l_str), printf("%s", l_str), printf("%s", l_str)); + j = _printf("%d%d%d\n", printf("%s", l_str), printf("%s", l_str), printf("%s", l_str)); + printf("%d %d\n", i, j); + _printf("%d %d\n", i, j); +*/ i = printf("%d%c%d%%d\n", 130, 130, 50); + j = _printf("%d%c%d%%d\n", 130, 130, 50); + printf("%d %d\n", i, j); + _printf("%d %d\n", i, j); + return (0); +} diff --git a/tests/small_tests.c b/tests/small_tests.c index 62300c9..cfd4bf5 100644 --- a/tests/small_tests.c +++ b/tests/small_tests.c @@ -11,17 +11,41 @@ int main(void) int a = 0; int b = 0; - a = printf("stdio %o\n", 0); - b = _printf("ours %o\n", 0); -/* printf("%d", a);*/ + a = _printf("ours %d% %", 42); + b = printf("stdio %d% %", 42); printf("(%d, %d)\n", a, b); -/* a = printf("stdio %c\n", '\0'); - b = _printf("ours %c\n", '\0'); + a = _printf("ours %%% %s%d%%c %%%% %Ybs\n", "hello", 10, 'H', "hello"); + b = printf("stdio %%% %s%d%%c %%%% %Ybs\n", "hello", 10, 'H', "hello"); printf("(%d, %d)\n", a, b); - a = printf("stdio %s\n", "Holberton"); - b = _printf("ours %s\n", "Holberton"); + a = _printf("ours %d% %\n", 50); + b = printf("stdio %d% %\n", 50); + printf("(%d, %d)\n", a, b); + + a = _printf("ours %y%"); + b = printf("stdio %y%"); + printf("(%d, %d)\n", a, b); + + a = _printf("ours % s"); + b = printf("stdio % s"); + printf("(%d, %d)\n", a, b); + + a = _printf("ours % %", 0); + b = printf("stdio % %", 0); + printf("(%d, %d)\n", a, b); + + /* Fixed! */ +/* a = _printf("ours a [z[%bgs][%s]x] woof \n more [%c]sentence.\n", "hello", 'H'); + b = printf("stdio a [z[%bgs][%s]x] woof \n more [%c]sentence.\n", "hello", 'H'); + printf("(%d, %d)\n", a, b); + + a = _printf("ours %c%c hi %bc%s - - ***\n", 'H', 'H', "hello"); + b = printf("stdio %c%c hi %bc%s - - ***\n", 'H', 'H', "hello"); + printf("(%d, %d)\n", a, b); + + a = _printf("ours %w"); + b = printf("stdio %w"); printf("(%d, %d)\n", a, b); */ return (0);