/* * Sample program for open() and close() system call - No. 5 and No. 6 * * open(2) - open a file or device * * Synopsis * #include * #include * #include * * int open(const char *pathname, int flags); * int open(const char *pathname, int flags, mode_t mode); * * * typedef unsigned int __u_int --- * * tpyedef __u_int __mode_t; ------ * * typedef __mode_t mode_t; ------- * * close(2) - close a file descriptor * * Synopsis * #include * * int close(int fd); */ #include /* from */ #define O_RDONLY 00 #define O_WRONLY 01 #define O_RDWR 02 /* from */ #define STDIN_FILENO 0 /* Standard input. */ #define STDOUT_FILENO 1 /* Standard output. */ #define STDERR_FILENO 2 /* Standard error output. */ main(int argc, char *argv[]) { int fd, n; char buf[BUFSIZ]; fd = open(argv[1], O_RDONLY); while((n = read(fd, buf, sizeof buf)) > 0) { write(STDOUT_FILENO, buf, n); } close(fd); exit(0); }