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

34 lines
698 B

10 years ago
  1. #include <ulib.h>
  2. #include <unistd.h>
  3. #include <file.h>
  4. #include <stat.h>
  5. int main(int argc, char *argv[]);
  6. static int
  7. initfd(int fd2, const char *path, uint32_t open_flags) {
  8. int fd1, ret;
  9. if ((fd1 = open(path, open_flags)) < 0) {
  10. return fd1;
  11. }
  12. if (fd1 != fd2) {
  13. close(fd2);
  14. ret = dup2(fd1, fd2);
  15. close(fd1);
  16. }
  17. return ret;
  18. }
  19. void
  20. umain(int argc, char *argv[]) {
  21. int fd;
  22. if ((fd = initfd(0, "stdin:", O_RDONLY)) < 0) {
  23. warn("open <stdin> failed: %e.\n", fd);
  24. }
  25. if ((fd = initfd(1, "stdout:", O_WRONLY)) < 0) {
  26. warn("open <stdout> failed: %e.\n", fd);
  27. }
  28. int ret = main(argc, argv);
  29. exit(ret);
  30. }