Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of gets_s().
  28. *
  29. ****************************************************************************/
  30.  
  31. #include "variety.h"
  32. #include "saferlib.h"
  33. #include <stdio.h>
  34.  
  35. _WCRTLINK extern char *gets_s( char *s, rsize_t n )
  36. /*************************************************/
  37. {
  38.     int         c;
  39.     char        *cs;
  40.     rsize_t     len;
  41.     unsigned    oflag;
  42.  
  43.     /* Check runtime constraint: s shall not be a null pointer */
  44.     if( ! __check_constraint_nullptr( s ) ) {
  45.         /* Just trigger the constraint handler if necessary */
  46.     }
  47.  
  48.     /* Check runtime constraint: n shall not be zero nor
  49.        greater than RSIZE_MAX */
  50.     if(  ! __check_constraint_zero( n )
  51.        || ! __check_constraint_maxsize( n ) ) {
  52.         n = 0;
  53.     }
  54.  
  55.     /* Filter out stream error and EOF status during this function */
  56.     oflag = stdin->_flag & (_SFERR | _EOF);
  57.     stdin->_flag &= ~(_SFERR | _EOF);
  58.  
  59.     cs = s;
  60.     len = n;
  61.  
  62.     /*
  63.      Read data until any of following conditions is met:
  64.      - Received EOF
  65.      - Read error
  66.      - New line character received
  67.      */
  68.     while( (c = getc( stdin )) != EOF
  69.         && (c != '\n') ) {
  70.         if( cs && len ) {
  71.             *cs++ = c;
  72.             len--;
  73.         }
  74.     }
  75.  
  76.     /* Post-process runtime constraints, return NULL when catched */
  77.     if( s == NULL || n == 0) {
  78.         stdin->_flag |= oflag;
  79.         return( NULL );
  80.     }
  81.  
  82.     /* After this point, it is assumed that s is valid and
  83.        contains at least one character */
  84.  
  85.     /* Catch too small buffer and I/O error */
  86.     if( ! __check_constraint_toosmall( s, len )
  87.       || ferror( stdin ) ) {
  88.         stdin->_flag |= oflag;
  89.         *s = 0;
  90.         return( NULL );
  91.     }
  92.  
  93.     /* Restore stream error states if they were present previously */
  94.     stdin->_flag |= oflag;
  95.  
  96.     /* If we got EOF and didn't get any characters, return NULL */
  97.     if( c == EOF && cs == s ) {
  98.         *s = 0;
  99.         return( NULL );
  100.     }
  101.  
  102.     /* Terminate string */
  103.     *cs = 0;
  104.  
  105.     return( s );
  106. }
  107.