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  * and/or 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 /*
19 FUNCTION
20 <<fputs>>, <<fputs_unlocked>>---write a character string in a file or stream
21 
22 INDEX
23 	fputs
24 INDEX
25 	fputs_unlocked
26 INDEX
27 	_fputs_r
28 INDEX
29 	_fputs_unlocked_r
30 
31 SYNOPSIS
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( const char *restrict <[s]>, FILE *restrict <[fp]>);
41 
42 	#include <stdio.h>
43 	int fputs_unlocked( const char *restrict <[s]>, FILE *restrict <[fp]>);
44 
45 DESCRIPTION
46 <<fputs>> writes the string at <[s]> (but without the trailing null)
47 to 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
51 protected by flockfile() (or ftrylockfile()) and funlockfile().  This
52 function may safely be used in a multi-threaded program if and only
53 if they are called while the invoking thread owns the (FILE *)
54 object, as is the case after a successful call to the flockfile() or
55 ftrylockfile() 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
59 above that take an additional reentrant struct pointer argument: <[ptr]>.
60 
61 RETURNS
62 If successful, the result is <<0>>; otherwise, the result is <<EOF>>.
63 
64 PORTABILITY
65 ANSI C requires <<fputs>>, but does not specify that the result on
66 success must be <<0>>; any non-negative value is permitted.
67 
68 <<fputs_unlocked>> is a GNU extension.
69 
70 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
71 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
72 */
73 
74 #define _DEFAULT_SOURCE
75 #include <_ansi.h>
76 #include <stdio.h>
77 #include <string.h>
78 #include "fvwrite.h"
79 #include "local.h"
80 
81 #ifdef __IMPL_UNLOCKED__
82 #define _fputs_r _fputs_unlocked_r
83 #define fputs fputs_unlocked
84 #endif
85 
86 /*
87  * Write the given string to the given file.
88  */
89 int
fputs(char const * __restrict s,FILE * __restrict fp)90 fputs (
91        char const *__restrict s,
92        FILE *__restrict fp)
93 {
94 #ifdef _FVWRITE_IN_STREAMIO
95   int result;
96   struct __suio uio;
97   struct __siov iov;
98 
99   iov.iov_base = s;
100   iov.iov_len = uio.uio_resid = strlen (s);
101   uio.uio_iov = &iov;
102   uio.uio_iovcnt = 1;
103 
104   CHECK_INIT(ptr, fp);
105 
106   _newlib_flockfile_start (fp);
107   ORIENT (fp, -1);
108   result = _sfvwrite ( fp, &uio);
109   _newlib_flockfile_end (fp);
110   return result;
111 #else
112   const char *p = s;
113 
114   CHECK_INIT(ptr, fp);
115 
116   _newlib_flockfile_start (fp);
117   ORIENT (fp, -1);
118   /* Make sure we can write.  */
119   if (cantwrite (ptr, fp))
120     goto error;
121 
122   while (*p)
123     {
124       if (_sputc ( *p++, fp) == EOF)
125 	goto error;
126     }
127   _newlib_flockfile_exit (fp);
128   return 0;
129 
130 error:
131   _newlib_flockfile_end (fp);
132   return EOF;
133 #endif
134 }
135