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

64 lines
1.3 KiB

12 years ago
  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <dev.h>
  4. #include <vfs.h>
  5. #include <iobuf.h>
  6. #include <inode.h>
  7. #include <unistd.h>
  8. #include <error.h>
  9. #include <assert.h>
  10. static int
  11. stdout_open(struct device *dev, uint32_t open_flags) {
  12. if (open_flags != O_WRONLY) {
  13. return -E_INVAL;
  14. }
  15. return 0;
  16. }
  17. static int
  18. stdout_close(struct device *dev) {
  19. return 0;
  20. }
  21. static int
  22. stdout_io(struct device *dev, struct iobuf *iob, bool write) {
  23. if (write) {
  24. char *data = iob->io_base;
  25. for (; iob->io_resid != 0; iob->io_resid --) {
  26. cputchar(*data ++);
  27. }
  28. return 0;
  29. }
  30. return -E_INVAL;
  31. }
  32. static int
  33. stdout_ioctl(struct device *dev, int op, void *data) {
  34. return -E_INVAL;
  35. }
  36. static void
  37. stdout_device_init(struct device *dev) {
  38. dev->d_blocks = 0;
  39. dev->d_blocksize = 1;
  40. dev->d_open = stdout_open;
  41. dev->d_close = stdout_close;
  42. dev->d_io = stdout_io;
  43. dev->d_ioctl = stdout_ioctl;
  44. }
  45. void
  46. dev_init_stdout(void) {
  47. struct inode *node;
  48. if ((node = dev_create_inode()) == NULL) {
  49. panic("stdout: dev_create_node.\n");
  50. }
  51. stdout_device_init(vop_info(node, device));
  52. int ret;
  53. if ((ret = vfs_add_dev("stdout", node, 0)) != 0) {
  54. panic("stdout: vfs_add_dev: %e.\n", ret);
  55. }
  56. }