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

89 lines
1.6 KiB

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