4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
36 #include <sys/types.h>
39 #define LINESZ 128 /* initial guess for a NULL *lineptr */
42 getdelim(char **_RESTRICT_KYWD lineptr
, size_t *_RESTRICT_KYWD n
,
43 int delimiter
, FILE *_RESTRICT_KYWD iop
)
51 if (lineptr
== NULL
|| n
== NULL
||
52 delimiter
< 0 || delimiter
> UCHAR_MAX
) {
57 if (*lineptr
== NULL
|| *n
< LINESZ
) { /* initial allocation */
58 if ((*lineptr
= realloc(*lineptr
, LINESZ
)) == NULL
) {
70 _SET_ORIENTATION_BYTE(iop
);
73 c
= (--iop
->_cnt
< 0) ? __filbuf(iop
) : *iop
->_ptr
++;
77 if (++cnt
== size
) { /* must reallocate */
78 if ((ptr
= realloc(*lineptr
, 2 * size
)) == NULL
) {
80 ptr
= *lineptr
+ size
- 1;
89 } while (c
!= delimiter
);
94 if (cnt
> SSIZE_MAX
) {
98 return (cnt
? cnt
: -1);
102 getline(char **_RESTRICT_KYWD lineptr
, size_t *_RESTRICT_KYWD n
,
103 FILE *_RESTRICT_KYWD iop
)
105 return (getdelim(lineptr
, n
, '\n', iop
));