stdio.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <stdarg.h>
  22. #include "stdio.h"
  23. const char lowercase[] = "0123456789abcdef";
  24. const char uppercase[] = "0123456789abcdef";
  25. int (*stderr_putc)(int c);
  26. int (*stdout_putc)(int c);
  27. int (*stream_putc)(int c, FILE *stream);
  28. int fputc(int c, FILE *stream)
  29. {
  30. if(stream == stdout)
  31. return stdout_putc(c);
  32. else if(stream == stderr)
  33. return stderr_putc(c);
  34. else
  35. return stream_putc(c, stream);
  36. }
  37. int putc(int c, FILE *stream)
  38. {
  39. return fputc(c, stream);
  40. }
  41. int putchar(int c)
  42. {
  43. return fputc(c, stdout);
  44. }
  45. int fputs(const char *s, FILE *stream)
  46. {
  47. while(*s)
  48. fputc(*s++, stream);
  49. return 0;
  50. }
  51. int puts(const char *s)
  52. {
  53. fputs(s, stdout);
  54. fputc('\n', stdout);
  55. return 0;
  56. }
  57. void puthex(uint32_t val)
  58. {
  59. for(int i = 7; i >= 0; i--)
  60. putchar(lowercase[(val >> (i * 4)) & 0xf]);
  61. }
  62. void putval(uint32_t val, int base, char *dest, int dest_size, int dest_start, int padding, char *case_str)
  63. {
  64. int i;
  65. if(padding > (dest_size - dest_start))
  66. padding = dest_size - dest_start;
  67. for(i = 0; i < padding; i++)
  68. dest[i + dest_start] = '0';
  69. i = 0;
  70. while((val != 0) && (i < (dest_size - dest_start)))
  71. {
  72. uint32_t digit = val % base;
  73. dest[dest_size - i - 1 + dest_start] = case_str[digit];
  74. i++;
  75. val /= base;
  76. }
  77. }