Facebook
From Perl Matamata, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 65
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <math.h>
  7. #include <limits.h>
  8.  
  9. static int write_stdout(const char *token, int length)
  10. {
  11.         int rc;
  12.         int bytes_written = 0;
  13.  
  14.         do {
  15.                 rc = write(1, token + bytes_written, length - bytes_written);
  16.                 if (rc < 0)
  17.                         return rc;
  18.  
  19.                 bytes_written += rc;
  20.         } while (bytes_written < length);
  21.  
  22.         return bytes_written;
  23. }
  24.  
  25. void reverse(char *array, long length)
  26. {
  27.         long start,end; char aux;
  28.         end=length-1;
  29.         for(start=0; start<end; start++){
  30.                 aux=*(array+start);
  31.                 *(array+start)=*(array+end);
  32.                 *(array+end)=aux;
  33.                 end--;
  34.         }
  35.         *(array+length)='\0';
  36. }
  37. char* toString(long number, int baza)
  38. {
  39.         char *array=(char*)malloc(1001*sizeof(char));
  40.     int i = 0, negative = 0;
  41.     if(number == 0) {
  42.         *(array+i) = '0';
  43.         i++;
  44.         reverse(array,i);
  45.         return array;
  46.     }
  47.     if(number<0) {
  48.         negative = 1;
  49.         number = abs(number);
  50.     }
  51.  
  52.     while (number != 0) {
  53.         int rest = number % baza;
  54.         if(rest>9)
  55.                 rest=rest+ 'a'-10;
  56.         else
  57.                 rest=rest+'0';
  58.  
  59.         *(array+i)=rest;
  60.         i++;
  61.         number = number/baza;
  62.     }
  63.     if (negative){
  64.        *(array+i) = '-';
  65.        i++;
  66.     }
  67.     reverse(array, i);
  68.     return array;
  69. }
  70. char* toStringhex(unsigned long number, int baza)
  71. {
  72.         char *array=(char*)malloc(1001*sizeof(char));
  73.     int i = 0;
  74.     if(number == 0) {
  75.         *(array+i) = '0';
  76.         reverse(array,i);
  77.         return array;
  78.     }
  79.     while (number != 0) {
  80.         int rest = number % baza;
  81.         if(rest>9)
  82.                 rest=rest-10+ 'a';
  83.         else
  84.                 rest=rest+'0';
  85.  
  86.         *(array+i)=rest;
  87.         i++;
  88.         number = number/baza;
  89.     }
  90.     reverse(array, i);
  91.     return array;
  92. }
  93.  
  94. unsigned long check(long number)
  95. {
  96.         if((1+(number>>31)-(-number>>31))==0)
  97.                 return(UINT_MAX+number+1);
  98.         else
  99.                 return number;
  100.        
  101. }
  102.  
  103. int iocla_printf(const char *format, ...)
  104. {
  105.         char *cpy_format=(char*)malloc((strlen(format)-1)*sizeof(char));
  106.         strcpy(cpy_format,format);
  107.         va_list arguments;
  108.         va_start(arguments, format);
  109.         long counter=0; long element;
  110.         char *array;
  111.         int checked;
  112.         for(long i=0;i<strlen(cpy_format);i++){
  113.                 checked=0;
  114.                 if(i<strlen(cpy_format) && *(cpy_format+i)!='%')
  115.                                 counter+=write_stdout(cpy_format+i,1);
  116.                 if(*(cpy_format+i)=='%'){
  117.                         i++; checked=1;
  118.                 }
  119.                 if(*(cpy_format+i)=='d' && checked){
  120.                         element=va_arg(arguments, int);
  121.                         array=toString(element,10);
  122.                         counter+=write_stdout(array,strlen(array));
  123.                 }
  124.  
  125.                 else if(*(cpy_format+i)=='x' && checked){
  126.                         element=va_arg(arguments, unsigned int);
  127.                         element=check(element);
  128.                         array=toStringhex(element,16);
  129.                         counter+=write_stdout(array,strlen(array));
  130.                 }
  131.                 else if(*(cpy_format+i)=='s' && checked){
  132.                         array=va_arg(arguments, char *);
  133.                         counter+=write_stdout(array,strlen(array));
  134.  
  135.                 }
  136.                 else if(*(cpy_format+i)=='u' && checked){
  137.                         element=va_arg(arguments,unsigned int);
  138.                         element=check(element);
  139.                         array=toStringhex(element,10);
  140.                         counter+=write_stdout(array,strlen(array));
  141.                 }
  142.                 else if(*(cpy_format+i)=='%'){
  143.                         counter+=write_stdout(cpy_format+i,1);
  144.                 }
  145.                 else if(*(cpy_format+i)=='c' && checked){
  146.                         element=va_arg(arguments, int);
  147.                         counter++;
  148.                         putchar(element);
  149.                 }
  150.         }
  151.         va_end(arguments);
  152.         return counter;
  153. }
  154.