├── .gitattributes ├── print_buf.c ├── print_prg.c ├── print_chr.c ├── handl_buf.c ├── print_str.c ├── fill_oct_array.c ├── fill_long_oct_array.c ├── fill_short_oct_array.c ├── print_unt.c ├── print_long_unt.c ├── print_short_unt.c ├── print_rev.c ├── fill_hex_array.c ├── fill_binary_array.c ├── print_plus_int.c ├── print_space_int.c ├── print_int.c ├── print_long_int.c ├── print_short_int.c ├── print_rot.c ├── print_bnr.c ├── print_usr.c ├── print_oct.c ├── print_long_oct.c ├── print_short_oct.c ├── print_hex.c ├── print_num_oct.c ├── print_upx.c ├── print_long_hex.c ├── print_short_hex.c ├── print_long_upx.c ├── print_short_upx.c ├── print_num_upx.c ├── print_num_hex.c ├── _printf.c ├── print_add.c ├── ev_print_func.c ├── get_print_func.c ├── main.h ├── man_3_printf └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /print_buf.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_buf - prints buffer 5 | * @buf: buffer pointer 6 | * @nbuf: number of bytes to print 7 | * Return: number of bytes printed. 8 | */ 9 | int print_buf(char *buf, unsigned int nbuf) 10 | { 11 | return (write(1, buf, nbuf)); 12 | } 13 | -------------------------------------------------------------------------------- /print_prg.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | 4 | /** 5 | * print_prg - writes the character c to stdout 6 | * @a: input char 7 | * @buf: buffer pointer 8 | * @i: index for buffer pointer 9 | * Return: On success 1. 10 | */ 11 | int print_prg(va_list a __attribute__((unused)), char *buf, unsigned int i) 12 | { 13 | handl_buf(buf, '%', i); 14 | 15 | return (1); 16 | } 17 | -------------------------------------------------------------------------------- /print_chr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_chr - writes the character c to stdout 5 | * @arguments: input char 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: On success 1. 9 | */ 10 | int print_chr(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | char c; 13 | 14 | c = va_arg(arguments, int); 15 | handl_buf(buf, c, ibuf); 16 | 17 | return (1); 18 | } 19 | -------------------------------------------------------------------------------- /handl_buf.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * handl_buf - concatenates the buffer characters 5 | * @buf: buffer pointer 6 | * @c: charcter to concatenate 7 | * @ibuf: index of buffer pointer 8 | * Return: index of buffer pointer. 9 | */ 10 | unsigned int handl_buf(char *buf, char c, unsigned int ibuf) 11 | { 12 | if (ibuf == 1024) 13 | { 14 | print_buf(buf, ibuf); 15 | ibuf = 0; 16 | } 17 | buf[ibuf] = c; 18 | ibuf++; 19 | return (ibuf); 20 | } 21 | -------------------------------------------------------------------------------- /print_str.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_str - writes the string to stdout 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: On success 1. 9 | */ 10 | int print_str(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | char *str; 13 | unsigned int i; 14 | char nill[] = "(null)"; 15 | 16 | str = va_arg(arguments, char *); 17 | if (str == NULL) 18 | { 19 | for (i = 0; nill[i]; i++) 20 | ibuf = handl_buf(buf, nill[i], ibuf); 21 | return (6); 22 | } 23 | for (i = 0; str[i]; i++) 24 | ibuf = handl_buf(buf, str[i], ibuf); 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /fill_oct_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * fill_oct_array - writes the character c to stdout 5 | * 6 | * @bnr: array where is stored the binary. 7 | * @oct: array where is stored the octal. 8 | * 9 | * Return: binary array. 10 | */ 11 | char *fill_oct_array(char *bnr, char *oct) 12 | { 13 | int op, i, j, ioct, limit; 14 | 15 | oct[11] = '\0'; 16 | for (i = 31, ioct = 10; i >= 0; i--, ioct--) 17 | { 18 | if (i > 1) 19 | limit = 4; 20 | else 21 | limit = 2; 22 | for (op = 0, j = 1; j <= limit; j *= 2, i--) 23 | op = ((bnr[i] - '0') * j) + op; 24 | i++; 25 | oct[ioct] = op + '0'; 26 | } 27 | return (oct); 28 | } 29 | -------------------------------------------------------------------------------- /fill_long_oct_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * fill_long_oct_array - calculates a long octal number 5 | * 6 | * @bnr: array where is stored the binary. 7 | * @oct: array where is stored the octal. 8 | * 9 | * Return: binary array. 10 | */ 11 | char *fill_long_oct_array(char *bnr, char *oct) 12 | { 13 | int op, i, j, ioct, limit; 14 | 15 | oct[22] = '\0'; 16 | for (i = 63, ioct = 21; i >= 0; i--, ioct--) 17 | { 18 | if (i > 0) 19 | limit = 4; 20 | else 21 | limit = 1; 22 | for (op = 0, j = 1; j <= limit; j *= 2, i--) 23 | op = ((bnr[i] - '0') * j) + op; 24 | i++; 25 | oct[ioct] = op + '0'; 26 | } 27 | return (oct); 28 | } 29 | -------------------------------------------------------------------------------- /fill_short_oct_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * fill_short_oct_array - calculates a short octal number 5 | * 6 | * @bnr: array where is stored the binary. 7 | * @oct: array where is stored the octal. 8 | * 9 | * Return: binary array. 10 | */ 11 | char *fill_short_oct_array(char *bnr, char *oct) 12 | { 13 | int op, i, j, ioct, limit; 14 | 15 | oct[6] = '\0'; 16 | for (i = 15, ioct = 5; i >= 0; i--, ioct--) 17 | { 18 | if (i > 0) 19 | limit = 4; 20 | else 21 | limit = 1; 22 | for (op = 0, j = 1; j <= limit; j *= 2, i--) 23 | op = ((bnr[i] - '0') * j) + op; 24 | i++; 25 | oct[ioct] = op + '0'; 26 | } 27 | return (oct); 28 | } 29 | -------------------------------------------------------------------------------- /print_unt.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_unt - prints an unsigned int 4 | * @arguments: number to print 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed. 8 | */ 9 | int print_unt(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | unsigned int int_in, int_temp, i, div; 12 | 13 | int_in = va_arg(arguments, unsigned int); 14 | int_temp = int_in; 15 | div = 1; 16 | while (int_temp > 9) 17 | { 18 | div *= 10; 19 | int_temp /= 10; 20 | } 21 | for (i = 0; div > 0; div /= 10, i++) 22 | { 23 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 24 | } 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /print_long_unt.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * prinlunt - prints a long unsigned integer 4 | * @arguments: number to print 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed. 8 | */ 9 | int prinlunt(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | unsigned long int int_in, int_temp, i, div; 12 | 13 | int_in = va_arg(arguments, unsigned long int); 14 | int_temp = int_in; 15 | div = 1; 16 | while (int_temp > 9) 17 | { 18 | div *= 10; 19 | int_temp /= 10; 20 | } 21 | for (i = 0; div > 0; div /= 10, i++) 22 | { 23 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 24 | } 25 | return (i); 26 | } 27 | -------------------------------------------------------------------------------- /print_short_unt.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinhunt - prints a short unsigned integer 5 | * @arguments: number to print 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int prinhunt(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | unsigned short int int_in, int_temp, i, div; 13 | 14 | int_in = va_arg(arguments, unsigned int); 15 | 16 | int_temp = int_in; 17 | div = 1; 18 | 19 | while (int_temp > 9) 20 | { 21 | div *= 10; 22 | int_temp /= 10; 23 | } 24 | 25 | for (i = 0; div > 0; div /= 10, i++) 26 | { 27 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 28 | } 29 | return (i); 30 | } 31 | -------------------------------------------------------------------------------- /print_rev.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rev - writes the str in reverse 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int print_rev(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | char *str; 13 | unsigned int i; 14 | int j = 0; 15 | char nill[] = "(llun)"; 16 | 17 | str = va_arg(arguments, char *); 18 | if (str == NULL) 19 | { 20 | for (i = 0; nill[i]; i++) 21 | ibuf = handl_buf(buf, nill[i], ibuf); 22 | return (6); 23 | } 24 | for (i = 0; str[i]; i++) 25 | ; 26 | j = i - 1; 27 | for (; j >= 0; j--) 28 | { 29 | ibuf = handl_buf(buf, str[j], ibuf); 30 | } 31 | return (i); 32 | } 33 | -------------------------------------------------------------------------------- /fill_hex_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * fill_hex_array - writes the character c to stdout 5 | * 6 | * @bnr: array where is stored the binary. 7 | * @hex: array where is stored the hexadecimal. 8 | * @isupp: integer that determines if the hexadecimal array is 9 | * in uppercase or lowercase letter. 10 | * @limit: size of hex 11 | * Return: binary array. 12 | */ 13 | char *fill_hex_array(char *bnr, char *hex, int isupp, int limit) 14 | { 15 | int op, i, j, toletter; 16 | 17 | hex[limit] = '\0'; 18 | if (isupp) 19 | toletter = 55; 20 | else 21 | toletter = 87; 22 | for (i = (limit * 4) - 1; i >= 0; i--) 23 | { 24 | for (op = 0, j = 1; j <= 8; j *= 2, i--) 25 | op = ((bnr[i] - '0') * j) + op; 26 | i++; 27 | if (op < 10) 28 | hex[i / 4] = op + 48; 29 | else 30 | hex[i / 4] = op + toletter; 31 | } 32 | return (hex); 33 | } 34 | -------------------------------------------------------------------------------- /fill_binary_array.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * fill_binary_array - prints decimal in binary 5 | * @binary: pointer to binary 6 | * @int_in: input number 7 | * @isneg: if input number is negative 8 | * @limit: size of the binary 9 | * Return: number of chars printed. 10 | */ 11 | char *fill_binary_array(char *binary, long int int_in, int isneg, int limit) 12 | { 13 | int i; 14 | 15 | for (i = 0; i < limit; i++) 16 | binary[i] = '0'; 17 | binary[limit] = '\0'; 18 | for (i = limit - 1; int_in > 1; i--) 19 | { 20 | if (int_in == 2) 21 | binary[i] = '0'; 22 | else 23 | binary[i] = (int_in % 2) + '0'; 24 | int_in /= 2; 25 | } 26 | if (int_in != 0) 27 | binary[i] = '1'; 28 | if (isneg) 29 | { 30 | for (i = 0; binary[i]; i++) 31 | if (binary[i] == '0') 32 | binary[i] = '1'; 33 | else 34 | binary[i] = '0'; 35 | } 36 | return (binary); 37 | } 38 | -------------------------------------------------------------------------------- /print_plus_int.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinpint - print integer with plus symbol 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinpint(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input; 13 | unsigned int int_in, int_temp, i, div; 14 | 15 | int_input = va_arg(arguments, int); 16 | if (int_input < 0) 17 | { 18 | int_in = int_input * -1; 19 | ibuf = handl_buf(buf, '-', ibuf); 20 | } 21 | else 22 | { 23 | int_in = int_input; 24 | ibuf = handl_buf(buf, '+', ibuf); 25 | } 26 | int_temp = int_in; 27 | div = 1; 28 | while (int_temp > 9) 29 | { 30 | div *= 10; 31 | int_temp /= 10; 32 | } 33 | for (i = 0; div > 0; div /= 10, i++) 34 | { 35 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 36 | } 37 | return (i + 1); 38 | } 39 | -------------------------------------------------------------------------------- /print_space_int.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinsint - prints int begining with space 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinsint(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input; 13 | unsigned int int_in, int_temp, i, div; 14 | 15 | int_input = va_arg(arguments, int); 16 | if (int_input < 0) 17 | { 18 | int_in = int_input * -1; 19 | ibuf = handl_buf(buf, '-', ibuf); 20 | } 21 | else 22 | { 23 | int_in = int_input; 24 | ibuf = handl_buf(buf, ' ', ibuf); 25 | } 26 | int_temp = int_in; 27 | div = 1; 28 | while (int_temp > 9) 29 | { 30 | div *= 10; 31 | int_temp /= 10; 32 | } 33 | for (i = 0; div > 0; div /= 10, i++) 34 | { 35 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 36 | } 37 | return (i + 1); 38 | } 39 | -------------------------------------------------------------------------------- /print_int.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_int - prints an integer 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int print_int(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input; 13 | unsigned int int_in, int_temp, i, div, isneg; 14 | 15 | int_input = va_arg(arguments, int); 16 | isneg = 0; 17 | if (int_input < 0) 18 | { 19 | int_in = int_input * -1; 20 | ibuf = handl_buf(buf, '-', ibuf); 21 | isneg = 1; 22 | } 23 | else 24 | { 25 | int_in = int_input; 26 | } 27 | 28 | int_temp = int_in; 29 | div = 1; 30 | 31 | while (int_temp > 9) 32 | { 33 | div *= 10; 34 | int_temp /= 10; 35 | } 36 | 37 | for (i = 0; div > 0; div /= 10, i++) 38 | { 39 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 40 | } 41 | return (i + isneg); 42 | } 43 | -------------------------------------------------------------------------------- /print_long_int.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * prinlint - prints a long integer 4 | * @arguments: input string 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed. 8 | */ 9 | int prinlint(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | long int int_input; 12 | unsigned long int int_in, int_temp, i, div, isneg; 13 | 14 | int_input = va_arg(arguments, long int); 15 | isneg = 0; 16 | if (int_input < 0) 17 | { 18 | int_in = int_input * -1; 19 | ibuf = handl_buf(buf, '-', ibuf); 20 | isneg = 1; 21 | } 22 | else 23 | { 24 | int_in = int_input; 25 | } 26 | 27 | int_temp = int_in; 28 | div = 1; 29 | while (int_temp > 9) 30 | { 31 | div *= 10; 32 | int_temp /= 10; 33 | } 34 | for (i = 0; div > 0; div /= 10, i++) 35 | { 36 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 37 | } 38 | return (i + isneg); 39 | } 40 | -------------------------------------------------------------------------------- /print_short_int.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinhint - prints a short integer 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int prinhint(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | short int int_input; 13 | unsigned short int int_in, int_temp, i, div, isneg; 14 | 15 | int_input = va_arg(arguments, int); 16 | isneg = 0; 17 | if (int_input < 0) 18 | { 19 | int_in = int_input * -1; 20 | ibuf = handl_buf(buf, '-', ibuf); 21 | isneg = 1; 22 | } 23 | else 24 | { 25 | int_in = int_input; 26 | } 27 | int_temp = int_in; 28 | div = 1; 29 | while (int_temp > 9) 30 | { 31 | div *= 10; 32 | int_temp /= 10; 33 | } 34 | for (i = 0; div > 0; div /= 10, i++) 35 | { 36 | ibuf = handl_buf(buf, ((int_in / div) % 10) + '0', ibuf); 37 | } 38 | return (i + isneg); 39 | } 40 | -------------------------------------------------------------------------------- /print_rot.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_rot - writes the str in ROT13 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | 11 | int print_rot(va_list arguments, char *buf, unsigned int ibuf) 12 | { 13 | char alf[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 14 | char rot[] = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"; 15 | char *str; 16 | unsigned int i, j, k; 17 | char nill[] = "(avyy)"; 18 | 19 | str = va_arg(arguments, char *); 20 | if (str == NULL) 21 | { 22 | for (i = 0; nill[i]; i++) 23 | ibuf = handl_buf(buf, nill[i], ibuf); 24 | return (6); 25 | } 26 | for (i = 0; str[i]; i++) 27 | { 28 | for (k = j = 0; alf[j]; j++) 29 | { 30 | if (str[i] == alf[j]) 31 | { 32 | k = 1; 33 | ibuf = handl_buf(buf, rot[j], ibuf); 34 | break; 35 | } 36 | } 37 | if (k == 0) 38 | ibuf = handl_buf(buf, str[i], ibuf); 39 | } 40 | return (i); 41 | } 42 | -------------------------------------------------------------------------------- /print_bnr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_bnr - prints decimal in binary 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int print_bnr(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, count, i, first_one, isnegative; 13 | char *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | binary = malloc(sizeof(char) * (32 + 1)); 28 | binary = fill_binary_array(binary, int_input, isnegative, 32); 29 | first_one = 0; 30 | for (count = i = 0; binary[i]; i++) 31 | { 32 | if (first_one == 0 && binary[i] == '1') 33 | first_one = 1; 34 | if (first_one == 1) 35 | { 36 | ibuf = handl_buf(buf, binary[i], ibuf); 37 | count++; 38 | } 39 | } 40 | free(binary); 41 | return (count); 42 | } 43 | -------------------------------------------------------------------------------- /print_usr.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_usr - prints a string and values of 5 | * non-printed chars 6 | * @arguments: input string 7 | * @buf: buffer pointer 8 | * @ibuf: index for buffer pointer 9 | * Return: number of chars printed 10 | */ 11 | int print_usr(va_list arguments, char *buf, unsigned int ibuf) 12 | { 13 | unsigned char *str; 14 | char *hexadecimal, *binary; 15 | unsigned int i, sum, op; 16 | 17 | str = va_arg(arguments, unsigned char *); 18 | binary = malloc(sizeof(char) * (32 + 1)); 19 | hexadecimal = malloc(sizeof(char) * (8 + 1)); 20 | for (sum = i = 0; str[i]; i++) 21 | { 22 | if (str[i] < 32 || str[i] >= 127) 23 | { 24 | ibuf = handl_buf(buf, '\\', ibuf); 25 | ibuf = handl_buf(buf, 'x', ibuf); 26 | op = str[i]; 27 | binary = fill_binary_array(binary, op, 0, 32); 28 | hexadecimal = fill_hex_array(binary, hexadecimal, 1, 8); 29 | ibuf = handl_buf(buf, hexadecimal[6], ibuf); 30 | ibuf = handl_buf(buf, hexadecimal[7], ibuf); 31 | sum += 3; 32 | } 33 | else 34 | ibuf = handl_buf(buf, str[i], ibuf); 35 | } 36 | free(binary); 37 | free(hexadecimal); 38 | return (i + sum); 39 | } 40 | -------------------------------------------------------------------------------- /print_oct.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_oct - prints decimal number in octal 5 | * @arguments: input number 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int print_oct(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, i, isnegative, count, first_digit; 13 | char *octal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | binary = malloc(sizeof(char) * (32 + 1)); 28 | binary = fill_binary_array(binary, int_input, isnegative, 32); 29 | octal = malloc(sizeof(char) * (11 + 1)); 30 | octal = fill_oct_array(binary, octal); 31 | for (first_digit = i = count = 0; octal[i]; i++) 32 | { 33 | if (octal[i] != '0' && first_digit == 0) 34 | first_digit = 1; 35 | if (first_digit) 36 | { 37 | ibuf = handl_buf(buf, octal[i], ibuf); 38 | count++; 39 | } 40 | } 41 | free(binary); 42 | free(octal); 43 | return (count); 44 | } 45 | -------------------------------------------------------------------------------- /print_long_oct.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * prinloct - prints long decimal number in octal 4 | * @arguments: input number 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed. 8 | */ 9 | int prinloct(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | long int int_input, i, isnegative, count, first_digit; 12 | char *octal, *binary; 13 | 14 | int_input = va_arg(arguments, long int); 15 | isnegative = 0; 16 | if (int_input == 0) 17 | { 18 | ibuf = handl_buf(buf, '0', ibuf); 19 | return (1); 20 | } 21 | if (int_input < 0) 22 | { 23 | int_input = (int_input * -1) - 1; 24 | isnegative = 1; 25 | } 26 | 27 | binary = malloc(sizeof(char) * (64 + 1)); 28 | binary = fill_binary_array(binary, int_input, isnegative, 64); 29 | octal = malloc(sizeof(char) * (22 + 1)); 30 | octal = fill_long_oct_array(binary, octal); 31 | for (first_digit = i = count = 0; octal[i]; i++) 32 | { 33 | if (octal[i] != '0' && first_digit == 0) 34 | first_digit = 1; 35 | if (first_digit) 36 | { 37 | ibuf = handl_buf(buf, octal[i], ibuf); 38 | count++; 39 | } 40 | } 41 | free(binary); 42 | free(octal); 43 | return (count); 44 | } 45 | -------------------------------------------------------------------------------- /print_short_oct.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinhoct - prints long decimal number in octal 5 | * @arguments: input number 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed. 9 | */ 10 | int prinhoct(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | short int int_input, i, isnegative, count, first_digit; 13 | char *octal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | 28 | binary = malloc(sizeof(char) * (16 + 1)); 29 | binary = fill_binary_array(binary, int_input, isnegative, 16); 30 | octal = malloc(sizeof(char) * (6 + 1)); 31 | octal = fill_short_oct_array(binary, octal); 32 | for (first_digit = i = count = 0; octal[i]; i++) 33 | { 34 | if (octal[i] != '0' && first_digit == 0) 35 | first_digit = 1; 36 | if (first_digit) 37 | { 38 | ibuf = handl_buf(buf, octal[i], ibuf); 39 | count++; 40 | } 41 | } 42 | free(binary); 43 | free(octal); 44 | return (count); 45 | } 46 | -------------------------------------------------------------------------------- /print_hex.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * print_hex - prints a decimal in hexadecimal 4 | * @arguments: input string 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed 8 | */ 9 | int print_hex(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | int int_input, i, isnegative, count, first_digit; 12 | char *hexadecimal, *binary; 13 | 14 | int_input = va_arg(arguments, int); 15 | isnegative = 0; 16 | if (int_input == 0) 17 | { 18 | ibuf = handl_buf(buf, '0', ibuf); 19 | return (1); 20 | } 21 | if (int_input < 0) 22 | { 23 | int_input = (int_input * -1) - 1; 24 | isnegative = 1; 25 | } 26 | binary = malloc(sizeof(char) * (32 + 1)); 27 | binary = fill_binary_array(binary, int_input, isnegative, 32); 28 | hexadecimal = malloc(sizeof(char) * (8 + 1)); 29 | hexadecimal = fill_hex_array(binary, hexadecimal, 0, 8); 30 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 31 | { 32 | if (hexadecimal[i] != '0' && first_digit == 0) 33 | first_digit = 1; 34 | if (first_digit) 35 | { 36 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 37 | count++; 38 | } 39 | } 40 | free(binary); 41 | free(hexadecimal); 42 | return (count); 43 | } 44 | -------------------------------------------------------------------------------- /print_num_oct.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinnoct - print the number in octal begining with zero 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinnoct(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, i, isnegative, count, first_digit; 13 | char *octal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | ibuf = handl_buf(buf, '0', ibuf); 28 | binary = malloc(sizeof(char) * (32 + 1)); 29 | binary = fill_binary_array(binary, int_input, isnegative, 32); 30 | octal = malloc(sizeof(char) * (11 + 1)); 31 | octal = fill_oct_array(binary, octal); 32 | for (first_digit = i = count = 0; octal[i]; i++) 33 | { 34 | if (octal[i] != '0' && first_digit == 0) 35 | first_digit = 1; 36 | if (first_digit) 37 | { 38 | ibuf = handl_buf(buf, octal[i], ibuf); 39 | count++; 40 | } 41 | } 42 | free(binary); 43 | free(octal); 44 | return (count + 1); 45 | } 46 | -------------------------------------------------------------------------------- /print_upx.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * print_upx - prints a decimal in hexadecimal 5 | * @arguments: The character to print 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int print_upx(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | binary = malloc(sizeof(char) * (32 + 1)); 28 | binary = fill_binary_array(binary, int_input, isnegative, 32); 29 | hexadecimal = malloc(sizeof(char) * (8 + 1)); 30 | hexadecimal = fill_hex_array(binary, hexadecimal, 1, 8); 31 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 32 | { 33 | if (hexadecimal[i] != '0' && first_digit == 0) 34 | first_digit = 1; 35 | if (first_digit) 36 | { 37 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 38 | count++; 39 | } 40 | } 41 | free(binary); 42 | free(hexadecimal); 43 | return (count); 44 | } 45 | -------------------------------------------------------------------------------- /print_long_hex.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * prinlhex - prints a long decimal in hexadecimal 4 | * @arguments: input string 5 | * @buf: buffer pointer 6 | * @ibuf: index for buffer pointer 7 | * Return: number of chars printed 8 | */ 9 | int prinlhex(va_list arguments, char *buf, unsigned int ibuf) 10 | { 11 | long int int_input, i, isnegative, count, first_digit; 12 | char *hexadecimal, *binary; 13 | 14 | int_input = va_arg(arguments, long int); 15 | isnegative = 0; 16 | if (int_input == 0) 17 | { 18 | ibuf = handl_buf(buf, '0', ibuf); 19 | return (1); 20 | } 21 | if (int_input < 0) 22 | { 23 | int_input = (int_input * -1) - 1; 24 | isnegative = 1; 25 | } 26 | 27 | binary = malloc(sizeof(char) * (64 + 1)); 28 | binary = fill_binary_array(binary, int_input, isnegative, 64); 29 | hexadecimal = malloc(sizeof(char) * (16 + 1)); 30 | hexadecimal = fill_hex_array(binary, hexadecimal, 0, 16); 31 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 32 | { 33 | if (hexadecimal[i] != '0' && first_digit == 0) 34 | first_digit = 1; 35 | if (first_digit) 36 | { 37 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 38 | count++; 39 | } 40 | } 41 | free(binary); 42 | free(hexadecimal); 43 | return (count); 44 | } 45 | -------------------------------------------------------------------------------- /print_short_hex.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinhhex - prints a short decimal in hexadecimal 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinhhex(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | short int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | 28 | binary = malloc(sizeof(char) * (16 + 1)); 29 | binary = fill_binary_array(binary, int_input, isnegative, 16); 30 | hexadecimal = malloc(sizeof(char) * (4 + 1)); 31 | hexadecimal = fill_hex_array(binary, hexadecimal, 0, 4); 32 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 33 | { 34 | if (hexadecimal[i] != '0' && first_digit == 0) 35 | first_digit = 1; 36 | if (first_digit) 37 | { 38 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 39 | count++; 40 | } 41 | } 42 | free(binary); 43 | free(hexadecimal); 44 | return (count); 45 | } 46 | -------------------------------------------------------------------------------- /print_long_upx.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinlupx - prints a long decimal in hexadecimal 5 | * @arguments: The character to print 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinlupx(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | long int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, long int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | 28 | binary = malloc(sizeof(char) * (64 + 1)); 29 | binary = fill_binary_array(binary, int_input, isnegative, 64); 30 | hexadecimal = malloc(sizeof(char) * (16 + 1)); 31 | hexadecimal = fill_hex_array(binary, hexadecimal, 1, 16); 32 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 33 | { 34 | if (hexadecimal[i] != '0' && first_digit == 0) 35 | first_digit = 1; 36 | if (first_digit) 37 | { 38 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 39 | count++; 40 | } 41 | } 42 | free(binary); 43 | free(hexadecimal); 44 | return (count); 45 | } 46 | -------------------------------------------------------------------------------- /print_short_upx.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinhupx - prints a short decimal in hexadecimal 5 | * @arguments: The character to print 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinhupx(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | short int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | 18 | if (int_input == 0) 19 | { 20 | ibuf = handl_buf(buf, '0', ibuf); 21 | return (1); 22 | } 23 | if (int_input < 0) 24 | { 25 | int_input = (int_input * -1) - 1; 26 | isnegative = 1; 27 | } 28 | 29 | binary = malloc(sizeof(char) * (16 + 1)); 30 | binary = fill_binary_array(binary, int_input, isnegative, 16); 31 | hexadecimal = malloc(sizeof(char) * (4 + 1)); 32 | hexadecimal = fill_hex_array(binary, hexadecimal, 1, 4); 33 | 34 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 35 | { 36 | if (hexadecimal[i] != '0' && first_digit == 0) 37 | first_digit = 1; 38 | if (first_digit) 39 | { 40 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 41 | count++; 42 | } 43 | } 44 | 45 | free(binary); 46 | free(hexadecimal); 47 | 48 | return (count); 49 | } 50 | -------------------------------------------------------------------------------- /print_num_upx.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinnupx - prints number in uppercase hex 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinnupx(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | ibuf = handl_buf(buf, '0', ibuf); 28 | ibuf = handl_buf(buf, 'X', ibuf); 29 | binary = malloc(sizeof(char) * (32 + 1)); 30 | binary = fill_binary_array(binary, int_input, isnegative, 32); 31 | hexadecimal = malloc(sizeof(char) * (8 + 1)); 32 | hexadecimal = fill_hex_array(binary, hexadecimal, 1, 8); 33 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 34 | { 35 | if (hexadecimal[i] != '0' && first_digit == 0) 36 | first_digit = 1; 37 | if (first_digit) 38 | { 39 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 40 | count++; 41 | } 42 | } 43 | free(binary); 44 | free(hexadecimal); 45 | return (count + 2); 46 | } 47 | -------------------------------------------------------------------------------- /print_num_hex.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * prinnhex - print number in hex begining with zero 5 | * @arguments: input string 6 | * @buf: buffer pointer 7 | * @ibuf: index for buffer pointer 8 | * Return: number of chars printed 9 | */ 10 | int prinnhex(va_list arguments, char *buf, unsigned int ibuf) 11 | { 12 | int int_input, i, isnegative, count, first_digit; 13 | char *hexadecimal, *binary; 14 | 15 | int_input = va_arg(arguments, int); 16 | isnegative = 0; 17 | if (int_input == 0) 18 | { 19 | ibuf = handl_buf(buf, '0', ibuf); 20 | return (1); 21 | } 22 | if (int_input < 0) 23 | { 24 | int_input = (int_input * -1) - 1; 25 | isnegative = 1; 26 | } 27 | ibuf = handl_buf(buf, '0', ibuf); 28 | ibuf = handl_buf(buf, 'x', ibuf); 29 | binary = malloc(sizeof(char) * (32 + 1)); 30 | binary = fill_binary_array(binary, int_input, isnegative, 32); 31 | hexadecimal = malloc(sizeof(char) * (8 + 1)); 32 | hexadecimal = fill_hex_array(binary, hexadecimal, 0, 8); 33 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 34 | { 35 | if (hexadecimal[i] != '0' && first_digit == 0) 36 | first_digit = 1; 37 | if (first_digit) 38 | { 39 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 40 | count++; 41 | } 42 | } 43 | free(binary); 44 | free(hexadecimal); 45 | return (count + 2); 46 | } 47 | -------------------------------------------------------------------------------- /_printf.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * _printf - formatted output conversion and print data. 5 | * @format: input string. 6 | * 7 | * Return: number of chars printed. 8 | */ 9 | int _printf(const char *format, ...) 10 | { 11 | unsigned int i = 0, len = 0, ibuf = 0; 12 | va_list arguments; 13 | int (*function)(va_list, char *, unsigned int); 14 | char *buffer; 15 | 16 | va_start(arguments, format), buffer = malloc(sizeof(char) * 1024); 17 | if (!format || !buffer || (format[i] == '%' && !format[i + 1])) 18 | return (-1); 19 | if (!format[i]) 20 | return (0); 21 | for (i = 0; format && format[i]; i++) 22 | { 23 | if (format[i] == '%') 24 | { 25 | if (format[i + 1] == '\0') 26 | { print_buf(buffer, ibuf), free(buffer), va_end(arguments); 27 | return (-1); 28 | } 29 | else 30 | { function = get_print_func(format, i + 1); 31 | if (function == NULL) 32 | { 33 | if (format[i + 1] == ' ' && !format[i + 2]) 34 | return (-1); 35 | handl_buf(buffer, format[i], ibuf), len++, i--; 36 | } 37 | else 38 | { 39 | len += function(arguments, buffer, ibuf); 40 | i += ev_print_func(format, i + 1); 41 | } 42 | } i++; 43 | } 44 | else 45 | handl_buf(buffer, format[i], ibuf), len++; 46 | for (ibuf = len; ibuf > 1024; ibuf -= 1024) 47 | ; 48 | } 49 | print_buf(buffer, ibuf), free(buffer), va_end(arguments); 50 | return (len); 51 | } 52 | -------------------------------------------------------------------------------- /print_add.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | #include 3 | /** 4 | * print_add - prints the address of an input variable 5 | * @arguments: input address. 6 | * @buf: buffer pointer. 7 | * @ibuf: index for buffer pointer 8 | * 9 | * Return: number of chars printed. 10 | */ 11 | int print_add(va_list arguments, char *buf, unsigned int ibuf) 12 | { 13 | void *add; 14 | long int int_input; 15 | int i, count, first_digit, isnegative; 16 | char *hexadecimal, *binary; 17 | char nill[] = "(nil)"; 18 | 19 | add = (va_arg(arguments, void *)); 20 | if (add == NULL) 21 | { 22 | for (i = 0; nill[i]; i++) 23 | ibuf = handl_buf(buf, nill[i], ibuf); 24 | return (5); 25 | } 26 | int_input = (intptr_t)add; 27 | isnegative = 0; 28 | if (int_input < 0) 29 | { 30 | int_input = (int_input * -1) - 1; 31 | isnegative = 1; 32 | } 33 | binary = malloc(sizeof(char) * (64 + 1)); 34 | binary = fill_binary_array(binary, int_input, isnegative, 64); 35 | hexadecimal = malloc(sizeof(char) * (16 + 1)); 36 | hexadecimal = fill_hex_array(binary, hexadecimal, 0, 16); 37 | ibuf = handl_buf(buf, '0', ibuf); 38 | ibuf = handl_buf(buf, 'x', ibuf); 39 | for (first_digit = i = count = 0; hexadecimal[i]; i++) 40 | { 41 | if (hexadecimal[i] != '0' && first_digit == 0) 42 | first_digit = 1; 43 | if (first_digit) 44 | { 45 | ibuf = handl_buf(buf, hexadecimal[i], ibuf); 46 | count++; 47 | } 48 | } 49 | free(binary); 50 | free(hexadecimal); 51 | return (count + 2); 52 | } 53 | -------------------------------------------------------------------------------- /ev_print_func.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | /** 4 | * ev_print_func - returns the amount of identifiers. 5 | * @s: argument indentifier 6 | * @index: index of argument identifier. 7 | * Return: amount of identifiers. 8 | */ 9 | int ev_print_func(const char *s, int index) 10 | { 11 | print_t pr[] = { 12 | {"c", print_chr}, {"s", print_str}, {"i", print_int}, 13 | {"d", print_int}, {"b", print_bnr}, {"u", print_unt}, 14 | {"o", print_oct}, {"x", print_hex}, {"X", print_upx}, 15 | {"S", print_usr}, {"p", print_add}, {"li", prinlint}, 16 | {"ld", prinlint}, {"lu", prinlunt}, {"lo", prinloct}, 17 | {"lx", prinlhex}, {"lX", prinlupx}, {"hi", prinhint}, 18 | {"hd", prinhint}, {"hu", prinhunt}, {"ho", prinhoct}, 19 | {"hx", prinhhex}, {"hX", prinhupx}, {"#o", prinnoct}, 20 | {"#x", prinnhex}, {"#X", prinnupx}, {"#i", print_int}, 21 | {"#d", print_int}, {"#u", print_unt}, {"+i", prinpint}, 22 | {"+d", prinpint}, {"+u", print_unt}, {"+o", print_oct}, 23 | {"+x", print_hex}, {"+X", print_upx}, {" i", prinsint}, 24 | {" d", prinsint}, {" u", print_unt}, {" o", print_oct}, 25 | {" x", print_hex}, {" X", print_upx}, {"R", print_rot}, 26 | {"r", print_rev}, {"%", print_prg}, {"l", print_prg}, 27 | {"h", print_prg}, {" +i", prinpint}, {" +d", prinpint}, 28 | {"+ i", prinpint}, {"+ d", prinpint}, {" %", print_prg}, 29 | {NULL, NULL}, 30 | }; 31 | int i = 0, j = 0, first_index; 32 | 33 | first_index = index; 34 | while (pr[i].type_arg) 35 | { 36 | if (s[index] == pr[i].type_arg[j]) 37 | { 38 | if (pr[i].type_arg[j + 1] != '\0') 39 | index++, j++; 40 | else 41 | break; 42 | } 43 | else 44 | { 45 | j = 0; 46 | i++; 47 | index = first_index; 48 | } 49 | } 50 | return (j); 51 | } 52 | -------------------------------------------------------------------------------- /get_print_func.c: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | /** 3 | * get_print_func - selects the correct function to perform the operation. 4 | * @s: argument indentifier 5 | * @index: index for argument indentifier 6 | * Return: pointer to a function. 7 | */ 8 | int (*get_print_func(const char *s, int index))(va_list, char *, unsigned int) 9 | { 10 | print_t pr[] = { 11 | {"c", print_chr}, {"s", print_str}, 12 | {"i", print_int}, {"d", print_int}, 13 | {"b", print_bnr}, {"u", print_unt}, 14 | {"o", print_oct}, {"x", print_hex}, 15 | {"X", print_upx}, {"S", print_usr}, 16 | {"p", print_add}, {"li", prinlint}, 17 | {"ld", prinlint}, {"lu", prinlunt}, 18 | {"lo", prinloct}, {"lx", prinlhex}, 19 | {"lX", prinlupx}, {"hi", prinhint}, 20 | {"hd", prinhint}, {"hu", prinhunt}, 21 | {"ho", prinhoct}, {"hx", prinhhex}, 22 | {"hX", prinhupx}, {"#o", prinnoct}, 23 | {"#x", prinnhex}, {"#X", prinnupx}, 24 | {"#i", print_int}, {"#d", print_int}, 25 | {"#u", print_unt}, {"+i", prinpint}, 26 | {"+d", prinpint}, {"+u", print_unt}, 27 | {"+o", print_oct}, {"+x", print_hex}, 28 | {"+X", print_upx}, {" i", prinsint}, 29 | {" d", prinsint}, {" u", print_unt}, 30 | {" o", print_oct}, {" x", print_hex}, 31 | {" X", print_upx}, {"R", print_rot}, 32 | {"r", print_rev}, {"%", print_prg}, 33 | {"l", print_prg}, {"h", print_prg}, 34 | {" +i", prinpint}, {" +d", prinpint}, 35 | {"+ i", prinpint}, {"+ d", prinpint}, 36 | {" %", print_prg}, {NULL, NULL}, 37 | }; 38 | int i = 0, j = 0, first_index; 39 | 40 | first_index = index; 41 | while (pr[i].type_arg) 42 | { 43 | if (s[index] == pr[i].type_arg[j]) 44 | { 45 | if (pr[i].type_arg[j + 1] != '\0') 46 | index++, j++; 47 | else 48 | break; 49 | } 50 | else 51 | { 52 | j = 0; 53 | i++; 54 | index = first_index; 55 | } 56 | } 57 | return (pr[i].f); 58 | } 59 | -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /** 9 | * struct print - struct for printer functions 10 | * @type_arg: identifier 11 | * @f: pointer to a printer functions 12 | * 13 | * Description: struct that stores pointers to a 14 | * printer functions. 15 | */ 16 | typedef struct print 17 | { 18 | char *type_arg; 19 | int (*f)(va_list, char *, unsigned int); 20 | } print_t; 21 | 22 | int _printf(const char *format, ...); 23 | int print_prg(va_list __attribute__((unused)), char *, unsigned int); 24 | int print_chr(va_list arguments, char *buf, unsigned int ibuf); 25 | int print_str(va_list arguments, char *buf, unsigned int ibuf); 26 | int print_int(va_list arguments, char *buf, unsigned int ibuf); 27 | int print_bnr(va_list arguments, char *buf, unsigned int ibuf); 28 | int print_unt(va_list arguments, char *buf, unsigned int ibuf); 29 | int print_oct(va_list arguments, char *buf, unsigned int ibuf); 30 | int print_hex(va_list arguments, char *buf, unsigned int ibuf); 31 | int print_upx(va_list arguments, char *buf, unsigned int ibuf); 32 | int print_usr(va_list arguments, char *buf, unsigned int ibuf); 33 | int print_add(va_list arguments, char *buf, unsigned int ibuf); 34 | int print_rev(va_list arguments, char *buf, unsigned int ibuf); 35 | int print_rot(va_list arguments, char *buf, unsigned int ibuf); 36 | int prinlint(va_list arguments, char *buf, unsigned int ibuf); 37 | int prinlunt(va_list arguments, char *buf, unsigned int ibuf); 38 | int prinloct(va_list arguments, char *buf, unsigned int ibuf); 39 | int prinlhex(va_list arguments, char *buf, unsigned int ibuf); 40 | int prinlupx(va_list arguments, char *buf, unsigned int ibuf); 41 | int prinhint(va_list arguments, char *buf, unsigned int ibuf); 42 | int prinhunt(va_list arguments, char *buf, unsigned int ibuf); 43 | int prinhoct(va_list arguments, char *buf, unsigned int ibuf); 44 | int prinhhex(va_list arguments, char *buf, unsigned int ibuf); 45 | int prinhupx(va_list arguments, char *buf, unsigned int ibuf); 46 | int prinpint(va_list arguments, char *buf, unsigned int ibuf); 47 | int prinnoct(va_list arguments, char *buf, unsigned int ibuf); 48 | int prinnhex(va_list arguments, char *buf, unsigned int ibuf); 49 | int prinnupx(va_list arguments, char *buf, unsigned int ibuf); 50 | int prinsint(va_list arguments, char *buf, unsigned int ibuf); 51 | int (*get_print_func(const char *s, int index))(va_list, char *, unsigned int); 52 | int ev_print_func(const char *s, int index); 53 | unsigned int handl_buf(char *buf, char c, unsigned int ibuf); 54 | int print_buf(char *buf, unsigned int nbuf); 55 | char *fill_binary_array(char *binary, long int int_in, int isneg, int limit); 56 | char *fill_oct_array(char *bnr, char *oct); 57 | char *fill_long_oct_array(char *bnr, char *oct); 58 | char *fill_short_oct_array(char *bnr, char *oct); 59 | char *fill_hex_array(char *bnr, char *hex, int isupp, int limit); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /man_3_printf: -------------------------------------------------------------------------------- 1 | .\" Manpage for _printf. 2 | .TH _printf 3 "18 April 2022" "1.0" "Program Manual for _printf" 3 | .SH NAME 4 | _printf - formatted output conversion and print data. 5 | .SH SYNOPSIS 6 | .nf 7 | .BI printf (FORMAT, ARGUMENT)... 8 | .PP 9 | .BI "#include 'main.h' 10 | .BI "#include " 11 | .BI "#include " 12 | .PP 13 | .BI "int _printf(const char *format, ...);" 14 | .BI "int print_prg(va_list __attribute__((unused)), char *, unsigned int);" 15 | .BI "int print_chr(va_list arguments, char *buf, unsigned int ibuf);" 16 | .BI "int print_str(va_list arguments, char *buf, unsigned int ibuf);" 17 | .BI "int print_int(va_list arguments, char *buf, unsigned int ibuf);" 18 | .BI "int print_bnr(va_list arguments, char *buf, unsigned int ibuf);" 19 | .BI "int print_unt(va_list arguments, char *buf, unsigned int ibuf);" 20 | .BI "int print_oct(va_list arguments, char *buf, unsigned int ibuf);" 21 | .BI "int print_hex(va_list arguments, char *buf, unsigned int ibuf);" 22 | .BI "int print_upx(va_list arguments, char *buf, unsigned int ibuf);" 23 | .BI "int print_usr(va_list arguments, char *buf, unsigned int ibuf);" 24 | .BI "int print_add(va_list arguments, char *buf, unsigned int ibuf);" 25 | .BI "int print_rev(va_list arguments, char *buf, unsigned int ibuf);" 26 | .BI "int print_rot(va_list arguments, char *buf, unsigned int ibuf);" 27 | .BI "int prinlint(va_list arguments, char *buf, unsigned int ibuf);" 28 | .BI "int prinlunt(va_list arguments, char *buf, unsigned int ibuf);" 29 | .BI "int prinloct(va_list arguments, char *buf, unsigned int ibuf);" 30 | .BI "int prinlhex(va_list arguments, char *buf, unsigned int ibuf);" 31 | .BI "int prinlupx(va_list arguments, char *buf, unsigned int ibuf);" 32 | .BI "int prinhint(va_list arguments, char *buf, unsigned int ibuf);" 33 | .BI "int prinhunt(va_list arguments, char *buf, unsigned int ibuf);" 34 | .BI "int prinhoct(va_list arguments, char *buf, unsigned int ibuf);" 35 | .BI "int prinhhex(va_list arguments, char *buf, unsigned int ibuf);" 36 | .BI "int prinhupx(va_list arguments, char *buf, unsigned int ibuf);" 37 | .BI "int prinpint(va_list arguments, char *buf, unsigned int ibuf);" 38 | .BI "int prinnoct(va_list arguments, char *buf, unsigned int ibuf);" 39 | .BI "int prinnhex(va_list arguments, char *buf, unsigned int ibuf);" 40 | .BI "int prinnupx(va_list arguments, char *buf, unsigned int ibuf);" 41 | .BI "int prinsint(va_list arguments, char *buf, unsigned int ibuf);" 42 | .PP 43 | .BI "int (*get_print_func(const char *s, int index))(va_list, char *, unsigned int);" 44 | .BI "int ev_print_func(const char *s, int index);" 45 | .PP 46 | .BI "unsigned int handl_buf(char *buf, char c, unsigned int ibuf);" 47 | .BI "int print_buf(char *buf, unsigned int nbuf);" 48 | .PP 49 | .BI "char *fill_binary_array(char *binary, long int int_in, int isneg, int limit);" 50 | .BI "char *fill_oct_array(char *bnr, char *oct);" 51 | .BI "char *fill_long_oct_array(char *bnr, char *oct);" 52 | .BI "char *fill_short_oct_array(char *bnr, char *oct);" 53 | .BI "char *fill_hex_array(char *bnr, char *hex, int isupp, int limit);" 54 | .PP 55 | .SH DESCRIPTION 56 | The output function _printf() produce output according to a format. the function _printf converts the character strings that receives as argument and prints it on the standard output. 57 | .SH RETURN VALUE 58 | Returns the number of all the characters printed, excluding the NULL byte used to end output to strings. 59 | .SH Format String Format 60 | The format string is a character string, which contains two types of directives: ordinary characters which are coppied to the output stream; and conversion specifiers. Each conversion specification is introduced by the character %, and ends with a conversion specifier. 61 | .SH Conversion Specifiers 62 | This are the characters that specifies the type of conversion to be applied. The conversion specifiers and their meanings are: 63 | .TP 64 | .B c 65 | .R The \fIint\fR argument is converted to an \fIunsigned char\fR, and the resulting character is written. 66 | .TP 67 | .B s 68 | .R The \fIconst char *\fR argument is a pointer to an array of characters, that converts the corresponding argument to a character string. 69 | .TP 70 | .B d, i 71 | .R The \fIint\fR argument is converted to signed decimal notation. 72 | .TP 73 | .B o, u, x, X 74 | .R The \fIunsigned int\fR argument is converted to unsigned octal \fIo\fR (base 8 number), unsigned decimal \fIu\fR (base 10 number), unsigned hexadecimal \fIx\fR (base 16 number with lowercase letters) and unsigned hexadecimal \fIX\fR (base 16 number with uppercase letters). 75 | .TP 76 | .B %, %% 77 | .R If only the \fI%\fR character is written no argument is converted. The complete conversion specification is \fI%%\fR, that returns the actual sign if there in front. 78 | .TP 79 | .B S 80 | .R The \fIconst char\fR argument is a pointer to an array of characters, that converts the corresponding argument to a character st\ 81 | ring, with non-printable characters (0 < ASCII value < 32 or >= 127). 82 | .TP 83 | .B p 84 | .R The \fIvoid * pointer\fR argument is printed in hexadecimal. 85 | .TP 86 | .B S 87 | .R The \fIconst char\fR argument is a pointer to an array 88 | .SH The flag characters 89 | The character \fI%\fR is followed by zero the following flags: 90 | .TP 91 | .B + 92 | .R A sign \fI+\fR or \fI-\fR will be placed before a number followed by a signed conversion. 93 | .TP 94 | .B # 95 | .R Print number in hexadecimal, upeercase hexadecimal and octal where the first character of the output string is made zero. 96 | .TP 97 | .B ' ' 98 | .R A space in blank should be left before a positive number followed by a signed conversion. 99 | .SH NOTES.R A space in blank should be left before a positive number followed by a signed conversion. 100 | .SH NOTES 101 | .R The \fB_printf()\fR is a project collaboration between \fBSuara Ayomide\fR and \fBOni Remi\fR, students of the ALX Software Engineering Programme at \fBHolberton School\fR. 102 | .SH BUGS 103 | .R In process 104 | .SH EXAMPLE 105 | .R To print the the string \fBHello Holberton!\fR and its length in decimal. 106 | 107 | #include ''holberton.h'' 108 | 109 | int main(void) 110 | 111 | { 112 | 113 | int length; 114 | 115 | _printf(''%s'', ''Hello, Holberton!'') 116 | 117 | _printf(''Hello Holberton! contains %d characters'', length); 118 | 119 | length = _printf(''Hello Holberton!''); 120 | 121 | return (0); 122 | 123 | } 124 | .SH SEE ALSO 125 | .R printf(3) 126 | .SH AUTHORS 127 | Written by \fBSuara Ayomide\fR and \fBOni Remi\fR. 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # printf() 2 | The printf project is a collaboration project by **Ayomide Suara** and **Remi Oni**, students of the ALX Software Engineering Programme by Holberton School, in which a function named "_printf" imitates the actual "printf" command located in the stdio.h library. It contains some of the basic features and functions found in the manual 3 of "printf". 3 | 4 | _printf() is a function that performs formatted output conversion and prints data. Its prototype is the following: 5 | 6 | int _printf(const char *format, ...) 7 | 8 | Where **format** contains the string that is printed. As _printf() is variadic function, it can receives n arguments that replace by n tags written inside the string. 9 | 10 | The format tags prototype is the following: 11 | 12 | %[flags][length]specifier 13 | 14 | If the program runs successfully, the **return value** is the amount of chars printed. 15 | 16 | | Specifier | Output | 17 | | ------------- | ------------- | 18 | | c | Character | 19 | | d or i | Signed decimal integer | 20 | | s | String of characters | 21 | | b | Signed binary | 22 | | o | Signed octal | 23 | | u | Unsigned integer | 24 | | x | Unsigned hexadecimal | 25 | | X | Unsigned hexadecimal (uppercase) | 26 | | p | Pointer address | 27 | | r | Reverse string of characters | 28 | | R | ROT13 translation of string | 29 | | S | String with special chars replaced by their ASCII value | 30 | | % | Character | 31 | 32 | | Flags | Description | Specifiers | 33 | | ------------- | ------------- | ------------- | 34 | | + | Prints a plus sign (+) when the argument is a positive number. In other case, prints a minus sign (-). | i, d | 35 | | (space) | Prints a blank space if the argument is a positive number | i, d | 36 | | # | Prints 0, 0x and 0X for o, x and X specifiers, respectively. It doesn't print anything if the argument is zero | o, x, X | 37 | 38 | | Length | Description | Specifiers | 39 | | ------------- | ------------- | ------------- | 40 | | l | Prints a long int or unsigned long int | i, d, o, u, x and X | 41 | | h | Prints a short int or unsigned short int | i, d, o, u, x and X | 42 | 43 | ------------ 44 | 45 | ## Examples 46 | 47 | 1. Printing the string of chars "Hello, Holberton": 48 | + Use: `_printf("Hello Hol%s.", "berton");` 49 | + Output: `Hello Holberton.` 50 | 51 | 2. Printing an integer number: 52 | + Use: `_printf("10 + 10 is equal to %d.", 20);` 53 | + Output: `10 + 10 is equal to 20.` 54 | 55 | 3. Printing a binary, octal and hexadecimal: 56 | + Use: `_printf("10 in binary is [%b], in octal is [%o] and in hexadecimal is [%x]", 5, 5, 5);` 57 | + Output: `10 in binary is [1010], in octal is [12] and in hexadecimal is [A]` 58 | 59 | 4. Printing a string codified in ROT13: 60 | + Use: `_printf("Hello in ROT13 is %R", "Hello");` 61 | + Output: `Hello in ROT13 is Urybb` 62 | 63 | Using flags and length tags: 64 | 65 | 5. Printing the string of chars "Hello, Holberton": 66 | + Use: `_printf("2 * 2 = %+d and 5 * -5 = %+i", 4, -25);` 67 | + Output: `2 * 2 = +4 and 5 * -5 = -25` 68 | 69 | 6. Printing a long integer number and short integer number: 70 | + Use: `_printf("1 million as a long int is %ld, but as a short int is %hd", 1000000, 1000000);` 71 | + Output: `1 million as a long int is 1000000, but as a short int is 16960` 72 | 73 | 74 | ------------ 75 | 76 | ## File Functions 77 | 78 | ### _printf.c 79 | Own Printf Function That Performs Formatted Output Conversion And Print Data. 80 | 81 | ------------ 82 | 83 | ### main.h 84 | Header File Where All Prototypes Are Saved. 85 | 86 | ------------ 87 | 88 | ### get_print_func.c 89 | Pointer To A Function That Selects The Correct Function To Perform The Operation. 90 | 91 | ------------ 92 | 93 | ### print_buf.c 94 | Function That Prints The Buffer. 95 | 96 | ------------ 97 | 98 | ### handl_buf.c 99 | Function That Concatenates The Buffer Characters. 100 | 101 | ------------ 102 | 103 | ### print_chr.c 104 | Function That Writes The Character C To Stdout. 105 | ```c 106 | /* Indetifier : %c */ 107 | ``` 108 | 109 | ------------ 110 | 111 | ### print_str.c 112 | Function That Writes The String To Stdout. 113 | ```c 114 | /* Indetifier : %s */ 115 | ``` 116 | 117 | ------------ 118 | 119 | ### print_int.c 120 | Function That Prints An Integer. 121 | ```c 122 | /* Indetifier : %i or %d */ 123 | ``` 124 | 125 | ------------ 126 | 127 | ### print_bnr.c 128 | Function That Prints Decimal In Binary. 129 | ```c 130 | /* Indetifier : %b */ 131 | ``` 132 | 133 | ------------ 134 | 135 | ### print_oct.c 136 | Function That Prints Decimal In Octal. 137 | ```c 138 | /* Indetifier : %o */ 139 | ``` 140 | 141 | ------------ 142 | 143 | ### print_hex.c 144 | Function That Prints Decimal In Hexadecimal. 145 | ```c 146 | /* Indetifier : %x */ 147 | ``` 148 | 149 | ------------ 150 | 151 | ### print_upx.c 152 | Function That Prints Decimal In Uppercase Hexadecimal. 153 | ```c 154 | /* Indetifier : %X */ 155 | ``` 156 | 157 | ------------ 158 | 159 | ### print_usr.c 160 | Function That Prints A String And Values Of Non-Printed Chars. 161 | ```c 162 | /* Indetifier : %S */ 163 | ``` 164 | 165 | ------------ 166 | 167 | ### print_unt.c 168 | Function That Prints An Unsigned Integer. 169 | ```c 170 | /* Indetifier : %u */ 171 | ``` 172 | 173 | ------------ 174 | 175 | ### print_rev.c 176 | Function That Writes The String To Stdout In Reverse. 177 | ```c 178 | /* Indetifier : %r */ 179 | ``` 180 | 181 | ------------ 182 | 183 | ### print_rot.c 184 | Function That Writes The String To Stdout In Rot13. 185 | ```c 186 | /* Indetifier : %R */ 187 | ``` 188 | 189 | ------------ 190 | 191 | ### print_add.c 192 | Function That Prints The Address Of An Input Variable. 193 | ```c 194 | /* Indetifier : %p */ 195 | ``` 196 | 197 | ------------ 198 | 199 | ### print_long_oct.c 200 | Function That Prints Long Decimal Number In Octal. 201 | ```c 202 | /* Indetifier : %lo */ 203 | ``` 204 | 205 | ------------ 206 | 207 | ### print_long_hex.c 208 | Function That Prints Long Decimal Number In Hexadecimal. 209 | ```c 210 | /* Indetifier : %lx */ 211 | ``` 212 | 213 | ------------ 214 | 215 | ### print_long_int.c 216 | Function That Prints A Long Integer. 217 | ```c 218 | /* Indetifier : %li */ 219 | ``` 220 | 221 | ------------ 222 | 223 | ### print_long_upx.c 224 | Function That Prints A Long Decimal In Uppercase Hexadecimal. 225 | ```c 226 | /* Indetifier : %lX */ 227 | ``` 228 | 229 | ------------ 230 | 231 | ### print_long_unt.c 232 | Function That Prints A Long Unsigned Integer. 233 | ```c 234 | /* Indetifier : %lu */ 235 | ``` 236 | 237 | ------------ 238 | 239 | ### print_short_oct.c 240 | Function That Prints Short Decimal Number In Octal. 241 | ```c 242 | /* Indetifier : %ho */ 243 | ``` 244 | 245 | ------------ 246 | 247 | ### print_short_hex.c 248 | Function That Prints Short Decimal Number In Hexadecimal. 249 | ```c 250 | /* Indetifier : %hx */ 251 | ``` 252 | 253 | ------------ 254 | 255 | ### print_short_int.c 256 | Function That Prints A Short Integer. 257 | ```c 258 | /* Indetifier : %hi */ 259 | ``` 260 | 261 | ------------ 262 | 263 | ### print_short_upx.c 264 | Function That Prints A Short Decimal In Uppercase Hexadecimal. 265 | ```c 266 | /* Indetifier : %hX */ 267 | ``` 268 | 269 | ------------ 270 | 271 | ### print_short_unt.c 272 | Function That Prints A Short Unsigned Integer. 273 | ```c 274 | /* Indetifier : %hu */ 275 | ``` 276 | 277 | ------------ 278 | 279 | ### print_num_hex.c 280 | Function That Print A Number In Hexadecimal Begining With 0 And x. 281 | ```c 282 | /* Indetifier : %#x */ 283 | ``` 284 | 285 | ------------ 286 | 287 | ### print_num_oct.c 288 | Function That Prints A Number In Octal Begining With 0 And o. 289 | ```c 290 | /* Indetifier : %#o */ 291 | ``` 292 | 293 | ------------ 294 | 295 | ### print_num_upx.c 296 | Function That Prints A Number In Uppercase Hexadecimal. 297 | ```c 298 | /* Indetifier : %#X */ 299 | ``` 300 | 301 | ------------ 302 | 303 | ### print_plus_int.c 304 | Function That Prints An Integer With Plus Symbol. 305 | ```c 306 | /* Indetifier : %+i */ 307 | ``` 308 | 309 | ------------ 310 | 311 | ### print_space_int.c 312 | Function That Prints An Integer Begining With 0 And u. 313 | ```c 314 | /* Indetifier : % i */ 315 | ``` 316 | 317 | ------------ 318 | 319 | ### ev_print_func.c 320 | Function That Returns The Amount Of Indetifiers. 321 | 322 | ------------ 323 | 324 | ## Authors 325 | Suara Ayomide 326 | 327 | Oni Remi 328 | 329 | ------------ 330 | 331 | ### End 332 | --------------------------------------------------------------------------------