c222390ea23b153dde3e2cc9eae07c200738cfa1
[reactos.git] / reactos / sdk / tools / mkisofs / schilytools / libschily / stdio / fgetline.c
1 /* @(#)fgetline.c 1.13 14/03/27 Copyright 1986, 1996-2014 J. Schilling */
2 /*
3 * Copyright (c) 1986, 1996-2014 J. Schilling
4 *
5 * This is an interface that exists in the public since 1982.
6 * The POSIX.1-2008 standard did ignore POSIX rules not to
7 * redefine existing public interfaces and redefined the interfaces
8 * forcing us to add a js_*() prefix to the original functions.
9 */
10 /*
11 * The contents of this file are subject to the terms of the
12 * Common Development and Distribution License, Version 1.0 only
13 * (the "License"). You may not use this file except in compliance
14 * with the License.
15 *
16 * See the file CDDL.Schily.txt in this distribution for details.
17 * A copy of the CDDL is also available via the Internet at
18 * http://www.opensource.org/licenses/cddl1.txt
19 *
20 * When distributing Covered Code, include this CDDL HEADER in each
21 * file and include the License file CDDL.Schily.txt from this distribution.
22 */
23
24 #define fgetline __no__fgetline__
25 #define getline __no__getline__
26
27 #include "schilyio.h"
28
29 #ifndef NO_GETLINE_COMPAT /* Define to disable backward compatibility */
30 #undef fgetline
31 #undef getline
32 #ifdef HAVE_PRAGMA_WEAK
33 #pragma weak fgetline = js_fgetline
34 #pragma weak getline = js_getline
35 #else
36
37 EXPORT int fgetline __PR((FILE *, char *, int));
38 EXPORT int getline __PR((char *, int));
39
40 EXPORT int
41 fgetline(f, buf, len)
42 FILE *f;
43 char *buf;
44 int len;
45 {
46 return (js_fgetline(f, buf, len));
47 }
48
49 EXPORT int
50 getline(buf, len)
51 char *buf;
52 int len;
53 {
54 return (js_fgetline(stdin, buf, len));
55 }
56 #endif
57 #endif
58
59 /*
60 * XXX should we check if HAVE_USG_STDIO is defined and
61 * XXX use something line memccpy to speed things up ???
62 * XXX On Solaris 64 bits, we may use #define FAST_GETC_PUTC
63 * XXX and getc_unlocked()
64 */
65 #if !defined(getc) && defined(USE_FGETS_FOR_FGETLINE)
66 #include <schily/string.h>
67
68 EXPORT int
69 js_fgetline(f, buf, len)
70 register FILE *f;
71 char *buf;
72 register int len;
73 {
74 char *bp = fgets(buf, len, f);
75
76 if (bp) {
77 len = strlen(bp);
78
79 if (len > 0) {
80 if (bp[len-1] == '\n')
81 bp[--len] = '\0';
82 }
83 return (len);
84 }
85 buf[0] = '\0';
86 return (-1);
87 }
88
89 #else
90 EXPORT int
91 js_fgetline(f, buf, len)
92 register FILE *f;
93 char *buf;
94 register int len;
95 {
96 register int c = '\0';
97 register char *bp = buf;
98 register int nl = '\n';
99
100 down2(f, _IOREAD, _IORW);
101
102 for (;;) {
103 if ((c = getc(f)) < 0)
104 break;
105 if (c == nl)
106 break;
107 if (--len > 0) {
108 *bp++ = (char)c;
109 } else {
110 #ifdef __never__
111 /*
112 * Read up to end of line
113 */
114 while ((c = getc(f)) >= 0 && c != nl)
115 /* LINTED */
116 ;
117 #endif
118 break;
119 }
120 }
121 *bp = '\0';
122 /*
123 * If buffer is empty and we hit EOF, return EOF
124 */
125 if (c < 0 && bp == buf)
126 return (c);
127
128 return (bp - buf);
129 }
130 #endif
131
132 EXPORT int
133 js_getline(buf, len)
134 char *buf;
135 int len;
136 {
137 return (js_fgetline(stdin, buf, len));
138 }