stdlib/public/stubs/Stubs.cpp
__swift_ssize_t
swift_stdlib_readLine_stdin(unsigned char **LinePtr) {
#if defined(_WIN32)
  if (LinePtr == nullptr)
    return -1;

  __swift_ssize_t Capacity = 0;
  __swift_ssize_t Pos = 0;
  unsigned char *ReadBuf = nullptr;

  _lock_file(stdin);

  for (;;) {
    int ch = _fgetc_nolock(stdin);

    if (ferror(stdin) || (ch == EOF && Pos == 0)) {
      if (ReadBuf)
        free(ReadBuf);
      _unlock_file(stdin);
      return -1;
    }

    if (Capacity - Pos <= 1) {
      // Capacity changes to 128, 128*2, 128*4, 128*8, ...
      Capacity = Capacity ? Capacity * 2 : 128;
      unsigned char *NextReadBuf =
          static_cast<unsigned char *>(realloc(ReadBuf, Capacity));
      if (NextReadBuf == nullptr) {
        if (ReadBuf)
          free(ReadBuf);
        _unlock_file(stdin);
        return -1;
      }
      ReadBuf = NextReadBuf;
    }

    if (ch == EOF)
      break;
    ReadBuf[Pos++] = ch;
    if (ch == '\n')
      break;
  }

  ReadBuf[Pos] = '\0';
  *LinePtr = ReadBuf;
  _unlock_file(stdin);
  return Pos;
#else
  size_t Capacity = 0;
  int result;
  do {
    result = getline((char **)LinePtr, &Capacity, stdin);
  } while (result < 0 && errno == EINTR);
  return result;
#endif
}
/// \param[out] LinePtr Replaced with the pointer to the malloc()-allocated
/// line.  Can be NULL if no characters were read. This buffer should be
/// freed by the caller.
///
/// \returns Size of character data returned in \c LinePtr, or -1
/// if an error occurred, or EOF was reached.
\param[out] LinePtr Replaced with the pointer to the malloc()-allocated
line.  Can be NULL if no characters were read. This buffer should be
freed by the caller.

\returns Size of character data returned in \c LinePtr, or -1
if an error occurred, or EOF was reached.
\ param [ out ] LinePtr Replaced with the pointer to the malloc () - allocated line . Can be NULL if no characters were read . This buffer should be freed by the caller . \ returns Size of character data returned in \ c LinePtr or - 1 if an error occurred or EOF was reached .
__swift_ssize_t
swift_stdlib_readLine_stdin(unsigned char **LinePtr) {
#if defined(_WIN32)
  if (LinePtr == nullptr)
    return -1;
  __swift_ssize_t Capacity = 0;
  __swift_ssize_t Pos = 0;
  unsigned char *ReadBuf = nullptr;
  _lock_file(stdin);
  for (;;) {
    int ch = _fgetc_nolock(stdin);
    if (ferror(stdin) || (ch == EOF && Pos == 0)) {
      if (ReadBuf)
        free(ReadBuf);
      _unlock_file(stdin);
      return -1;
    }
    if (Capacity - Pos <= 1) {
      Capacity = Capacity ? Capacity * 2 : 128;
      unsigned char *NextReadBuf =
          static_cast<unsigned char *>(realloc(ReadBuf, Capacity));
      if (NextReadBuf == nullptr) {
        if (ReadBuf)
          free(ReadBuf);
        _unlock_file(stdin);
        return -1;
      }
      ReadBuf = NextReadBuf;
    }
    if (ch == EOF)
      break;
    ReadBuf[Pos++] = ch;
    if (ch == '\n')
      break;
  }
  ReadBuf[Pos] = '\0';
  *LinePtr = ReadBuf;
  _unlock_file(stdin);
  return Pos;
#else
  size_t Capacity = 0;
  int result;
  do {
    result = getline((char **)LinePtr, &Capacity, stdin);
  } while (result < 0 && errno == EINTR);
  return result;
#endif
}
__swift_ssize_t swift_stdlib_readLine_stdin ( unsigned char * * LinePtr ) { #if defined ( _WIN32 ) if ( LinePtr == nullptr ) return -1 ; __swift_ssize_t Capacity = 0 ; __swift_ssize_t Pos = 0 ; unsigned char * ReadBuf = nullptr ; _lock_file ( stdin ) ; for ( ; ; ) { int ch = _fgetc_nolock ( stdin ) ; if ( ferror ( stdin ) || ( ch == EOF && Pos == 0 ) ) { if ( ReadBuf ) free ( ReadBuf ) ; _unlock_file ( stdin ) ; return -1 ; } if ( Capacity - Pos <= 1 ) { Capacity = Capacity ? Capacity * 2 : 128 ; unsigned char * NextReadBuf = static_cast < unsigned char * > ( realloc ( ReadBuf , Capacity ) ) ; if ( NextReadBuf == nullptr ) { if ( ReadBuf ) free ( ReadBuf ) ; _unlock_file ( stdin ) ; return -1 ; } ReadBuf = NextReadBuf ; } if ( ch == EOF ) break ; ReadBuf [ Pos ++ ] = ch ; if ( ch == ' \n ' ) break ; } ReadBuf [ Pos ] = ' \0 ' ; * LinePtr = ReadBuf ; _unlock_file ( stdin ) ; return Pos ; #else size_t Capacity = 0 ; int result ; do { result = getline ( ( char * * ) LinePtr , & Capacity , stdin ) ; } while ( result < 0 && errno == EINTR ) ; return result ; #endif }
\param[out] LinePtr Replaced with the pointer to the malloc()-allocated
line.
\ param [ out ] LinePtr Replaced with the pointer to the malloc () - allocated line .
// Capacity changes to 128, 128*2, 128*4, 128*8, ...