Penguin

Differences between current version and previous revision of fcntl(2).

Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History

Newer page: version 4 Last edited on Sunday, November 3, 2002 3:12:33 pm by PerryLorier
Older page: version 3 Last edited on Sunday, November 3, 2002 2:54:57 pm by PerryLorier Revert
@@ -93,7 +93,102 @@
 !!CONFORMING TO 
 SVr4, SVID, POSIX, X/OPEN, BSD 4.3. Only the operations F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLK and F_SETLKW are specified in POSIX.1. F_GETOWN and F_SETOWN are BSDisms not supported in SVr4; F_GETSIG and F_SETSIG are specific to Linux. The flags legal for F_GETFL/F_SETFL are those supported by open(2) and vary between these systems; O_APPEND, O_NONBLOCK, O_RDONLY, and O_RDWR are specified in POSIX.1. SVr4 supports several other options and flags not documented here. 
  
 SVr4 documents additional [EIO], [ENOLINK] and [EOVERFLOW] error conditions. 
+  
+!!EXAMPLES  
+ /*  
+ * This program demonstrates using fcntl(2) similar to dup(2)  
+ */  
+  
+ #include <sys/stat.h> /* for S_* constants */  
+ #include <unistd.h> /* write(2), close(2) prototype */  
+ #include <fcntl.h> /* fcntl(2), creat(2) prototypes, F_* constants */  
+ #include <stdio.h> /* fprintf(3), stderr prototype */  
+ #include <string.h> /* strerror(3) prototype */  
+ #include <errno.h> /* for errno prototype */  
+  
+ #define FILENAME "/tmp/fcntl_dup.example"  
+  
+ /*  
+ * Input:  
+ * fd: a file descriptor to write to  
+ * msg: A message to write  
+ * Returns:  
+ * 0: success  
+ * non 0: error (error message already written to stderr)  
+ */  
+ int do_write(int fd,char *msg)  
+ {  
+ int err;  
+  
+ err=write(fd,msg,strlen(msg));  
+  
+ if (err==-1) {  
+ fprintf(  
+ stderr,  
+ "write(fd,\"%s\",strlen(\"%s\")): %s (%i)\n",  
+ msg,  
+ msg,  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
+  
+ if (err!=strlen(msg)) {  
+ fprintf(  
+ stderr,  
+ "write did not write the full length, and I'm too dumb to try again\n"  
+ );  
+ return 1;  
+ }  
+ return 0;  
+ }  
+  
+ int main(int argc,char **argv)  
+ {  
+ int fd;  
+ int fd2;  
+  
+ fd=creat(FILENAME,S_IRUSR|S_IWUSR);  
+ if (fd==-1) {  
+ fprintf(  
+ stderr,  
+ "creat(\"%s\",S_IRUSR|S_IWUSR): %s (%i)\n",  
+ FILENAME,  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
+  
+ if (do_write(fd,"This is some sample text\n")!=0) {  
+ return 1;  
+ }  
+  
+ /* Now, use fcntl to create a new fd, greater or equal to 0 */  
+ fd2 = fcntl(fd,F_DUPFD,0);  
+  
+ if (fd2 == -1) {  
+ fprintf(  
+ stderr,  
+ "fcntl(fd,F_DUPFD,0): %s (%i)\n",  
+ strerror(errno),  
+ errno);  
+ return 1;  
+ }  
+  
+ if (do_write(fd2,"This proves that fd2 is working\n")!=0) {  
+ return 1;  
+ }  
+  
+ if (do_write(fd,"This proves that fd's file pointer has been moved by fd2 "  
+ "writing to the socket\n")!=0) {  
+ return 1;  
+ }  
+  
+ close(fd);  
+ close(fd2);  
+ return 0;  
+ }  
  
 !!SEE ALSO 
 dup2(2), flock(2), open(2), socket(2) 
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.