source: trunk/libs/newlib/src/newlib/libc/stdio/fputs.c @ 620

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

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

File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley.  The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18/*
19FUNCTION
20<<fputs>>, <<fputs_unlocked>>---write a character string in a file or stream
21
22INDEX
23        fputs
24INDEX
25        fputs_unlocked
26INDEX
27        _fputs_r
28INDEX
29        _fputs_unlocked_r
30
31SYNOPSIS
32        #include <stdio.h>
33        int fputs(const char *restrict <[s]>, FILE *restrict <[fp]>);
34
35        #define _GNU_SOURCE
36        #include <stdio.h>
37        int fputs_unlocked(const char *restrict <[s]>, FILE *restrict <[fp]>);
38
39        #include <stdio.h>
40        int _fputs_r(struct _reent *<[ptr]>, const char *restrict <[s]>, FILE *restrict <[fp]>);
41
42        #include <stdio.h>
43        int _fputs_unlocked_r(struct _reent *<[ptr]>, const char *restrict <[s]>, FILE *restrict <[fp]>);
44
45DESCRIPTION
46<<fputs>> writes the string at <[s]> (but without the trailing null)
47to the file or stream identified by <[fp]>.
48
49<<fputs_unlocked>> is a non-thread-safe version of <<fputs>>.
50<<fputs_unlocked>> may only safely be used within a scope
51protected by flockfile() (or ftrylockfile()) and funlockfile().  This
52function may safely be used in a multi-threaded program if and only
53if they are called while the invoking thread owns the (FILE *)
54object, as is the case after a successful call to the flockfile() or
55ftrylockfile() functions.  If threads are disabled, then
56<<fputs_unlocked>> is equivalent to <<fputs>>.
57
58<<_fputs_r>> and <<_fputs_unlocked_r>> are simply reentrant versions of the
59above that take an additional reentrant struct pointer argument: <[ptr]>.
60
61RETURNS
62If successful, the result is <<0>>; otherwise, the result is <<EOF>>.
63
64PORTABILITY
65ANSI C requires <<fputs>>, but does not specify that the result on
66success must be <<0>>; any non-negative value is permitted.
67
68<<fputs_unlocked>> is a GNU extension.
69
70Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
71<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
72*/
73
74#include <_ansi.h>
75#include <stdio.h>
76#include <string.h>
77#include "fvwrite.h"
78#include "local.h"
79
80#ifdef __IMPL_UNLOCKED__
81#define _fputs_r _fputs_unlocked_r
82#define fputs fputs_unlocked
83#endif
84
85/*
86 * Write the given string to the given file.
87 */
88int
89_fputs_r (struct _reent * ptr,
90       char const *__restrict s,
91       FILE *__restrict fp)
92{
93#ifdef _FVWRITE_IN_STREAMIO
94  int result;
95  struct __suio uio;
96  struct __siov iov;
97
98  iov.iov_base = s;
99  iov.iov_len = uio.uio_resid = strlen (s);
100  uio.uio_iov = &iov;
101  uio.uio_iovcnt = 1;
102
103  CHECK_INIT(ptr, fp);
104
105  _newlib_flockfile_start (fp);
106  ORIENT (fp, -1);
107  result = __sfvwrite_r (ptr, fp, &uio);
108  _newlib_flockfile_end (fp);
109  return result;
110#else
111  const char *p = s;
112
113  CHECK_INIT(ptr, fp);
114
115  _newlib_flockfile_start (fp);
116  ORIENT (fp, -1);
117  /* Make sure we can write.  */
118  if (cantwrite (ptr, fp))
119    goto error;
120
121  while (*p)
122    {
123      if (__sputc_r (ptr, *p++, fp) == EOF)
124        goto error;
125    }
126  _newlib_flockfile_exit (fp);
127  return 0;
128
129error:
130  _newlib_flockfile_end (fp);
131  return EOF;
132#endif
133}
134
135#ifndef _REENT_ONLY
136int
137fputs (char const *__restrict s,
138       FILE *__restrict fp)
139{
140  return _fputs_r (_REENT, s, fp);
141}
142#endif /* !_REENT_ONLY */
Note: See TracBrowser for help on using the repository browser.