fgetln.c (50476) | fgetln.c (72373) |
---|---|
1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)fgetln.c 8.2 (Berkeley) 1/2/94"; 40#endif 41static const char rcsid[] = | 1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)fgetln.c 8.2 (Berkeley) 1/2/94"; 40#endif 41static const char rcsid[] = |
42 "$FreeBSD: head/lib/libc/stdio/fgetln.c 50476 1999-08-28 00:22:10Z peter $"; | 42 "$FreeBSD: head/lib/libc/stdio/fgetln.c 72373 2001-02-11 22:06:43Z deischen $"; |
43#endif /* LIBC_SCCS and not lint */ 44 | 43#endif /* LIBC_SCCS and not lint */ 44 |
45#include "namespace.h" |
|
45#include <stdio.h> 46#include <stdlib.h> 47#include <string.h> | 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> |
49#include "un-namespace.h" 50#include "libc_private.h" |
|
48#include "local.h" 49 50/* 51 * Expand the line buffer. Return -1 on error. 52#ifdef notdef 53 * The `new size' does not account for a terminating '\0', 54 * so we add 1 here. 55#endif 56 */ | 51#include "local.h" 52 53/* 54 * Expand the line buffer. Return -1 on error. 55#ifdef notdef 56 * The `new size' does not account for a terminating '\0', 57 * so we add 1 here. 58#endif 59 */ |
57int 58__slbexpand(fp, newsize) 59 FILE *fp; 60 size_t newsize; | 60static int 61slbexpand(FILE *fp, size_t newsize) |
61{ 62 void *p; 63 64#ifdef notdef 65 ++newsize; 66#endif 67 if (fp->_lb._size >= newsize) 68 return (0); --- 7 unchanged lines hidden (view full) --- 76/* 77 * Get an input line. The returned pointer often (but not always) 78 * points into a stdio buffer. Fgetln does not alter the text of 79 * the returned line (which is thus not a C string because it will 80 * not necessarily end with '\0'), but does allow callers to modify 81 * it if they wish. Thus, we set __SMOD in case the caller does. 82 */ 83char * | 62{ 63 void *p; 64 65#ifdef notdef 66 ++newsize; 67#endif 68 if (fp->_lb._size >= newsize) 69 return (0); --- 7 unchanged lines hidden (view full) --- 77/* 78 * Get an input line. The returned pointer often (but not always) 79 * points into a stdio buffer. Fgetln does not alter the text of 80 * the returned line (which is thus not a C string because it will 81 * not necessarily end with '\0'), but does allow callers to modify 82 * it if they wish. Thus, we set __SMOD in case the caller does. 83 */ 84char * |
84fgetln(fp, lenp) 85 register FILE *fp; 86 size_t *lenp; | 85fgetln(FILE *fp, size_t *lenp) |
87{ | 86{ |
88 register unsigned char *p; 89 register size_t len; | 87 unsigned char *p; 88 size_t len; |
90 size_t off; 91 | 89 size_t off; 90 |
91 FLOCKFILE(fp); |
|
92 /* make sure there is input */ 93 if (fp->_r <= 0 && __srefill(fp)) { 94 *lenp = 0; | 92 /* make sure there is input */ 93 if (fp->_r <= 0 && __srefill(fp)) { 94 *lenp = 0; |
95 FUNLOCKFILE(fp); |
|
95 return (NULL); 96 } 97 98 /* look for a newline in the input */ 99 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) { | 96 return (NULL); 97 } 98 99 /* look for a newline in the input */ 100 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) { |
100 register char *ret; | 101 char *ret; |
101 102 /* 103 * Found one. Flag buffer as modified to keep fseek from 104 * `optimising' a backward seek, in case the user stomps on 105 * the text. 106 */ 107 p++; /* advance over it */ 108 ret = (char *)fp->_p; 109 *lenp = len = p - fp->_p; 110 fp->_flags |= __SMOD; 111 fp->_r -= len; 112 fp->_p = p; | 102 103 /* 104 * Found one. Flag buffer as modified to keep fseek from 105 * `optimising' a backward seek, in case the user stomps on 106 * the text. 107 */ 108 p++; /* advance over it */ 109 ret = (char *)fp->_p; 110 *lenp = len = p - fp->_p; 111 fp->_flags |= __SMOD; 112 fp->_r -= len; 113 fp->_p = p; |
114 FUNLOCKFILE(fp); |
|
113 return (ret); 114 } 115 116 /* 117 * We have to copy the current buffered data to the line buffer. 118 * As a bonus, though, we can leave off the __SMOD. 119 * 120 * OPTIMISTIC is length that we (optimistically) expect will 121 * accomodate the `rest' of the string, on each trip through the 122 * loop below. 123 */ 124#define OPTIMISTIC 80 125 126 for (len = fp->_r, off = 0;; len += fp->_r) { | 115 return (ret); 116 } 117 118 /* 119 * We have to copy the current buffered data to the line buffer. 120 * As a bonus, though, we can leave off the __SMOD. 121 * 122 * OPTIMISTIC is length that we (optimistically) expect will 123 * accomodate the `rest' of the string, on each trip through the 124 * loop below. 125 */ 126#define OPTIMISTIC 80 127 128 for (len = fp->_r, off = 0;; len += fp->_r) { |
127 register size_t diff; | 129 size_t diff; |
128 129 /* 130 * Make sure there is room for more bytes. Copy data from 131 * file buffer to line buffer, refill file and look for 132 * newline. The loop stops only when we find a newline. 133 */ | 130 131 /* 132 * Make sure there is room for more bytes. Copy data from 133 * file buffer to line buffer, refill file and look for 134 * newline. The loop stops only when we find a newline. 135 */ |
134 if (__slbexpand(fp, len + OPTIMISTIC)) | 136 if (slbexpand(fp, len + OPTIMISTIC)) |
135 goto error; 136 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 137 len - off); 138 off = len; 139 if (__srefill(fp)) 140 break; /* EOF or error: return partial line */ 141 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL) 142 continue; 143 144 /* got it: finish up the line (like code above) */ 145 p++; 146 diff = p - fp->_p; 147 len += diff; | 137 goto error; 138 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 139 len - off); 140 off = len; 141 if (__srefill(fp)) 142 break; /* EOF or error: return partial line */ 143 if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL) 144 continue; 145 146 /* got it: finish up the line (like code above) */ 147 p++; 148 diff = p - fp->_p; 149 len += diff; |
148 if (__slbexpand(fp, len)) | 150 if (slbexpand(fp, len)) |
149 goto error; 150 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 151 diff); 152 fp->_r -= diff; 153 fp->_p = p; 154 break; 155 } 156 *lenp = len; 157#ifdef notdef 158 fp->_lb._base[len] = 0; 159#endif | 151 goto error; 152 (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, 153 diff); 154 fp->_r -= diff; 155 fp->_p = p; 156 break; 157 } 158 *lenp = len; 159#ifdef notdef 160 fp->_lb._base[len] = 0; 161#endif |
162 FUNLOCKFILE(fp); |
|
160 return ((char *)fp->_lb._base); 161 162error: 163 *lenp = 0; /* ??? */ | 163 return ((char *)fp->_lb._base); 164 165error: 166 *lenp = 0; /* ??? */ |
167 FUNLOCKFILE(fp); |
|
164 return (NULL); /* ??? */ 165} | 168 return (NULL); /* ??? */ 169} |