1 /** @file 2 Implementation of fgets as declared in <stdio.h>. 3 4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR> 5 This program and the accompanying materials are licensed and made available 6 under the terms and conditions of the BSD License that accompanies this 7 distribution. The full text of the license may be found at 8 http://opensource.org/licenses/bsd-license. 9 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 12 13 Copyright (c) 1990, 1993 14 The Regents of the University of California. All rights reserved. 15 16 This code is derived from software contributed to Berkeley by 17 Chris Torek. 18 19 Redistribution and use in source and binary forms, with or without 20 modification, are permitted provided that the following conditions 21 are met: 22 - Redistributions of source code must retain the above copyright 23 notice, this list of conditions and the following disclaimer. 24 - Redistributions in binary form must reproduce the above copyright 25 notice, this list of conditions and the following disclaimer in the 26 documentation and/or other materials provided with the distribution. 27 - Neither the name of the University nor the names of its contributors 28 may be used to endorse or promote products derived from this software 29 without specific prior written permission. 30 31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 35 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 39 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 POSSIBILITY OF SUCH DAMAGE. 42 43 NetBSD: fgets.c,v 1.20 2003/12/14 23:56:28 lukem Exp 44 fgets.c 8.2 (Berkeley) 12/22/93 45 **/ 46 #include <LibConfig.h> 47 48 #include <assert.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <errno.h> 52 #include "reentrant.h" 53 #include "local.h" 54 55 /* 56 * Read at most n-1 characters from the given file. 57 * Stop when a newline has been read, or the count runs out. 58 * Return first argument, or NULL if no characters were read. 59 */ 60 char * 61 fgets(char *buf, int n, FILE *fp) 62 { 63 size_t len; 64 char *s; 65 unsigned char *p, *t; 66 67 _DIAGASSERT(buf != NULL); 68 _DIAGASSERT(fp != NULL); 69 if ((fp == NULL) || (n <= 0)) { /* sanity check */ 70 errno = EINVAL; 71 return (NULL); 72 } 73 74 FLOCKFILE(fp); 75 _SET_ORIENTATION(fp, -1); 76 s = buf; 77 n--; /* leave space for NUL */ 78 while (n != 0) { 79 /* 80 * If the buffer is empty, refill it. 81 */ 82 if (fp->_r <= 0) { 83 if (__srefill(fp)) { 84 /* EOF/error: stop with partial or no line */ 85 if (s == buf) { 86 FUNLOCKFILE(fp); 87 return (NULL); 88 } 89 break; 90 } 91 } 92 len = fp->_r; 93 p = fp->_p; 94 95 /* 96 * Scan through at most n bytes of the current buffer, 97 * looking for '\n'. If found, copy up to and including 98 * newline, and stop. Otherwise, copy entire chunk 99 * and loop. 100 */ 101 if (len > (size_t)n) 102 len = n; 103 t = memchr((void *)p, '\n', len); 104 if (t != NULL) { 105 len = ++t - p; 106 fp->_r -= (int)len; 107 fp->_p = t; 108 (void)memcpy((void *)s, (void *)p, len); 109 s[len] = 0; 110 FUNLOCKFILE(fp); 111 return (buf); 112 } 113 fp->_r -= (int)len; 114 fp->_p += len; 115 (void)memcpy((void *)s, (void *)p, len); 116 s += len; 117 n -= (int)len; 118 } 119 *s = 0; 120 FUNLOCKFILE(fp); 121 return (buf); 122 } 123