|
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<stdlib.h>
- #include<stdio.h>
-
- int main(void)
- {
- int fdsrc, fdnull, fdtmp, numbytes;
- int flags=O_CREAT | O_EXCL | O_WRONLY;
- char buf[10];
-
- if ((fdsrc=open("fdread.c", O_RDONLY, 0644))==-1){
- perror("open fdread.c");
- exit(1);
- }
- if ((fdnull=open("/dev/null", O_WRONLY))==-1){
- perror("open /dev/null");
- close(fdsrc);
- exit(1);
- }
- if ((fdtmp=open("/tmp/foo.bar", flags, 0644))==-1){
- perror("open /tmp/foo.bar");
- close(fdsrc);
- close(fdnull);
- exit(1);
- }
-
- while((numbytes=read(fdsrc, buf, 10))>0){
- if(write(fdnull, buf, 10)==-1){
- perror("write /dev/null");
- }
- if(write(fdtmp, buf, 10)==-1){
- perror("write /tmp/foo.bar");
- }
- }
- close(fdsrc);
- close(fdnull);
- close(fdtmp);
- exit(0);
- }
复制代码
运行后,more /tmp/foo.bar的结果不但打印了上述源代码,还在最后一行显示了一句
xit(0);
请问这是何原因 |
|