~ubuntu-branches/ubuntu/feisty/avr-libc/feisty

« back to all changes in this revision

Viewing changes to libc/stdio/fdevopen.c

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2006-11-15 21:12:47 UTC
  • mfrom: (3.1.2 feisty)
  • Revision ID: james.westby@ubuntu.com-20061115211247-b7qhgnb6o49v5zsg
Tags: 1:1.4.5-2
* Convertion to debheler fixed (closes: #398220)
* Reference to /usr/share/common-licenses in copyright file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2002, Joerg Wunsch
 
1
/* Copyright (c) 2002,2005 Joerg Wunsch
2
2
   All rights reserved.
3
3
 
4
4
   Redistribution and use in source and binary forms, with or without
27
27
  POSSIBILITY OF SUCH DAMAGE.
28
28
*/
29
29
 
30
 
/* $Id: fdevopen.c,v 1.4 2004/12/22 22:08:23 joerg_wunsch Exp $ */
 
30
/* $Id: fdevopen.c,v 1.6 2005/09/14 19:04:58 joerg_wunsch Exp $ */
31
31
 
32
 
#include <inttypes.h>
 
32
#include <stdint.h>
33
33
#include <stdio.h>
34
34
#include <stdlib.h>
35
35
 
36
36
#include "stdio_private.h"
37
37
 
38
 
FILE *__iob[3];                 /* stdin, stdout, stderr */
39
 
 
40
38
/** \ingroup avr_stdio
41
39
   This function is a replacement for \c fopen().
42
40
 
49
47
   insufficient dynamic memory is available to establish a new stream.
50
48
 
51
49
   If the \c put function pointer is provided, the stream is opened
52
 
   with write intent.  The function passed as \c put shall take one
53
 
   character to write to the device as argument , and shall return 0
 
50
   with write intent.  The function passed as \c put shall take two
 
51
   arguments, the first a character to write to the device,
 
52
   and the second a pointer to FILE, and shall return 0
54
53
   if the output was successful, and a nonzero value if the character
55
54
   could not be sent to the device.
56
55
 
57
56
   If the \c get function pointer is provided, the stream is opened
58
 
   with read intent.  The function passed as \c get shall take no
59
 
   arguments, and return one character from the device, passed as an
 
57
   with read intent.  The function passed as \c get shall take
 
58
   a pointer to FILE as its single argument,
 
59
   and return one character from the device, passed as an
60
60
   \c int type.  If an error occurs when trying to read from the
61
 
   device, it shall return \c -1.
 
61
   device, it shall return \c _FDEV_ERR.
 
62
   If an end-of-file condition was reached while reading from the
 
63
   device, \c _FDEV_EOF shall be returned.
62
64
 
63
65
   If both functions are provided, the stream is opened with read
64
66
   and write intent.
67
69
   and the first one opened with write intent is assigned to both,
68
70
   \c stdout and \c stderr.
69
71
 
70
 
   The third parameter \c opts is currently unused, but reserved for
71
 
   future extensions.
72
 
 
73
72
   fdevopen() uses calloc() (und thus malloc()) in order to allocate
74
73
   the storage for the new stream.
 
74
 
 
75
   \note If the macro __STDIO_FDEVOPEN_COMPAT_12 is declared before
 
76
   including <stdio.h>, a function prototype for fdevopen() will be
 
77
   chosen that is backwards compatible with avr-libc version 1.2 and
 
78
   before.  This is solely intented for providing a simple migration
 
79
   path without the need to immediately change all source code.  Do
 
80
   not use for new code.
75
81
*/
76
82
 
77
83
FILE *
78
 
fdevopen(int (*put)(char), int (*get)(void), int opts __attribute__((unused)))
 
84
fdevopen(int (*put)(char, FILE *), int (*get)(FILE *))
79
85
{
80
86
        FILE *s;
81
87
 
85
91
        if ((s = calloc(1, sizeof(FILE))) == 0)
86
92
                return 0;
87
93
 
 
94
        s->flags = __SMALLOC;
 
95
 
88
96
        if (get != 0) {
89
97
                s->get = get;
90
 
                s->flags = __SRD;
 
98
                s->flags |= __SRD;
91
99
                if (stdin == 0)
92
100
                        stdin = s;
93
101
        }