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

68 lines
1.3 KiB

12 years ago
  1. #include <defs.h>
  2. #include <string.h>
  3. #include <syscall.h>
  4. #include <stdio.h>
  5. #include <stat.h>
  6. #include <error.h>
  7. #include <unistd.h>
  8. int
  9. open(const char *path, uint32_t open_flags) {
  10. return sys_open(path, open_flags);
  11. }
  12. int
  13. close(int fd) {
  14. return sys_close(fd);
  15. }
  16. int
  17. read(int fd, void *base, size_t len) {
  18. return sys_read(fd, base, len);
  19. }
  20. int
  21. write(int fd, void *base, size_t len) {
  22. return sys_write(fd, base, len);
  23. }
  24. int
  25. seek(int fd, off_t pos, int whence) {
  26. return sys_seek(fd, pos, whence);
  27. }
  28. int
  29. fstat(int fd, struct stat *stat) {
  30. return sys_fstat(fd, stat);
  31. }
  32. int
  33. fsync(int fd) {
  34. return sys_fsync(fd);
  35. }
  36. int
  37. dup2(int fd1, int fd2) {
  38. return sys_dup(fd1, fd2);
  39. }
  40. static char
  41. transmode(struct stat *stat) {
  42. uint32_t mode = stat->st_mode;
  43. if (S_ISREG(mode)) return 'r';
  44. if (S_ISDIR(mode)) return 'd';
  45. if (S_ISLNK(mode)) return 'l';
  46. if (S_ISCHR(mode)) return 'c';
  47. if (S_ISBLK(mode)) return 'b';
  48. return '-';
  49. }
  50. void
  51. print_stat(const char *name, int fd, struct stat *stat) {
  52. cprintf("[%03d] %s\n", fd, name);
  53. cprintf(" mode : %c\n", transmode(stat));
  54. cprintf(" links : %lu\n", stat->st_nlinks);
  55. cprintf(" blocks : %lu\n", stat->st_blocks);
  56. cprintf(" size : %lu\n", stat->st_size);
  57. }