1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* Copyright (c) 1988 AT&T */ 30 /* All Rights Reserved */ 31 32 33 #include "synonyms.h" 34 #include "file64.h" 35 #include "mtlib.h" 36 #include <sys/types.h> 37 #include <stdio.h> 38 #include <memory.h> 39 #include <thread.h> 40 #include <synch.h> 41 #include <errno.h> 42 #include "stdiom.h" 43 #include "mse.h" 44 45 /* read a single line from stdin, replace the '\n' with '\0' */ 46 char * 47 gets(char *buf) 48 { 49 char *ptr = buf; 50 ssize_t n; 51 char *p; 52 Uchar *bufend; 53 rmutex_t *lk; 54 55 FLOCKFILE(lk, stdin); 56 57 _SET_ORIENTATION_BYTE(stdin); 58 59 if (!(stdin->_flag & (_IOREAD | _IORW))) { 60 errno = EBADF; 61 FUNLOCKFILE(lk); 62 return (0); 63 } 64 65 if (stdin->_base == NULL) { 66 if ((bufend = _findbuf(stdin)) == 0) { 67 FUNLOCKFILE(lk); 68 return (0); 69 } 70 } 71 else 72 bufend = _bufend(stdin); 73 74 for (;;) /* until get a '\n' */ 75 { 76 if (stdin->_cnt <= 0) /* empty buffer */ 77 { 78 if (__filbuf(stdin) != EOF) { 79 stdin->_ptr--; /* put back the character */ 80 stdin->_cnt++; 81 } else if (ptr == buf) { /* never read anything */ 82 FUNLOCKFILE(lk); 83 return (0); 84 } else 85 break; /* nothing left to read */ 86 } 87 n = stdin->_cnt; 88 if ((p = (char *)memccpy(ptr, (char *)stdin->_ptr, '\n', 89 (size_t)n)) != 0) 90 n = p - ptr; 91 ptr += n; 92 stdin->_cnt -= n; 93 stdin->_ptr += n; 94 if (_needsync(stdin, bufend)) 95 _bufsync(stdin, bufend); 96 if (p != 0) /* found a '\n' */ 97 { 98 ptr--; /* step back over the '\n' */ 99 break; 100 } 101 } 102 *ptr = '\0'; 103 FUNLOCKFILE(lk); 104 return (buf); 105 } 106