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 "stdio_private.h"
33 
34 /** \file */
35 
36 /** \ingroup avr_stdio
37    This function is a replacement for \c fopen().
38 
39    It opens a stream for a device where the actual device
40    implementation needs to be provided by the application.  If
41    successful, a pointer to the structure for the opened stream is
42    returned.  Reasons for a possible failure currently include that
43    neither the \c put nor the \c get argument have been provided, thus
44    attempting to open a stream with no IO intent at all, or that
45    insufficient dynamic memory is available to establish a new stream.
46 
47    If the \c put function pointer is provided, the stream is opened
48    with write intent.  The function passed as \c put shall take two
49    arguments, the first a character to write to the device,
50    and the second a pointer to FILE, and shall return 0
51    if the output was successful, and a nonzero value if the character
52    could not be sent to the device.
53 
54    If the \c get function pointer is provided, the stream is opened
55    with read intent.  The function passed as \c get shall take
56    a pointer to FILE as its single argument,
57    and return one character from the device, passed as an
58    \c int type.  If an error occurs when trying to read from the
59    device, it shall return \c _FDEV_ERR.
60    If an end-of-file condition was reached while reading from the
61    device, \c _FDEV_EOF shall be returned.
62 
63    If both functions are provided, the stream is opened with read
64    and write intent.
65 
66    If the \c flush function pointer is provided, then it will be
67    called whenever the application calls the fflush function on the
68    file. This allows the underlying I/O implementation to perform
69    buffering as needed.
70 
71    fdevopen() uses calloc() (und thus malloc()) in order to allocate
72    the storage for the new stream.
73 
74    \note If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before
75    including <stdio.h>, a function prototype for fdevopen() will be
76    chosen that is backwards compatible with avr-libc version 1.2 and
77    before.  This is solely intented for providing a simple migration
78    path without the need to immediately change all source code.  Do
79    not use for new code.
80 */
81 
82 static int
fdevclose(FILE * f)83 fdevclose(FILE *f)
84 {
85 	int ret = 0;
86 	if  (f->flush)
87 		ret = (*f->flush)(f);
88 	free(f);
89 	return ret;
90 }
91 
92 FILE *
fdevopen(int (* put)(char,FILE *),int (* get)(FILE *),int (* flush)(FILE *))93 fdevopen(int (*put)(char, FILE *), int (*get)(FILE *), int (*flush)(FILE *))
94 {
95 	struct __file_close *cf;
96 
97 	if (put == 0 && get == 0)
98 		return 0;
99 
100 	if ((cf = calloc(1, sizeof(*cf))) == 0)
101 		return 0;
102 
103 	cf->file.flags = __SCLOSE;
104 	cf->close = fdevclose;
105 
106 	if (get != 0) {
107 		cf->file.get = get;
108 		cf->file.flags |= __SRD;
109 	}
110 
111 	if (put != 0) {
112 		cf->file.put = put;
113 		cf->file.flush = flush;
114 		cf->file.flags |= __SWR;
115 	}
116 
117 	return (FILE *) cf;
118 }
119