source: trunk/libs/newlib/src/newlib/libc/stdio/wsetup.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.5 KB
Line 
1/* No user fns here. Pesch 15apr92. */
2
3/*
4 * Copyright (c) 1990, 2007 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by the University of California, Berkeley.  The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 */
19
20#include <_ansi.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <errno.h>
24#include "local.h"
25
26/*
27 * Various output routines call wsetup to be sure it is safe to write,
28 * because either _flags does not include __SWR, or _buf is NULL.
29 * _wsetup returns 0 if OK to write, nonzero and set errno otherwise.
30 */
31
32int
33__swsetup_r (struct _reent *ptr,
34       register FILE * fp)
35{
36  /* Make sure stdio is set up.  */
37
38  CHECK_INIT (_REENT, fp);
39
40  /*
41   * If we are not writing, we had better be reading and writing.
42   */
43
44  if ((fp->_flags & __SWR) == 0)
45    {
46      if ((fp->_flags & __SRW) == 0)
47        {
48          ptr->_errno = EBADF;
49          fp->_flags |= __SERR;
50          return EOF;
51        }
52      if (fp->_flags & __SRD)
53        {
54          /* clobber any ungetc data */
55          if (HASUB (fp))
56            FREEUB (ptr, fp);
57          fp->_flags &= ~(__SRD | __SEOF);
58          fp->_r = 0;
59          fp->_p = fp->_bf._base;
60        }
61      fp->_flags |= __SWR;
62    }
63
64  /*
65   * Make a buffer if necessary, then set _w.
66   * A string I/O file should not explicitly allocate a buffer
67   * unless asprintf is being used.
68   */
69  if (fp->_bf._base == NULL
70        && (!(fp->_flags & __SSTR) || (fp->_flags & __SMBF)))
71    __smakebuf_r (ptr, fp);
72
73  if (fp->_flags & __SLBF)
74    {
75      /*
76       * It is line buffered, so make _lbfsize be -_bufsize
77       * for the putc() macro.  We will change _lbfsize back
78       * to 0 whenever we turn off __SWR.
79       */
80      fp->_w = 0;
81      fp->_lbfsize = -fp->_bf._size;
82    }
83  else
84    fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size;
85
86  if (!fp->_bf._base && (fp->_flags & __SMBF))
87    {
88      /* __smakebuf_r set errno, but not flag */
89      fp->_flags |= __SERR;
90      return EOF;
91    }
92  return 0;
93}
Note: See TracBrowser for help on using the repository browser.