- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <math.h>
- #include <limits.h>
- static int write_stdout(const char *token, int length)
- {
- int rc;
- int bytes_written = 0;
- do {
- rc = write(1, token + bytes_written, length - bytes_written);
- if (rc < 0)
- return rc;
- bytes_written += rc;
- } while (bytes_written < length);
- return bytes_written;
- }
- void reverse(char *array, long length)
- {
- long start,end; char aux;
- end=length-1;
- for(start=0; start<end; start++){
- aux=*(array+start);
- *(array+start)=*(array+end);
- *(array+end)=aux;
- end--;
- }
- *(array+length)='\0';
- }
- char* toString(long number, int baza)
- {
- char *array=(char*)malloc(1001*sizeof(char));
- int i = 0, negative = 0;
- if(number == 0) {
- *(array+i) = '0';
- i++;
- reverse(array,i);
- return array;
- }
- if(number<0) {
- negative = 1;
- number = abs(number);
- }
- while (number != 0) {
- int rest = number % baza;
- if(rest>9)
- rest=rest+ 'a'-10;
- else
- rest=rest+'0';
- *(array+i)=rest;
- i++;
- number = number/baza;
- }
- if (negative){
- *(array+i) = '-';
- i++;
- }
- reverse(array, i);
- return array;
- }
- char* toStringhex(unsigned long number, int baza)
- {
- char *array=(char*)malloc(1001*sizeof(char));
- int i = 0;
- if(number == 0) {
- *(array+i) = '0';
- reverse(array,i);
- return array;
- }
- while (number != 0) {
- int rest = number % baza;
- if(rest>9)
- rest=rest-10+ 'a';
- else
- rest=rest+'0';
- *(array+i)=rest;
- i++;
- number = number/baza;
- }
- reverse(array, i);
- return array;
- }
- unsigned long check(long number)
- {
- if((1+(number>>31)-(-number>>31))==0)
- return(UINT_MAX+number+1);
- else
- return number;
- }
- int iocla_printf(const char *format, ...)
- {
- char *cpy_format=(char*)malloc((strlen(format)-1)*sizeof(char));
- strcpy(cpy_format,format);
- va_list arguments;
- va_start(arguments, format);
- long counter=0; long element;
- char *array;
- int checked;
- for(long i=0;i<strlen(cpy_format);i++){
- checked=0;
- if(i<strlen(cpy_format) && *(cpy_format+i)!='%')
- counter+=write_stdout(cpy_format+i,1);
- if(*(cpy_format+i)=='%'){
- i++; checked=1;
- }
- if(*(cpy_format+i)=='d' && checked){
- element=va_arg(arguments, int);
- array=toString(element,10);
- counter+=write_stdout(array,strlen(array));
- }
- else if(*(cpy_format+i)=='x' && checked){
- element=va_arg(arguments, unsigned int);
- element=check(element);
- array=toStringhex(element,16);
- counter+=write_stdout(array,strlen(array));
- }
- else if(*(cpy_format+i)=='s' && checked){
- array=va_arg(arguments, char *);
- counter+=write_stdout(array,strlen(array));
- }
- else if(*(cpy_format+i)=='u' && checked){
- element=va_arg(arguments,unsigned int);
- element=check(element);
- array=toStringhex(element,10);
- counter+=write_stdout(array,strlen(array));
- }
- else if(*(cpy_format+i)=='%'){
- counter+=write_stdout(cpy_format+i,1);
- }
- else if(*(cpy_format+i)=='c' && checked){
- element=va_arg(arguments, int);
- counter++;
- putchar(element);
- }
- }
- va_end(arguments);
- return counter;
- }