《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB

  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <console.h>
  4. /* HIGH level console I/O */
  5. /* *
  6. * cputch - writes a single character @c to stdout, and it will
  7. * increace the value of counter pointed by @cnt.
  8. * */
  9. static void
  10. cputch(int c, int *cnt) {
  11. cons_putc(c);
  12. (*cnt) ++;
  13. }
  14. /* *
  15. * vcprintf - format a string and writes it to stdout
  16. *
  17. * The return value is the number of characters which would be
  18. * written to stdout.
  19. *
  20. * Call this function if you are already dealing with a va_list.
  21. * Or you probably want cprintf() instead.
  22. * */
  23. int
  24. vcprintf(const char *fmt, va_list ap) {
  25. int cnt = 0;
  26. vprintfmt((void*)cputch, &cnt, fmt, ap);
  27. return cnt;
  28. }
  29. /* *
  30. * cprintf - formats a string and writes it to stdout
  31. *
  32. * The return value is the number of characters which would be
  33. * written to stdout.
  34. * */
  35. int
  36. cprintf(const char *fmt, ...) {
  37. va_list ap;
  38. int cnt;
  39. va_start(ap, fmt);
  40. cnt = vcprintf(fmt, ap);
  41. va_end(ap);
  42. return cnt;
  43. }
  44. /* cputchar - writes a single character to stdout */
  45. void
  46. cputchar(int c) {
  47. cons_putc(c);
  48. }
  49. /* *
  50. * cputs- writes the string pointed by @str to stdout and
  51. * appends a newline character.
  52. * */
  53. int
  54. cputs(const char *str) {
  55. int cnt = 0;
  56. char c;
  57. while ((c = *str ++) != '\0') {
  58. cputch(c, &cnt);
  59. }
  60. cputch('\n', &cnt);
  61. return cnt;
  62. }
  63. /* getchar - reads a single non-zero character from stdin */
  64. int
  65. getchar(void) {
  66. int c;
  67. while ((c = cons_getc()) == 0)
  68. /* do nothing */;
  69. return c;
  70. }