OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / fopencookie.3
index a590bb9..78d4abb 100644 (file)
@@ -1,6 +1,7 @@
 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
 .\"      <mtk.manpages@gmail.com>
 .\"
+.\" %%%LICENSE_START(VERBATIM)
 .\" Permission is granted to make and distribute verbatim copies of this
 .\" manual provided the copyright notice and this permission notice are
 .\" preserved on all copies.
@@ -20,8 +21,9 @@
 .\"
 .\" Formatted or processed versions of this manual, if unaccompanied by
 .\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
 .\"
-.TH FOPENCOOKIE 3 2008-12-05 "Linux" "Linux Programmer's Manual"
+.TH FOPENCOOKIE 3 2015-01-22 "Linux" "Linux Programmer's Manual"
 .SH NAME
 fopencookie \- opening a custom stream
 .SH SYNOPSIS
@@ -105,12 +107,12 @@ The structure is defined as follows
 .in +4n
 .nf
 
-struct cookie_io_functions_t {
+typedef struct {
     cookie_read_function_t  *read;
     cookie_write_function_t *write;
     cookie_seek_function_t  *seek;
     cookie_close_function_t *close;
-};
+} cookie_io_functions_t;
 
 .fi
 .in
@@ -139,7 +141,7 @@ function should update the stream offset appropriately.
 
 If
 .I *read
-is a NULL pointer,
+is a null pointer,
 then reads from the custom stream always return end of file.
 .TP
 .I cookie_write_function_t *write
@@ -158,14 +160,15 @@ As its function result, the
 .I write
 function should return the number of bytes copied from
 .IR buf ,
-or \-1 on error.
+or 0 on error.
+(The function must not return a negative value.)
 The
 .I write
 function should update the stream offset appropriately.
 
 If
 .I *write
-is a NULL pointer,
+is a null pointer,
 then output to the stream is discarded.
 .TP
 .I cookie_seek_function_t *seek
@@ -207,7 +210,7 @@ function should return 0 on success, and \-1 on error.
 
 If
 .I *seek
-is a NULL pointer,
+is a null pointer,
 then it is not possible to perform seek operations on the stream.
 .TP
 .I cookie_close_function_t *close
@@ -292,8 +295,8 @@ memfile_write(void *c, const char *buf, size_t size)
 
     /* Buffer too small? Keep doubling size until big enough */
 
-    while (size + cookie\->offset > cookie->allocated) {
-        new_buff = realloc(cookie\->buf, cookie->allocated * 2);
+    while (size + cookie\->offset > cookie\->allocated) {
+        new_buff = realloc(cookie\->buf, cookie\->allocated * 2);
         if (new_buff == NULL) {
             return \-1;
         } else {
@@ -302,11 +305,11 @@ memfile_write(void *c, const char *buf, size_t size)
         }
     }
 
-    memcpy(cookie\->buf + cookie->offset, buf, size);
+    memcpy(cookie\->buf + cookie\->offset, buf, size);
 
     cookie\->offset += size;
-    if (cookie\->offset > cookie->endpos)
-        cookie\->endpos = cookie->offset;
+    if (cookie\->offset > cookie\->endpos)
+        cookie\->endpos = cookie\->offset;
 
     return size;
 }
@@ -320,12 +323,12 @@ memfile_read(void *c, char *buf, size_t size)
     /* Fetch minimum of bytes requested and bytes available */
 
     xbytes = size;
-    if (cookie\->offset + size > cookie->endpos)
-        xbytes = cookie\->endpos - cookie->offset;
+    if (cookie\->offset + size > cookie\->endpos)
+        xbytes = cookie\->endpos \- cookie\->offset;
     if (xbytes < 0)     /* offset may be past endpos */
        xbytes = 0;
 
-    memcpy(buf, cookie\->buf + cookie->offset, xbytes);
+    memcpy(buf, cookie\->buf + cookie\->offset, xbytes);
 
     cookie\->offset += xbytes;
     return xbytes;
@@ -375,7 +378,7 @@ main(int argc, char *argv[])
         .seek  = memfile_seek,
         .close = memfile_close
     };
-    FILE *fp;
+    FILE *stream;
     struct memfile_cookie mycookie;
     ssize_t nread;
     long p;
@@ -394,8 +397,8 @@ main(int argc, char *argv[])
     mycookie.offset = 0;
     mycookie.endpos = 0;
 
-    fp = fopencookie(&mycookie,"w+", memfile_func);
-    if (fp == NULL) {
+    stream = fopencookie(&mycookie,"w+", memfile_func);
+    if (stream == NULL) {
         perror("fopencookie");
         exit(EXIT_FAILURE);
     }
@@ -403,7 +406,7 @@ main(int argc, char *argv[])
     /* Write command\-line arguments to our file */
 
     for (j = 1; j < argc; j++)
-        if (fputs(argv[j], fp) == EOF) {
+        if (fputs(argv[j], stream) == EOF) {
             perror("fputs");
             exit(EXIT_FAILURE);
         }
@@ -411,11 +414,11 @@ main(int argc, char *argv[])
     /* Read two bytes out of every five, until EOF */
 
     for (p = 0; ; p += 5) {
-        if (fseek(fp, p, SEEK_SET) == \-1) {
+        if (fseek(stream, p, SEEK_SET) == \-1) {
             perror("fseek");
             exit(EXIT_FAILURE);
         }
-        nread = fread(buf, 1, 2, fp);
+        nread = fread(buf, 1, 2, stream);
         if (nread == \-1) {
             perror("fread");
             exit(EXIT_FAILURE);
@@ -436,3 +439,12 @@ main(int argc, char *argv[])
 .BR fmemopen (3),
 .BR fopen (3),
 .BR fseek (3)
+.SH COLOPHON
+This page is part of release 3.79 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.