1 /* Copyright (c) 2002,2005, 2007 Joerg Wunsch
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of the copyright holders nor the names of
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /* $Id: fdevopen.c 1944 2009-04-01 23:12:20Z arcanum $ */
31
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "stdio_private.h"
36
37 /** \file */
38
39 /** \ingroup avr_stdio
40 This function is a replacement for \c fopen().
41
42 It opens a stream for a device where the actual device
43 implementation needs to be provided by the application. If
44 successful, a pointer to the structure for the opened stream is
45 returned. Reasons for a possible failure currently include that
46 neither the \c put nor the \c get argument have been provided, thus
47 attempting to open a stream with no IO intent at all, or that
48 insufficient dynamic memory is available to establish a new stream.
49
50 If the \c put function pointer is provided, the stream is opened
51 with write intent. The function passed as \c put shall take two
52 arguments, the first a character to write to the device,
53 and the second a pointer to FILE, and shall return 0
54 if the output was successful, and a nonzero value if the character
55 could not be sent to the device.
56
57 If the \c get function pointer is provided, the stream is opened
58 with read intent. The function passed as \c get shall take
59 a pointer to FILE as its single argument,
60 and return one character from the device, passed as an
61 \c int type. If an error occurs when trying to read from the
62 device, it shall return \c _FDEV_ERR.
63 If an end-of-file condition was reached while reading from the
64 device, \c _FDEV_EOF shall be returned.
65
66 If both functions are provided, the stream is opened with read
67 and write intent.
68
69 If the \c flush function pointer is provided, then it will be
70 called whenever the application calls the fflush function on the
71 file. This allows the underlying I/O implementation to perform
72 buffering as needed.
73
74 fdevopen() uses calloc() (und thus malloc()) in order to allocate
75 the storage for the new stream.
76
77 \note If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before
78 including <stdio.h>, a function prototype for fdevopen() will be
79 chosen that is backwards compatible with avr-libc version 1.2 and
80 before. This is solely intented for providing a simple migration
81 path without the need to immediately change all source code. Do
82 not use for new code.
83 */
84
85 static int
fdevclose(FILE * f)86 fdevclose(FILE *f)
87 {
88 int ret = 0;
89 if (f->flush)
90 ret = (*f->flush)(f);
91 free(f);
92 return ret;
93 }
94
95 FILE *
fdevopen(int (* put)(char,FILE *),int (* get)(FILE *),int (* flush)(FILE *))96 fdevopen(int (*put)(char, FILE *), int (*get)(FILE *), int (*flush)(FILE *))
97 {
98 struct __file_close *cf;
99
100 if (put == 0 && get == 0)
101 return 0;
102
103 if ((cf = calloc(1, sizeof(*cf))) == 0)
104 return 0;
105
106 cf->file.flags = __SCLOSE;
107 cf->close = fdevclose;
108
109 if (get != 0) {
110 cf->file.get = get;
111 cf->file.flags |= __SRD;
112 }
113
114 if (put != 0) {
115 cf->file.put = put;
116 cf->file.flush = flush;
117 cf->file.flags |= __SWR;
118 }
119
120 return &(cf->file);
121 }
122