os_kernel_lab/labcodes/lab8/user/libs/umain.c

35 lines
698 B
C
Raw Normal View History

2012-08-22 12:32:13 +08:00
#include <ulib.h>
#include <unistd.h>
#include <file.h>
#include <stat.h>
int main(int argc, char *argv[]);
static int
initfd(int fd2, const char *path, uint32_t open_flags) {
int fd1, ret;
if ((fd1 = open(path, open_flags)) < 0) {
return fd1;
}
if (fd1 != fd2) {
close(fd2);
ret = dup2(fd1, fd2);
close(fd1);
}
return ret;
}
void
umain(int argc, char *argv[]) {
int fd;
if ((fd = initfd(0, "stdin:", O_RDONLY)) < 0) {
warn("open <stdin> failed: %e.\n", fd);
}
if ((fd = initfd(1, "stdout:", O_WRONLY)) < 0) {
warn("open <stdout> failed: %e.\n", fd);
}
int ret = main(argc, argv);
exit(ret);
}