Subversion Repositories Kolibri OS

Rev

Rev 6074 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. /*
  19. FUNCTION
  20. <<puts>>---write a character string
  21.  
  22. INDEX
  23.         puts
  24. INDEX
  25.         _puts_r
  26.  
  27. ANSI_SYNOPSIS
  28.         #include <stdio.h>
  29.         int puts(const char *<[s]>);
  30.  
  31.         int _puts_r(struct _reent *<[reent]>, const char *<[s]>);
  32.  
  33. TRAD_SYNOPSIS
  34.         #include <stdio.h>
  35.         int puts(<[s]>)
  36.         char *<[s]>;
  37.  
  38.         int _puts_r(<[reent]>, <[s]>)
  39.         struct _reent *<[reent]>;
  40.         char *<[s]>;
  41.  
  42. DESCRIPTION
  43. <<puts>> writes the string at <[s]> (followed by a newline, instead of
  44. the trailing null) to the standard output stream.
  45.  
  46. The alternate function <<_puts_r>> is a reentrant version.  The extra
  47. argument <[reent]> is a pointer to a reentrancy structure.
  48.  
  49. RETURNS
  50. If successful, the result is a nonnegative integer; otherwise, the
  51. result is <<EOF>>.
  52.  
  53. PORTABILITY
  54. ANSI C requires <<puts>>, but does not specify that the result on
  55. success must be <<0>>; any non-negative value is permitted.
  56.  
  57. Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
  58. <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
  59. */
  60.  
  61. #if defined(LIBC_SCCS) && !defined(lint)
  62. static char sccsid[] = "%W% (Berkeley) %G%";
  63. #endif /* LIBC_SCCS and not lint */
  64.  
  65. #include <_ansi.h>
  66. #include <reent.h>
  67. #include <stdio.h>
  68. #include <string.h>
  69. #include "fvwrite.h"
  70. #include "local.h"
  71.  
  72. /*
  73.  * Write the given string to stdout, appending a newline.
  74.  */
  75.  
  76. int
  77. _DEFUN(_puts_r, (ptr, s),
  78.        struct _reent *ptr _AND
  79.        _CONST char * s)
  80. {
  81. #ifdef _FVWRITE_IN_STREAMIO
  82.   int result;
  83.   size_t c = strlen (s);
  84.   struct __suio uio;
  85.   struct __siov iov[2];
  86.   FILE *fp;
  87.  
  88.   iov[0].iov_base = s;
  89.   iov[0].iov_len = c;
  90.   iov[1].iov_base = "\n";
  91.   iov[1].iov_len = 1;
  92.   uio.uio_resid = c + 1;
  93.   uio.uio_iov = &iov[0];
  94.   uio.uio_iovcnt = 2;
  95.  
  96.   _REENT_SMALL_CHECK_INIT (ptr);
  97.   fp = _stdout_r (ptr);
  98.   CHECK_INIT (ptr, fp);
  99.   _newlib_flockfile_start (fp);
  100.   ORIENT (fp, -1);
  101.   result = (__sfvwrite_r (ptr, fp, &uio) ? EOF : '\n');
  102.   _newlib_flockfile_end (fp);
  103.   return result;
  104. #else
  105.   int result = EOF;
  106.   const char *p = s;
  107.   FILE *fp;
  108.   _REENT_SMALL_CHECK_INIT (ptr);
  109.  
  110.   fp = _stdout_r (ptr);
  111.   CHECK_INIT (ptr, fp);
  112.   _newlib_flockfile_start (fp);
  113.   ORIENT (fp, -1);
  114.   /* Make sure we can write.  */
  115.   if (cantwrite (ptr, fp))
  116.     goto err;
  117.  
  118.   while (*p)
  119.     {
  120.       if (__sputc_r (ptr, *p++, fp) == EOF)
  121.         goto err;
  122.     }
  123.   if (__sputc_r (ptr, '\n', fp) == EOF)
  124.     goto err;
  125.  
  126.   result = '\n';
  127.  
  128. err:
  129.   _newlib_flockfile_end (fp);
  130.   return result;
  131. #endif
  132. }
  133.  
  134. #ifndef _REENT_ONLY
  135.  
  136. int
  137. _DEFUN(puts, (s),
  138.        char _CONST * s)
  139. {
  140.   return _puts_r (_REENT, s);
  141. }
  142.  
  143. #endif
  144.