Files
seta75D 7c4988eac0 Init
2021-10-11 19:38:01 -03:00

82 lines
1.6 KiB
C
Executable File

/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
#ident "@(#)gets.c 1.9 92/09/23 SMI" /* SVr4.0 3.13 */
/*LINTLIBRARY*/
#include "synonyms.h"
#include "shlib.h"
#include <stdio.h>
#include "stdiom.h"
#include <memory.h>
#include <thread.h>
#include <synch.h>
#include <mtlib.h>
#include <errno.h>
char *
gets(buf) /* read a single line from stdin, replace the '\n' with '\0' */
char *buf;
{
register char *ptr = buf;
register int n;
register char *p;
register Uchar *bufend;
#ifdef _REENTRANT
rmutex_t *lk;
#endif _REENTRANT
if (!(stdin->_flag & (_IOREAD | _IORW))) {
errno = EBADF;
return 0;
}
FLOCKFILE(lk, stdin);
if (stdin->_base == 0)
{
if ((bufend = _findbuf(stdin)) == 0) {
FUNLOCKFILE(lk);
return 0;
}
}
else
bufend = _bufend(stdin);
for (;;) /* until get a '\n' */
{
if (stdin->_cnt <= 0) /* empty buffer */
{
if (_filbuf(stdin) != EOF)
{
stdin->_ptr--; /* put back the character */
stdin->_cnt++;
}
else if (ptr == buf) { /* never read anything */
FUNLOCKFILE(lk);
return 0;
}
else
break; /* nothing left to read */
}
n = stdin->_cnt;
if ((p = (char *)memccpy(ptr, (char *)stdin->_ptr, '\n', n)) != 0)
n = p - ptr;
ptr += n;
stdin->_cnt -= n;
stdin->_ptr += n;
if (_needsync(stdin, bufend))
_bufsync(stdin, bufend);
if (p != 0) /* found a '\n' */
{
ptr--; /* step back over the '\n' */
break;
}
}
*ptr = '\0';
FUNLOCKFILE(lk);
return buf;
}