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

62 lines
1.2 KiB

пре 12 година
  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <syscall.h>
  4. /* *
  5. * cputch - writes a single character @c to stdout, and it will
  6. * increace the value of counter pointed by @cnt.
  7. * */
  8. static void
  9. cputch(int c, int *cnt) {
  10. sys_putc(c);
  11. (*cnt) ++;
  12. }
  13. /* *
  14. * vcprintf - format a string and writes it to stdout
  15. *
  16. * The return value is the number of characters which would be
  17. * written to stdout.
  18. *
  19. * Call this function if you are already dealing with a va_list.
  20. * Or you probably want cprintf() instead.
  21. * */
  22. int
  23. vcprintf(const char *fmt, va_list ap) {
  24. int cnt = 0;
  25. vprintfmt((void*)cputch, &cnt, fmt, ap);
  26. return cnt;
  27. }
  28. /* *
  29. * cprintf - formats a string and writes it to stdout
  30. *
  31. * The return value is the number of characters which would be
  32. * written to stdout.
  33. * */
  34. int
  35. cprintf(const char *fmt, ...) {
  36. va_list ap;
  37. va_start(ap, fmt);
  38. int cnt = vcprintf(fmt, ap);
  39. va_end(ap);
  40. return cnt;
  41. }
  42. /* *
  43. * cputs- writes the string pointed by @str to stdout and
  44. * appends a newline character.
  45. * */
  46. int
  47. cputs(const char *str) {
  48. int cnt = 0;
  49. char c;
  50. while ((c = *str ++) != '\0') {
  51. cputch(c, &cnt);
  52. }
  53. cputch('\n', &cnt);
  54. return cnt;
  55. }