1*f65a228fSRobert Mustacchi.\" 2*f65a228fSRobert Mustacchi.\" This file and its contents are supplied under the terms of the 3*f65a228fSRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0. 4*f65a228fSRobert Mustacchi.\" You may only use this file in accordance with the terms of version 5*f65a228fSRobert Mustacchi.\" 1.0 of the CDDL. 6*f65a228fSRobert Mustacchi.\" 7*f65a228fSRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this 8*f65a228fSRobert Mustacchi.\" source. A copy of the CDDL is also available via the Internet at 9*f65a228fSRobert Mustacchi.\" http://www.illumos.org/license/CDDL. 10*f65a228fSRobert Mustacchi.\" 11*f65a228fSRobert Mustacchi.\" 12*f65a228fSRobert Mustacchi.\" Copyright 2020 Robert Mustacchi 13*f65a228fSRobert Mustacchi.\" 14*f65a228fSRobert Mustacchi.Dd March 25, 2020 15*f65a228fSRobert Mustacchi.Dt FMEMOPEN 3C 16*f65a228fSRobert Mustacchi.Os 17*f65a228fSRobert Mustacchi.Sh NAME 18*f65a228fSRobert Mustacchi.Nm fmemopen 19*f65a228fSRobert Mustacchi.Nd open a memory stream 20*f65a228fSRobert Mustacchi.Sh SYNOPSIS 21*f65a228fSRobert Mustacchi.In stdio.h 22*f65a228fSRobert Mustacchi.Ft "FILE *" 23*f65a228fSRobert Mustacchi.Fo fmemopen 24*f65a228fSRobert Mustacchi.Fa "void *restrict buf" 25*f65a228fSRobert Mustacchi.Fa "size_t size" 26*f65a228fSRobert Mustacchi.Fa "const char *restrict mode" 27*f65a228fSRobert Mustacchi.Fc 28*f65a228fSRobert Mustacchi.Sh DESCRIPTION 29*f65a228fSRobert MustacchiThe 30*f65a228fSRobert Mustacchi.Fn fmemopen 31*f65a228fSRobert Mustacchifunction provides a means of associating a file stream with a 32*f65a228fSRobert Mustacchicorresponding memory buffer of a fixed size. 33*f65a228fSRobert MustacchiThe resulting stream can then be used just like any other stream, and 34*f65a228fSRobert Mustacchiwhen done should be released by calling the 35*f65a228fSRobert Mustacchi.Xr fclose 3C 36*f65a228fSRobert Mustacchifunction. 37*f65a228fSRobert Mustacchi.Pp 38*f65a228fSRobert MustacchiThe stream can either dynamically allocate memory or it can use an 39*f65a228fSRobert Mustacchiexisting memory buffer. 40*f65a228fSRobert MustacchiIf 41*f65a228fSRobert Mustacchi.Fa buf 42*f65a228fSRobert Mustacchiis 43*f65a228fSRobert Mustacchi.Dv NULL , 44*f65a228fSRobert Mustacchithen a buffer 45*f65a228fSRobert Mustacchi.Fa size 46*f65a228fSRobert Mustacchibytes long will be allocated for the stream and initialized to zero. 47*f65a228fSRobert MustacchiThis buffer will be allocated as though a call to 48*f65a228fSRobert Mustacchi.Xr malloc 3C 49*f65a228fSRobert Mustacchiand will be freed when 50*f65a228fSRobert Mustacchi.Xr fclose 3C 51*f65a228fSRobert Mustacchiis called. 52*f65a228fSRobert MustacchiWhen using this mode, the stream must be created for update 53*f65a228fSRobert Mustacchi.Po 54*f65a228fSRobert Mustacchiindicated by a 55*f65a228fSRobert Mustacchi.Sq + 56*f65a228fSRobert Mustacchicharacter in the 57*f65a228fSRobert Mustacchi.Fa mode 58*f65a228fSRobert Mustacchiargument 59*f65a228fSRobert Mustacchi.Pc . 60*f65a228fSRobert MustacchiOtherwise, it is assumed that 61*f65a228fSRobert Mustacchi.Fa buf 62*f65a228fSRobert Mustacchiis at least 63*f65a228fSRobert Mustacchi.Fa size 64*f65a228fSRobert Mustacchibytes long. 65*f65a228fSRobert Mustacchi.Pp 66*f65a228fSRobert MustacchiThe 67*f65a228fSRobert Mustacchi.Fa mode 68*f65a228fSRobert Mustacchiargument determines whether the stream is opened for read, write, or 69*f65a228fSRobert Mustacchiappend. 70*f65a228fSRobert MustacchiThe 71*f65a228fSRobert Mustacchi.Fa mode 72*f65a228fSRobert Mustacchiargument accepts all the same values as 73*f65a228fSRobert Mustacchi.Xr fopen 3C . 74*f65a228fSRobert Mustacchi.Pp 75*f65a228fSRobert MustacchiThe resulting stream behaves in a similar way to a stream backed by a 76*f65a228fSRobert Mustacchifile. 77*f65a228fSRobert MustacchiThe stream can be read and written to. 78*f65a228fSRobert MustacchiThe stream is seekeable and can either be byte or wide-character 79*f65a228fSRobert Mustacchioriented. 80*f65a228fSRobert MustacchiA NUL byte has no special meaning when reading. 81*f65a228fSRobert Mustacchi.Pp 82*f65a228fSRobert MustacchiThe stream logically tracks three different values: 83*f65a228fSRobert Mustacchi.Bl -enum -offset indent 84*f65a228fSRobert Mustacchi.It 85*f65a228fSRobert MustacchiThe current position in the stream. 86*f65a228fSRobert Mustacchi.It 87*f65a228fSRobert MustacchiThe current size of the stream. 88*f65a228fSRobert Mustacchi.It 89*f65a228fSRobert MustacchiThe maximum size of the stream. 90*f65a228fSRobert Mustacchi.El 91*f65a228fSRobert Mustacchi.Pp 92*f65a228fSRobert MustacchiThe current position is where reads or writes take place. 93*f65a228fSRobert MustacchiWhen the stream is opened for read or write 94*f65a228fSRobert Mustacchi.Pq r, r+, w, w+ 95*f65a228fSRobert Mustacchithen the initial position is set to zero. 96*f65a228fSRobert MustacchiIf the stream is opened for update 97*f65a228fSRobert Mustacchi.Pq a, a+ 98*f65a228fSRobert Mustacchithen the initial position is set to the first NUL byte in the buffer. 99*f65a228fSRobert Mustacchi.Pp 100*f65a228fSRobert MustacchiThe current size of the stream represents the length of the stream. 101*f65a228fSRobert MustacchiLike a file, this starts at a specific size and then can grow over time. 102*f65a228fSRobert MustacchiUnlike a file, where the maximum size is determined by the file system, 103*f65a228fSRobert Mustacchithe maximum size here is determined at the creation of the stream. 104*f65a228fSRobert Mustacchi.Pp 105*f65a228fSRobert MustacchiThis size is used when using 106*f65a228fSRobert Mustacchi.Dv SEEK_END 107*f65a228fSRobert Mustacchias an argument to functions like 108*f65a228fSRobert Mustacchi.Xr fseek 3C . 109*f65a228fSRobert MustacchiReads cannot advance beyond the current size of the stream and 110*f65a228fSRobert Mustacchiattempting to read beyond it is considered hitting the end-of-file. 111*f65a228fSRobert MustacchiWrites beyond the end of the current size will cause the current size to 112*f65a228fSRobert Mustacchiincrease, though it cannot increase beyond the maximum size. 113*f65a228fSRobert Mustacchi.Pp 114*f65a228fSRobert MustacchiThe initial size of the stream varies. 115*f65a228fSRobert MustacchiIt is set depending on the mode and works as follows: 116*f65a228fSRobert Mustacchi.Bl -tag -width Sy -offset indent 117*f65a228fSRobert Mustacchi.It Sy r, r+ 118*f65a228fSRobert MustacchiThe size is set to the 119*f65a228fSRobert Mustacchi.Fa size 120*f65a228fSRobert Mustacchiargument. 121*f65a228fSRobert Mustacchi.It Sy w, w+ 122*f65a228fSRobert MustacchiThe initial size is set to zero. 123*f65a228fSRobert Mustacchi.It Sy a, a+ 124*f65a228fSRobert MustacchiThe initial size is set according to the following rules: 125*f65a228fSRobert Mustacchi.Bl -enum 126*f65a228fSRobert Mustacchi.It 127*f65a228fSRobert MustacchiIf 128*f65a228fSRobert Mustacchi.Fa buf 129*f65a228fSRobert Mustacchiis a 130*f65a228fSRobert Mustacchi.Dv NULL 131*f65a228fSRobert Mustacchipointer, the current size is set to zero. 132*f65a228fSRobert Mustacchi.It 133*f65a228fSRobert MustacchiIf a NUL byte is found in the first 134*f65a228fSRobert Mustacchi.Fa size 135*f65a228fSRobert Mustacchibytes of 136*f65a228fSRobert Mustacchi.Fa buf , 137*f65a228fSRobert Mustacchithen the current size is set to the first NUL byte. 138*f65a228fSRobert Mustacchi.It 139*f65a228fSRobert MustacchiThe position is set to the 140*f65a228fSRobert Mustacchi.Fa size 141*f65a228fSRobert Mustacchiargument 142*f65a228fSRobert Mustacchi.Pq the maximum size 143*f65a228fSRobert Mustacchiif no NUL byte was found in 144*f65a228fSRobert Mustacchi.Fa buf . 145*f65a228fSRobert Mustacchi.El 146*f65a228fSRobert Mustacchi.El 147*f65a228fSRobert Mustacchi.Pp 148*f65a228fSRobert MustacchiThe maximum size of the stream is determined by the 149*f65a228fSRobert Mustacchi.Fa size 150*f65a228fSRobert Mustacchiargument. 151*f65a228fSRobert MustacchiWrites beyond this size are dropped. 152*f65a228fSRobert MustacchiAttempts to seek beyond the maximum size will result in an error. 153*f65a228fSRobert Mustacchi.Pp 154*f65a228fSRobert MustacchiIf the stream was open for writing or update, when the stream is flushed 155*f65a228fSRobert Mustacchior closed, a NUL byte will be written to terminate the stream based on 156*f65a228fSRobert Mustacchithe current position and size of the stream. 157*f65a228fSRobert MustacchiIf the stream was open for update, if the stream is flushed or closed 158*f65a228fSRobert Mustacchiand the last write changed the current buffer size, a NUL byte will be 159*f65a228fSRobert Mustacchiwritten if there is still space for it within the buffer. 160*f65a228fSRobert Mustacchi.Pp 161*f65a228fSRobert MustacchiBy default, all streams are buffered. 162*f65a228fSRobert MustacchiThis means that writes beyond the size of the memory buffer could fail, 163*f65a228fSRobert Mustacchibut not be seen until the stream is flushed or closed. 164*f65a228fSRobert MustacchiTo detect errors right away, one can explicitly disable buffering with 165*f65a228fSRobert Mustacchithe 166*f65a228fSRobert Mustacchi.Xr setvbuf 3C 167*f65a228fSRobert Mustacchifunction or perform explicit buffer flushes with the 168*f65a228fSRobert Mustacchi.Xr fflush 3C 169*f65a228fSRobert Mustacchifunction. 170*f65a228fSRobert Mustacchi.Sh RETURN VALUES 171*f65a228fSRobert MustacchiUpon successful completion, the 172*f65a228fSRobert Mustacchi.Fn fmemopen 173*f65a228fSRobert Mustacchifunction returns a pointer to a stream. 174*f65a228fSRobert MustacchiOtherwise, 175*f65a228fSRobert Mustacchi.Dv NULL 176*f65a228fSRobert Mustacchiis returned and 177*f65a228fSRobert Mustacchi.Dv errno 178*f65a228fSRobert Mustacchiis set to indicate the error. 179*f65a228fSRobert Mustacchi.Sh ERRORS 180*f65a228fSRobert MustacchiThe 181*f65a228fSRobert Mustacchi.Fn fmemopen 182*f65a228fSRobert Mustacchifunction will fail if: 183*f65a228fSRobert Mustacchi.Bl -tag -width Er 184*f65a228fSRobert Mustacchi.It Er EINVAL 185*f65a228fSRobert MustacchiThe value of 186*f65a228fSRobert Mustacchi.Fa mode 187*f65a228fSRobert Mustacchiis invalid. 188*f65a228fSRobert Mustacchi.Pp 189*f65a228fSRobert MustacchiThe 190*f65a228fSRobert Mustacchi.Fa size 191*f65a228fSRobert Mustacchiargument was zero. 192*f65a228fSRobert Mustacchi.Pp 193*f65a228fSRobert MustacchiThe 194*f65a228fSRobert Mustacchi.Fa buf 195*f65a228fSRobert Mustacchiargument is 196*f65a228fSRobert Mustacchi.Dv NULL 197*f65a228fSRobert Mustacchiand the 198*f65a228fSRobert Mustacchi.Fa mode 199*f65a228fSRobert Mustacchiargument does not contain a 200*f65a228fSRobert Mustacchi.Sq + 201*f65a228fSRobert Mustacchicharacter. 202*f65a228fSRobert Mustacchi.It Er EMFILE 203*f65a228fSRobert Mustacchi.Brq FOPEN_MAX 204*f65a228fSRobert Mustacchistreams are currently open in the calling process. 205*f65a228fSRobert Mustacchi.Pp 206*f65a228fSRobert Mustacchi.Brq STREAM_MAX 207*f65a228fSRobert Mustacchistreams are currently open in the calling process. 208*f65a228fSRobert Mustacchi.It Er ENOMEM 209*f65a228fSRobert MustacchiThe system was unable to allocate memory for the stream or its backing 210*f65a228fSRobert Mustacchibuffer. 211*f65a228fSRobert Mustacchi.El 212*f65a228fSRobert Mustacchi.Sh MT-LEVEL 213*f65a228fSRobert Mustacchi.Sy MT-Safe 214*f65a228fSRobert Mustacchi.Sh INTERFACE STABILITY 215*f65a228fSRobert Mustacchi.Sy Committed 216*f65a228fSRobert Mustacchi.Sh SEE ALSO 217*f65a228fSRobert Mustacchi.Xr fclose 3C , 218*f65a228fSRobert Mustacchi.Xr fflush 3C , 219*f65a228fSRobert Mustacchi.Xr fopen 3C , 220*f65a228fSRobert Mustacchi.Xr fseek 3C , 221*f65a228fSRobert Mustacchi.Xr malloc 3C , 222*f65a228fSRobert Mustacchi.Xr open_memstream 3C 223