os/ossrv/genericopenlibs/cstdlib/LSTDIO/PUTS.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* PUTS.C
     2  * 
     3  * Portions Copyright (c) 1990-2006 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 /*
     8  * Copyright (c) 1990 The Regents of the University of California.
     9  * All rights reserved.
    10  *
    11  * Redistribution and use in source and binary forms are permitted
    12  * provided that the above copyright notice and this paragraph are
    13  * duplicated in all such forms and that any documentation,
    14  * advertising materials, and other materials related to such
    15  * distribution and use acknowledge that the software was developed
    16  * by the University of California, Berkeley.  The name of the
    17  * University may not be used to endorse or promote products derived
    18  * from this software without specific prior written permission.
    19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    22  */
    23 
    24 /*
    25 FUNCTION
    26 <<puts>>---write a character string
    27 
    28 INDEX
    29 	puts
    30 INDEX
    31 	_puts_r
    32 
    33 ANSI_SYNOPSIS
    34 	#include <stdio.h>
    35 	int puts(const char *<[s]>);
    36 
    37 	int _puts_r(void *<[reent]>, const char *<[s]>);
    38 
    39 TRAD_SYNOPSIS
    40 	#include <stdio.h>
    41 	int puts(<[s]>)
    42 	char *<[s]>;
    43 
    44 	int _puts_r(<[reent]>, <[s]>)
    45 	char *<[reent]>;
    46 	char *<[s]>;
    47 
    48 DESCRIPTION
    49 <<puts>> writes the string at <[s]> (followed by a newline, instead of
    50 the trailing null) to the standard output stream.
    51 
    52 The alternate function <<_puts_r>> is a reentrant version.  The extra
    53 argument <[reent]> is a pointer to a reentrancy structure.
    54 
    55 RETURNS
    56 If successful, the result is a nonnegative integer; otherwise, the
    57 result is <<EOF>>.
    58 
    59 PORTABILITY
    60 ANSI C requires <<puts>>, but does not specify that the result on
    61 success must be <<0>>; any non-negative value is permitted.
    62 
    63 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
    64 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.  */
    65 
    66 #include <stdio_r.h>
    67 #include <string.h>
    68 #include "FVWRITE.H"
    69 #include "LOCAL.H"
    70 
    71 /**
    72 Writes the given string to stdout, appending a newline.
    73 @return
    74 @param 
    75 */
    76 EXPORT_C int
    77 _puts_r (struct _reent *ptr, const char * s)
    78 {
    79   size_t c = strlen (s);
    80   struct __suio uio;
    81   struct __siov iov[2];
    82 
    83   iov[0].iov_base = s;
    84   iov[0].iov_len = c;
    85   iov[1].iov_base = "\n";
    86   iov[1].iov_len = 1;
    87   uio.uio_resid = c + 1;
    88   uio.uio_iov = &iov[0];
    89   uio.uio_iovcnt = 2;
    90 
    91   return (__sfvwrite (_stdout_r (ptr), &uio) ? EOF : '\n');
    92 }
    93 
    94 #ifndef _REENT_ONLY
    95 /**
    96 Outputs a string to stdout. Copies the string to standard output stream
    97 and appends a new line character (\n).
    98 
    99 @param Null-terminated string to be outputed.
   100 
   101 @return On Success, a non-negative value is returned.
   102 		On Failure, EOF value is returned and errno may be set.
   103 */
   104 EXPORT_C int
   105 puts (char const * s)
   106 {
   107   struct _reent *r = _REENT2;
   108   if (!r)
   109 	return EOF; // Memory for library globals is not allocated (errno not set).
   110   return _puts_r (r, s);
   111 }
   112 
   113 #endif