《操作系统》的实验代码。
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

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