diff options
author | svn2git <svn2git@FreeBSD.org> | 1994-07-01 08:00:00 +0000 |
---|---|---|
committer | svn2git <svn2git@FreeBSD.org> | 1994-07-01 08:00:00 +0000 |
commit | 5e0e9b99dc3fc0ecd49d929db0d57c784b66f481 (patch) | |
tree | e779b5a6edddbb949b7990751b12d6f25304ba86 /lib/libc/stdio | |
parent | a16f65c7d117419bd266c28a1901ef129a337569 (diff) |
Diffstat (limited to 'lib/libc/stdio')
-rw-r--r-- | lib/libc/stdio/Makefile.inc | 7 | ||||
-rw-r--r-- | lib/libc/stdio/fclose.c | 2 | ||||
-rw-r--r-- | lib/libc/stdio/fgetline.c | 1 | ||||
-rw-r--r-- | lib/libc/stdio/fgetln.3 (renamed from lib/libc/stdio/fgetline.3) | 52 | ||||
-rw-r--r-- | lib/libc/stdio/fgetln.c | 162 | ||||
-rw-r--r-- | lib/libc/stdio/fseek.c | 2 | ||||
-rw-r--r-- | lib/libc/stdio/gets.c | 13 | ||||
-rw-r--r-- | lib/libc/stdio/local.h | 2 | ||||
-rw-r--r-- | lib/libc/stdio/printf.3 | 2 |
9 files changed, 206 insertions, 37 deletions
diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index ad87006fbac74..3fd05f51ffc35 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -3,8 +3,9 @@ # stdio sources .PATH: ${.CURDIR}/stdio -SRCS+= clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ - fgetline.c fgetpos.c fgets.c fileno.c findfp.c flags.c fopen.c \ +SRCS+= clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \ + fgetc.c fgetline.c fgetln.c fgetpos.c fgets.c \ + fileno.c findfp.c flags.c fopen.c \ fprintf.c fpurge.c fputc.c fputs.c fread.c freopen.c fscanf.c \ fseek.c fsetpos.c ftell.c funopen.c fvwrite.c fwalk.c fwrite.c \ getc.c getchar.c gets.c getw.c makebuf.c perror.c printf.c putc.c \ @@ -14,7 +15,7 @@ SRCS+= clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ vfscanf.c vprintf.c vscanf.c vsnprintf.c vsprintf.c vsscanf.c \ wbuf.c wsetup.c -MAN3+= stdio/fclose.3 stdio/ferror.3 stdio/fflush.3 stdio/fgetline.3 \ +MAN3+= stdio/fclose.3 stdio/ferror.3 stdio/fflush.3 stdio/fgetln.3 \ stdio/fgets.3 stdio/fopen.3 stdio/fputs.3 stdio/fread.3 \ stdio/fseek.3 stdio/funopen.3 stdio/getc.3 stdio/mktemp.3 \ stdio/printf.3 stdio/putc.3 stdio/remove.3 stdio/scanf.3 \ diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c index 27d873f2073fe..974cd444853d5 100644 --- a/lib/libc/stdio/fclose.c +++ b/lib/libc/stdio/fclose.c @@ -48,7 +48,7 @@ fclose(fp) { register int r; - if (fp->_flags == 0) { /* not open! */ + if (!fp || (fp->_flags == 0)) { /* not open! */ errno = EBADF; return (EOF); } diff --git a/lib/libc/stdio/fgetline.c b/lib/libc/stdio/fgetline.c index bcd80aca4e861..70fe9d65fe5b3 100644 --- a/lib/libc/stdio/fgetline.c +++ b/lib/libc/stdio/fgetline.c @@ -48,6 +48,7 @@ static char sccsid[] = "@(#)fgetline.c 5.2 (Berkeley) 5/4/91"; * The `new size' does not account for a terminating '\0', * so we add 1 here. */ +static int __slbexpand(fp, newsize) FILE *fp; size_t newsize; diff --git a/lib/libc/stdio/fgetline.3 b/lib/libc/stdio/fgetln.3 index 745e745c66f6b..a453cff29c134 100644 --- a/lib/libc/stdio/fgetline.3 +++ b/lib/libc/stdio/fgetln.3 @@ -1,5 +1,5 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. +.\" Copyright (c) 1990, 1991, 1993 +.\" The Regents of the University of California. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -29,32 +29,37 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" @(#)fgetline.3 5.4 (Berkeley) 4/19/91 +.\" from: @(#)fgetline.3 8.1 (Berkeley) 6/9/93 +.\" $Id: fgetln.3,v 1.1 1994/04/17 09:16:36 alm Exp $ .\" -.Dd April 19, 1991 -.Dt FGETLINE 3 +.Dd June 9, 1993 +.Dt FGETLN 3 .Os .Sh NAME -.Nm fgetline +.Nm fgetln .Nd get a line from a stream .Sh SYNOPSIS .Fd #include <stdio.h> .Ft char * -.Fn fgetline "FILE *stream" "size_t *len" +.Fn fgetln "FILE *stream" "size_t *len" .Sh DESCRIPTION The -.Fn fgetline +.Fn fgetln function returns a pointer to the next line from the stream referenced by .Fa stream . -The newline character at the end of the line is replaced by a -.Dv NUL . -.Pp -If +This line is +.Em not +a C string as it does not end with a terminating +.Dv NUL +character. +The length of the line, including the final newline, +is stored in the memory location to which .Fa len -is non-NULL, the length of the line, not counting the terminating -.Dv NUL , -is stored in the memory location it references. +points. +(Note, however, that if the line is the last +in a file that does not end in a newline, +the returned text will not contain a newline.) .Sh RETURN VALUES Upon successful completion a pointer is returned; this pointer becomes invalid after the next @@ -67,7 +72,7 @@ Otherwise, .Dv NULL is returned. The -.Fn fgetline +.Fn fgetln function does not distinguish between end-of-file and error; the routines .Xr feof 3 @@ -75,7 +80,7 @@ and .Xr ferror 3 must be used to determine which occurred. -If an error occurrs, the global variable +If an error occurs, the global variable .Va errno is set to indicate the error. The end-of-file condition is remembered, even on a terminal, and all @@ -86,8 +91,7 @@ cleared with .Xr clearerr 3 . .Pp The text to which the returned pointer points may be modified, -provided that no changes are made beyond the terminating -.Dv NUL . +provided that no changes are made beyond the returned size. These changes are lost as soon as the pointer becomes invalid. .Sh ERRORS .Bl -tag -width [EBADF] @@ -98,7 +102,7 @@ is not a stream open for reading. .El .Pp The -.Fn fgetline +.Fn fgetln function may also fail and set .Va errno @@ -116,9 +120,5 @@ or .Xr putc 3 .Sh HISTORY The -.Fn fgetline -function is -.Ud . -.Sh BUGS -It is not possible to tell whether the final line of an input file -was terminated with a newline. +.Fn fgetln +function first appeared in 4.4BSD. diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c new file mode 100644 index 0000000000000..992b4b57e0889 --- /dev/null +++ b/lib/libc/stdio/fgetln.c @@ -0,0 +1,162 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +/* from: static char sccsid[] = "@(#)fgetline.c 8.1 (Berkeley) 6/4/93"; */ +static char *rcsid = "$Id: fgetln.c,v 1.1 1994/04/17 09:16:39 alm Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "local.h" + +/* + * Expand the line buffer. Return -1 on error. +#ifdef notdef + * The `new size' does not account for a terminating '\0', + * so we add 1 here. +#endif + */ +int +__slbexpand(fp, newsize) + FILE *fp; + size_t newsize; +{ + void *p; + +#ifdef notdef + ++newsize; +#endif + if (fp->_lb._size >= newsize) + return (0); + if ((p = realloc(fp->_lb._base, newsize)) == NULL) + return (-1); + fp->_lb._base = p; + fp->_lb._size = newsize; + return (0); +} + +/* + * Get an input line. The returned pointer often (but not always) + * points into a stdio buffer. Fgetline does not alter the text of + * the returned line (which is thus not a C string because it will + * not necessarily end with '\0'), but does allow callers to modify + * it if they wish. Thus, we set __SMOD in case the caller does. + */ +char * +fgetln(fp, lenp) + register FILE *fp; + size_t *lenp; +{ + register unsigned char *p; + register size_t len; + size_t off; + + /* make sure there is input */ + if (fp->_r <= 0 && __srefill(fp)) { + *lenp = 0; + return (NULL); + } + + /* look for a newline in the input */ + if ((p = memchr((void *)fp->_p, '\n', fp->_r)) != NULL) { + register char *ret; + + /* + * Found one. Flag buffer as modified to keep fseek from + * `optimising' a backward seek, in case the user stomps on + * the text. + */ + p++; /* advance over it */ + ret = (char *)fp->_p; + *lenp = len = p - fp->_p; + fp->_flags |= __SMOD; + fp->_r -= len; + fp->_p = p; + return (ret); + } + + /* + * We have to copy the current buffered data to the line buffer. + * As a bonus, though, we can leave off the __SMOD. + * + * OPTIMISTIC is length that we (optimistically) expect will + * accomodate the `rest' of the string, on each trip through the + * loop below. + */ +#define OPTIMISTIC 80 + + for (len = fp->_r, off = 0;; len += fp->_r) { + register size_t diff; + + /* + * Make sure there is room for more bytes. Copy data from + * file buffer to line buffer, refill file and look for + * newline. The loop stops only when we find a newline. + */ + if (__slbexpand(fp, len + OPTIMISTIC)) + goto error; + (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, + len - off); + off = len; + if (__srefill(fp)) + break; /* EOF or error: return partial line */ + if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL) + continue; + + /* got it: finish up the line (like code above) */ + p++; + diff = p - fp->_p; + len += diff; + if (__slbexpand(fp, len)) + goto error; + (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, + diff); + fp->_r -= diff; + fp->_p = p; + break; + } + *lenp = len; +#ifdef notdef + fp->_lb._base[len] = 0; +#endif + return ((char *)fp->_lb._base); + +error: + *lenp = 0; /* ??? */ + return (NULL); /* ??? */ +} diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index a027109e56591..a8fb89449ed0b 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -197,7 +197,7 @@ fseek(fp, offset, whence) * If the target offset is within the current buffer, * simply adjust the pointers, clear EOF, undo ungetc(), * and return. (If the buffer was modified, we have to - * skip this; see fgetline.c.) + * skip this; see fgetln.c.) */ if ((fp->_flags & __SMOD) == 0 && target >= curoff && target < curoff + n) { diff --git a/lib/libc/stdio/gets.c b/lib/libc/stdio/gets.c index 4f1622369dbcb..12fc115bc64ff 100644 --- a/lib/libc/stdio/gets.c +++ b/lib/libc/stdio/gets.c @@ -35,11 +35,16 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)gets.c 5.3 (Berkeley) 1/20/91"; +static char sccsid[] = "From: @(#)gets.c 5.3 (Berkeley) 1/20/91"; +static char rcsid[] = + "$Id: gets.c,v 1.3 1994/04/23 20:35:27 wollman Exp $"; #endif /* LIBC_SCCS and not lint */ #include <unistd.h> #include <stdio.h> +#include <string.h> + +#define MESSAGE ": warning: this program uses gets(), which is unsafe.\r\n" char * gets(buf) @@ -48,11 +53,11 @@ gets(buf) register int c; register char *s; static int warned; - static char w[] = - "warning: this program uses gets(), which is unsafe.\r\n"; if (!warned) { - (void) write(STDERR_FILENO, w, sizeof(w) - 1); + extern char *__progname; + write(2, __progname, strlen(__progname)); + write(2, MESSAGE, sizeof(MESSAGE) - 1); warned = 1; } for (s = buf; (c = getchar()) != '\n';) diff --git a/lib/libc/stdio/local.h b/lib/libc/stdio/local.h index 21966d704dbdc..87ed4e60d23e8 100644 --- a/lib/libc/stdio/local.h +++ b/lib/libc/stdio/local.h @@ -78,7 +78,7 @@ extern int __sdidinit; } /* - * test for an fgetline() buffer. + * test for an fgetln() buffer. */ #define HASLB(fp) ((fp)->_lb._base != NULL) #define FREELB(fp) { \ diff --git a/lib/libc/stdio/printf.3 b/lib/libc/stdio/printf.3 index 130246ea1b0a8..0d7c528738814 100644 --- a/lib/libc/stdio/printf.3 +++ b/lib/libc/stdio/printf.3 @@ -402,7 +402,7 @@ are used for conversions; the letters .Cm ABCDEF are used for -.m X +.Cm X conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on |