Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / librols / stdio / fgetline.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)fgetline.c       1.8 04/09/25 Copyright 1986, 1996-2003 J. Schilling */
14 /*
15  *      Copyright (c) 1986, 1996-2003 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #include "schilyio.h"
33
34 /*
35  * XXX should we check if HAVE_USG_STDIO is defined and
36  * XXX use something line memccpy to speed things up ???
37  */
38
39 EXPORT int
40 rols_fgetline(f, buf, len)
41         register        FILE    *f;
42                         char    *buf;
43         register        int     len;
44 {
45         register int    c       = '\0';
46         register char   *bp     = buf;
47         register int    nl      = '\n';
48
49         down2(f, _IOREAD, _IORW);
50
51         for (;;) {
52                 if ((c = getc(f)) < 0)
53                         break;
54                 if (c == nl)
55                         break;
56                 if (--len > 0) {
57                         *bp++ = (char)c;
58                 } else {
59                         /*
60                          * Read up to end of line
61                          */
62                         while ((c = getc(f)) >= 0 && c != nl)
63                                 /* LINTED */
64                                 ;
65                         break;
66                 }
67         }
68         *bp = '\0';
69         /*
70          * If buffer is empty and we hit EOF, return EOF
71          */
72         if (c < 0 && bp == buf)
73                 return (c);
74
75         return (bp - buf);
76 }
77
78 EXPORT int
79 rols_getline(buf, len)
80         char    *buf;
81         int     len;
82 {
83         return (rols_fgetline(stdin, buf, len));
84 }