1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A
1N/A/*
1N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/* Copyright (c) 1988 AT&T */
1N/A/* All Rights Reserved */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include "lint.h"
1N/A#include "file64.h"
1N/A#include "mtlib.h"
1N/A#include <sys/types.h>
1N/A#include <stdio.h>
1N/A#include <memory.h>
1N/A#include <thread.h>
1N/A#include <synch.h>
1N/A#include <errno.h>
1N/A#include "stdiom.h"
1N/A#include "mse.h"
1N/A
1N/A/* read a single line from stdin, replace the '\n' with '\0' */
1N/Achar *
1N/Agets(char *buf)
1N/A{
1N/A char *ptr = buf;
1N/A ssize_t n;
1N/A char *p;
1N/A Uchar *bufend;
1N/A rmutex_t *lk;
1N/A
1N/A FLOCKFILE(lk, stdin);
1N/A
1N/A _SET_ORIENTATION_BYTE(stdin);
1N/A
1N/A if (!(stdin->_flag & (_IOREAD | _IORW))) {
1N/A errno = EBADF;
1N/A FUNLOCKFILE(lk);
1N/A return (0);
1N/A }
1N/A
1N/A if (stdin->_base == NULL) {
1N/A if ((bufend = _findbuf(stdin)) == 0) {
1N/A FUNLOCKFILE(lk);
1N/A return (0);
1N/A }
1N/A }
1N/A else
1N/A bufend = _bufend(stdin);
1N/A
1N/A for (;;) /* until get a '\n' */
1N/A {
1N/A if (stdin->_cnt <= 0) /* empty buffer */
1N/A {
1N/A if (__filbuf(stdin) != EOF) {
1N/A stdin->_ptr--; /* put back the character */
1N/A stdin->_cnt++;
1N/A } else if (ptr == buf) { /* never read anything */
1N/A FUNLOCKFILE(lk);
1N/A return (0);
1N/A } else
1N/A break; /* nothing left to read */
1N/A }
1N/A n = stdin->_cnt;
1N/A if ((p = (char *)memccpy(ptr, (char *)stdin->_ptr, '\n',
1N/A (size_t)n)) != 0)
1N/A n = p - ptr;
1N/A ptr += n;
1N/A stdin->_cnt -= n;
1N/A stdin->_ptr += n;
1N/A if (_needsync(stdin, bufend))
1N/A _bufsync(stdin, bufend);
1N/A if (p != 0) /* found a '\n' */
1N/A {
1N/A ptr--; /* step back over the '\n' */
1N/A break;
1N/A }
1N/A }
1N/A *ptr = '\0';
1N/A FUNLOCKFILE(lk);
1N/A return (buf);
1N/A}
1N/A