1 /* @(#)fgetstr.c 1.14 18/10/10 Copyright 1986, 1996-2018 J. Schilling */
2 /*
3 * Copyright (c) 1986, 1996-2018 J. Schilling
4 */
5 /*
6 * The contents of this file are subject to the terms of the
7 * Common Development and Distribution License, Version 1.0 only
8 * (the "License"). You may not use this file except in compliance
9 * with the License.
10 *
11 * See the file CDDL.Schily.txt in this distribution for details.
12 * A copy of the CDDL is also available via the Internet at
13 * http://www.opensource.org/licenses/cddl1.txt
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file CDDL.Schily.txt from this distribution.
17 */
18
19 #define FAST_GETC_PUTC /* Will be reset if not possible */
20 #include "schilyio.h"
21
22 #if !defined(getc) && defined(USE_FGETS_FOR_FGETSTR)
23 #include <schily/string.h>
24
25 /*
26 * Warning: this prevents us from being able to have embedded null chars.
27 */
28 EXPORT int
fgetstr(f,buf,len)29 fgetstr(f, buf, len)
30 register FILE *f;
31 char *buf;
32 register int len;
33 {
34 char *bp = fgets(buf, len, f);
35
36 if (bp) {
37 return (strlen(bp));
38 }
39 buf[0] = '\0';
40 return (-1);
41 }
42
43 #else
44
45 EXPORT int
fgetstr(f,buf,len)46 fgetstr(f, buf, len)
47 register FILE *f;
48 char *buf;
49 register int len;
50 {
51 register char *bp = buf;
52 #if defined(HAVE_USG_STDIO) || defined(FAST_GETC_PUTC)
53 register char *p;
54 #else
55 register int c = '\0';
56 register int nl = '\n';
57 #endif
58
59 down2(f, _IOREAD, _IORW);
60
61 if (len <= 0)
62 return (0);
63
64 *bp = '\0';
65 for (;;) {
66 #if defined(HAVE_USG_STDIO) || defined(FAST_GETC_PUTC)
67 size_t n;
68
69 if ((__js_fp f)->_cnt <= 0) {
70 if (usg_filbuf(f) == EOF) {
71 /*
72 * If buffer is empty and we hit EOF, return EOF
73 */
74 if (bp == buf)
75 return (EOF);
76 break;
77 }
78 (__js_fp f)->_cnt++;
79 (__js_fp f)->_ptr--;
80 }
81
82 n = len;
83 if (n > (__js_fp f)->_cnt)
84 n = (__js_fp f)->_cnt;
85 p = movecbytes((__js_fp f)->_ptr, bp, '\n', n);
86 if (p) {
87 n = p - bp;
88 }
89 (__js_fp f)->_ptr += n;
90 (__js_fp f)->_cnt -= n;
91 bp += n;
92 len -= n;
93 if (p != NULL || len == 0)
94 break;
95 #else
96 if ((c = getc(f)) < 0) {
97 /*
98 * If buffer is empty and we hit EOF, return EOF
99 */
100 if (bp == buf)
101 return (c);
102 break;
103 }
104 if (--len > 0)
105 *bp++ = (char)c;
106 else
107 break;
108 if (c == nl)
109 break;
110 #endif
111 }
112 *bp = '\0';
113
114 return (bp - buf);
115 }
116
117 #endif
118
119 EXPORT int
getstr(buf,len)120 getstr(buf, len)
121 char *buf;
122 int len;
123 {
124 return (fgetstr(stdin, buf, len));
125 }
126