~ubuntu-branches/ubuntu/hoary/cdrtools/hoary

« back to all changes in this revision

Viewing changes to lib/stdio/fgetline.c

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2002-04-09 10:03:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020409100306-t4hagiv7gm0fhggv
Tags: upstream-1.10
ImportĀ upstreamĀ versionĀ 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)fgetline.c       1.5 00/12/03 Copyright 1986 J. Schilling */
 
2
/*
 
3
 *      Copyright (c) 1986 J. Schilling
 
4
 */
 
5
/*
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2, or (at your option)
 
9
 * any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; see the file COPYING.  If not, write to
 
18
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include "io.h"
 
22
 
 
23
/*
 
24
 * XXX should we check if HAVE_USG_STDIO is defined and
 
25
 * XXX use something line memccpy to speed things up ???
 
26
 */
 
27
 
 
28
EXPORT int
 
29
fgetline(f, buf, len)
 
30
        register        FILE    *f;
 
31
                        char    *buf;
 
32
        register        int     len;
 
33
{
 
34
        register int    c       = '\0';
 
35
        register char   *bp     = buf;
 
36
        register int    nl      = '\n';
 
37
 
 
38
        down2(f, _IOREAD, _IORW);
 
39
 
 
40
        for(;;) {
 
41
                if((c = getc(f)) < 0)
 
42
                        break;
 
43
                if(c == nl)
 
44
                        break;
 
45
                if (--len > 0) {
 
46
                        *bp++ = c;
 
47
                } else {
 
48
                        /*
 
49
                         * Read up to end of line
 
50
                         */
 
51
                        while ((c = getc(f)) >= 0 && c != nl)
 
52
                                ;
 
53
                        break;
 
54
                }
 
55
        }
 
56
        *bp = '\0';
 
57
        /*
 
58
         * If buffer is empty and we hit EOF, return EOF
 
59
         */
 
60
        if(c < 0 && bp == buf)
 
61
                return (c);
 
62
 
 
63
        return (bp - buf);
 
64
}
 
65
 
 
66
EXPORT int
 
67
getline(buf, len)
 
68
        char    *buf;
 
69
        int     len;
 
70
{
 
71
        return (fgetline(stdin, buf, len));
 
72
}