xref: /onnv-gate/usr/src/common/openssl/doc/crypto/BIO_s_file.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948BIO_s_file, BIO_new_file, BIO_new_fp, BIO_set_fp, BIO_get_fp,
6*2175Sjp161948BIO_read_filename, BIO_write_filename, BIO_append_filename,
7*2175Sjp161948BIO_rw_filename - FILE bio
8*2175Sjp161948
9*2175Sjp161948=head1 SYNOPSIS
10*2175Sjp161948
11*2175Sjp161948 #include <openssl/bio.h>
12*2175Sjp161948
13*2175Sjp161948 BIO_METHOD *	BIO_s_file(void);
14*2175Sjp161948 BIO *BIO_new_file(const char *filename, const char *mode);
15*2175Sjp161948 BIO *BIO_new_fp(FILE *stream, int flags);
16*2175Sjp161948
17*2175Sjp161948 BIO_set_fp(BIO *b,FILE *fp, int flags);
18*2175Sjp161948 BIO_get_fp(BIO *b,FILE **fpp);
19*2175Sjp161948
20*2175Sjp161948 int BIO_read_filename(BIO *b, char *name)
21*2175Sjp161948 int BIO_write_filename(BIO *b, char *name)
22*2175Sjp161948 int BIO_append_filename(BIO *b, char *name)
23*2175Sjp161948 int BIO_rw_filename(BIO *b, char *name)
24*2175Sjp161948
25*2175Sjp161948=head1 DESCRIPTION
26*2175Sjp161948
27*2175Sjp161948BIO_s_file() returns the BIO file method. As its name implies it
28*2175Sjp161948is a wrapper round the stdio FILE structure and it is a
29*2175Sjp161948source/sink BIO.
30*2175Sjp161948
31*2175Sjp161948Calls to BIO_read() and BIO_write() read and write data to the
32*2175Sjp161948underlying stream. BIO_gets() and BIO_puts() are supported on file BIOs.
33*2175Sjp161948
34*2175Sjp161948BIO_flush() on a file BIO calls the fflush() function on the wrapped
35*2175Sjp161948stream.
36*2175Sjp161948
37*2175Sjp161948BIO_reset() attempts to change the file pointer to the start of file
38*2175Sjp161948using fseek(stream, 0, 0).
39*2175Sjp161948
40*2175Sjp161948BIO_seek() sets the file pointer to position B<ofs> from start of file
41*2175Sjp161948using fseek(stream, ofs, 0).
42*2175Sjp161948
43*2175Sjp161948BIO_eof() calls feof().
44*2175Sjp161948
45*2175Sjp161948Setting the BIO_CLOSE flag calls fclose() on the stream when the BIO
46*2175Sjp161948is freed.
47*2175Sjp161948
48*2175Sjp161948BIO_new_file() creates a new file BIO with mode B<mode> the meaning
49*2175Sjp161948of B<mode> is the same as the stdio function fopen(). The BIO_CLOSE
50*2175Sjp161948flag is set on the returned BIO.
51*2175Sjp161948
52*2175Sjp161948BIO_new_fp() creates a file BIO wrapping B<stream>. Flags can be:
53*2175Sjp161948BIO_CLOSE, BIO_NOCLOSE (the close flag) BIO_FP_TEXT (sets the underlying
54*2175Sjp161948stream to text mode, default is binary: this only has any effect under
55*2175Sjp161948Win32).
56*2175Sjp161948
57*2175Sjp161948BIO_set_fp() set the fp of a file BIO to B<fp>. B<flags> has the same
58*2175Sjp161948meaning as in BIO_new_fp(), it is a macro.
59*2175Sjp161948
60*2175Sjp161948BIO_get_fp() retrieves the fp of a file BIO, it is a macro.
61*2175Sjp161948
62*2175Sjp161948BIO_seek() is a macro that sets the position pointer to B<offset> bytes
63*2175Sjp161948from the start of file.
64*2175Sjp161948
65*2175Sjp161948BIO_tell() returns the value of the position pointer.
66*2175Sjp161948
67*2175Sjp161948BIO_read_filename(), BIO_write_filename(), BIO_append_filename() and
68*2175Sjp161948BIO_rw_filename() set the file BIO B<b> to use file B<name> for
69*2175Sjp161948reading, writing, append or read write respectively.
70*2175Sjp161948
71*2175Sjp161948=head1 NOTES
72*2175Sjp161948
73*2175Sjp161948When wrapping stdout, stdin or stderr the underlying stream should not
74*2175Sjp161948normally be closed so the BIO_NOCLOSE flag should be set.
75*2175Sjp161948
76*2175Sjp161948Because the file BIO calls the underlying stdio functions any quirks
77*2175Sjp161948in stdio behaviour will be mirrored by the corresponding BIO.
78*2175Sjp161948
79*2175Sjp161948=head1 EXAMPLES
80*2175Sjp161948
81*2175Sjp161948File BIO "hello world":
82*2175Sjp161948
83*2175Sjp161948 BIO *bio_out;
84*2175Sjp161948 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
85*2175Sjp161948 BIO_printf(bio_out, "Hello World\n");
86*2175Sjp161948
87*2175Sjp161948Alternative technique:
88*2175Sjp161948
89*2175Sjp161948 BIO *bio_out;
90*2175Sjp161948 bio_out = BIO_new(BIO_s_file());
91*2175Sjp161948 if(bio_out == NULL) /* Error ... */
92*2175Sjp161948 if(!BIO_set_fp(bio_out, stdout, BIO_NOCLOSE)) /* Error ... */
93*2175Sjp161948 BIO_printf(bio_out, "Hello World\n");
94*2175Sjp161948
95*2175Sjp161948Write to a file:
96*2175Sjp161948
97*2175Sjp161948 BIO *out;
98*2175Sjp161948 out = BIO_new_file("filename.txt", "w");
99*2175Sjp161948 if(!out) /* Error occurred */
100*2175Sjp161948 BIO_printf(out, "Hello World\n");
101*2175Sjp161948 BIO_free(out);
102*2175Sjp161948
103*2175Sjp161948Alternative technique:
104*2175Sjp161948
105*2175Sjp161948 BIO *out;
106*2175Sjp161948 out = BIO_new(BIO_s_file());
107*2175Sjp161948 if(out == NULL) /* Error ... */
108*2175Sjp161948 if(!BIO_write_filename(out, "filename.txt")) /* Error ... */
109*2175Sjp161948 BIO_printf(out, "Hello World\n");
110*2175Sjp161948 BIO_free(out);
111*2175Sjp161948
112*2175Sjp161948=head1 RETURN VALUES
113*2175Sjp161948
114*2175Sjp161948BIO_s_file() returns the file BIO method.
115*2175Sjp161948
116*2175Sjp161948BIO_new_file() and BIO_new_fp() return a file BIO or NULL if an error
117*2175Sjp161948occurred.
118*2175Sjp161948
119*2175Sjp161948BIO_set_fp() and BIO_get_fp() return 1 for success or 0 for failure
120*2175Sjp161948(although the current implementation never return 0).
121*2175Sjp161948
122*2175Sjp161948BIO_seek() returns the same value as the underlying fseek() function:
123*2175Sjp1619480 for success or -1 for failure.
124*2175Sjp161948
125*2175Sjp161948BIO_tell() returns the current file position.
126*2175Sjp161948
127*2175Sjp161948BIO_read_filename(), BIO_write_filename(),  BIO_append_filename() and
128*2175Sjp161948BIO_rw_filename() return 1 for success or 0 for failure.
129*2175Sjp161948
130*2175Sjp161948=head1 BUGS
131*2175Sjp161948
132*2175Sjp161948BIO_reset() and BIO_seek() are implemented using fseek() on the underlying
133*2175Sjp161948stream. The return value for fseek() is 0 for success or -1 if an error
134*2175Sjp161948occurred this differs from other types of BIO which will typically return
135*2175Sjp1619481 for success and a non positive value if an error occurred.
136*2175Sjp161948
137*2175Sjp161948=head1 SEE ALSO
138*2175Sjp161948
139*2175Sjp161948L<BIO_seek(3)|BIO_seek(3)>, L<BIO_tell(3)|BIO_tell(3)>,
140*2175Sjp161948L<BIO_reset(3)|BIO_reset(3)>, L<BIO_flush(3)|BIO_flush(3)>,
141*2175Sjp161948L<BIO_read(3)|BIO_read(3)>,
142*2175Sjp161948L<BIO_write(3)|BIO_write(3)>, L<BIO_puts(3)|BIO_puts(3)>,
143*2175Sjp161948L<BIO_gets(3)|BIO_gets(3)>, L<BIO_printf(3)|BIO_printf(3)>,
144*2175Sjp161948L<BIO_set_close(3)|BIO_set_close(3)>, L<BIO_get_close(3)|BIO_get_close(3)>
145