第三章 文件IO:
STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
filedes -> [0, OPEN_MAX], OPEN_MAX->63
#include <fcntl.h>
int open(const char * pathname, int oflag, ... /* mode_t mode */ );
oflag:
O_RDONLY, O_WRONLY, O_RDWR
O_APPEND, O_CREAT, O_EXCL
O_TRUNC, O_NOCTTY, O_NONBLOCK
O_DSYNC, O_RSYNC, O_SYNC
mode:
S_ISUSR, S_ISGID, (S: set)
S_ISVTX, (SVTX: save text)
S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR
S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP
S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH
#include <fcntl.h>
int creat(const char * pathname, mode_t mode);
same as: open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
#include <unistd.h>
int close(int filedes);
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
whence:
SEEK_SET, SEEK_CUR, SEEK_END
#include <unistd.h>
ssize_t read(int filedes, void * buf, size_t nbytes);
ssize_t write(int filedes, const void * buf, size_t nbytes);
ssize_t pread(int filedes, void * buf, size_t nbytes, off_t offset);
ssize_t pwrite(int filedes, const void * buf, size_t nbytes, off_t offset);
#include <unistd.h>
int dup(int filedes);
int dup2(int filedes, int filedes2);
#include <unistd.h>
int fsync(int filedes);
int fdatasync(int filedes);
void sync(void);
#include <fcntl.h>
int fcntl(int filedes, int cmd, ... /* int arg */ );
cmd:
F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL,
F_GETOWN, F_SETOWN, F_GETLK, F_SETLK, F_SETLKW(ait)
#include <unistd.h> /* System V */
#include <sys/ioctl.h> /* BSD and Linux */
#include <stropts.h> /* XSI STREAMS */
int ioctl(int filedes, int request, ...);
示例:
#include <stdio.h>
#include <unistd.h>
int
main(int argc, char * argv[])
{
if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1) {
printf("can not seek\n");
}
else {
printf("seek ok\n");
}
return 0;
}
#include <stdio.h>
#include <fcntl.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int
main(int argc, char * argv[])
{
int fd;
if ((fd = creat("file.hole", S_IRWXU | S_IRWXG | S_IRWXO)) < 0) {
printf("creat error\n");
return -1;
}
if (write(fd, buf1, 10) != 10) {
printf("buf1 write error\n");
return -1;
}
if(lseek(fd, 16384, SEEK_SET) == -1) {
printf("lseek error\n");
return -1;
}
if (write(fd, buf2, 10) != 10) {
printf("buf2 write error\n");
return -1;
}
close(fd);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#define BUFFSIZE 4096
int
main(int argc, char * argv[])
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write error\n");
return -1;
}
}
if (n < 0) {
printf("read error\n");
return -1;
}
return 0;
}
#include <stdio.h>
#include <fcntl.h>
int
main(int argc, char * argv[])
{
int val;
if (argc != 2) {
printf("usage: ./a.out <descriptor#>\n");
return -1;
}
if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {
printf("fcntl error for fd %d\n", atoi(argv[1]));
return -1;
}
switch (val & O_ACCMODE) {
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
printf("unknown access mode");
break;
}
if (val & O_APPEND) {
printf(", append");
}
#if defined(NONBLOCK)
if (val & NONBLOCK) {
printf(", nonblocking");
}
#endif
#if defined(O_SYNC)
if (val & O_SYNC) {
printf(", synchronous writes");
}
#endif
#if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC)
if (val & O_FSYNC) {
printf(", synchronous writes");
}
#endif
putchar('\n');
return 0;
}
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
void
set_fl(int fd, int flags)
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
printf("fcntl F_GETFL error\n");
return;
}
if (fcntl(fd, F_SETFL, (val | flags)) < 0) {
printf("fcntl F_SETFL error\n");
return;
}
}
void
clr_fl(int fd, int flags)
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
printf("fcntl F_GETFL error\n");
return;
}
if (fcntl(fd, F_SETFL, (val & ~flags)) < 0) {
printf("fcntl F_SETFL error\n");
return;
}
}
void
test()
{
int n;
char buf[4096];
if ((n = read(STDIN_FILENO, buf, 4096)) < 0) {
printf("read error\n");
return;
}
printf("read ok\n");
if (write(STDOUT_FILENO, buf, n) < 0) {
printf("write error\n");
return;
}
else {
printf("write ok\n");
}
}
int
main(int argc, char * argv[])
{
clr_fl(STDIN_FILENO, O_RDONLY | O_RDWR);
printf("clear stdin read\n");
test();
set_fl(STDIN_FILENO, O_RDONLY | O_RDWR);
printf("set stdin read\n");
test();
return 0;
}
#include <stdio.h>
#include <unistd.h>
void
copy(int fd, int fd2)
{
int n;
char buf[4096];
char * p;
while ((n = read(fd, buf, 4096)) > 0) {
p = buf;
while (n > 0) {
if (*p != '\0') {
if (write(fd2, p, 1) != 1) {
printf("write error\n");
return;
}
}
++p;
--n;
}
}
if (n < 0) {
printf("read error\n");
}
}
int
main(int argc, char * argv[])
{
copy(STDIN_FILENO, STDOUT_FILENO);
return 0;
}