Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdio Fix incorrect example (what happens when len ...



details:   https://anonhg.NetBSD.org/src/rev/9afbd045ef48
branches:  trunk
changeset: 796813:9afbd045ef48
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Jun 19 14:27:50 2014 +0000

description:
Fix incorrect example (what happens when len == 0?)

diffstat:

 lib/libc/stdio/fgetln.3 |  22 +++++++---------------
 1 files changed, 7 insertions(+), 15 deletions(-)

diffs (46 lines):

diff -r 45db05a53d72 -r 9afbd045ef48 lib/libc/stdio/fgetln.3
--- a/lib/libc/stdio/fgetln.3   Thu Jun 19 13:20:28 2014 +0000
+++ b/lib/libc/stdio/fgetln.3   Thu Jun 19 14:27:50 2014 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: fgetln.3,v 1.14 2004/05/10 17:15:28 drochner Exp $
+.\"    $NetBSD: fgetln.3,v 1.15 2014/06/19 14:27:50 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)fgetln.3   8.3 (Berkeley) 4/19/94
 .\"
-.Dd April 21, 2004
+.Dd June 19, 2014
 .Dt FGETLN 3
 .Os
 .Sh NAME
@@ -134,22 +134,14 @@
        char *buf, *lbuf;
        size_t len;
 
-       lbuf = NULL;
-       while ((buf = fgetln(fp, &len))) {
-               if (buf[len - 1] == '\en')
+       while ((lbuf = buf = fgetln(fp, &len)) != NULL) {
+               if (len > 0 && buf[len - 1] == '\en')
                        buf[len - 1] = '\e0';
-               else {
-                       if ((lbuf = (char *)malloc(len + 1)) == NULL)
+               else if ((lbuf = strndup(buf, len + 1)) == NULL)
                                err(1, NULL);
-                       memcpy(lbuf, buf, len);
-                       lbuf[len] = '\e0';
-                       buf = lbuf;
-               }
-               printf("%s\en", buf);
+               printf("%s\en", lbuf);
 
-               if (lbuf != NULL) {
+               if (lbuf != buf)
                        free(lbuf);
-                       lbuf = NULL;
-               }
        }
 .Ed



Home | Main Index | Thread Index | Old Index