4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
35 * This version writes directly to the buffer rather than looping on putc.
36 * Ptr args aren't checked for NULL because the program would be a
37 * catastrophic mess anyway. Better to abort than just to return NULL.
44 static char *memnulccpy(char *, char *, int, int);
47 fputs(char *ptr
, FILE *iop
)
50 unsigned char *cptr
, *bufend
;
61 bufend
= iop
->_base
+ iop
->_bufsiz
;
63 if ((iop
->_flag
& _IONBF
) == 0) {
64 if (iop
->_flag
& _IOLBF
) {
66 while ((n
= bufend
- (cptr
= iop
->_ptr
)) <= 0)
68 if (_xflsbuf(iop
) == EOF
)
70 if ((p
= memnulccpy((char *) cptr
, ptr
, '\n', n
)) != NULL
) {
72 * Copy terminated either because we
73 * saw a newline or we saw a NUL (end
76 c
= *(p
- 1); /* last character moved */
78 p
--; /* didn't write '\0' */
79 n
= p
- (char *) cptr
;
87 * We found either a newline or a NUL.
88 * If we found a newline, flush the
90 * If we found a NUL, we're done.
93 if (_xflsbuf(iop
) == EOF
)
102 for ( ; ; ptr
+= n
) {
103 while ((n
= bufend
- (cptr
= iop
->_ptr
)) <= 0)
105 if (_xflsbuf(iop
) == EOF
)
107 if ((p
= memccpy((char *) cptr
, ptr
, '\0', n
)) != NULL
)
108 n
= (p
- (char *) cptr
) - 1;
120 /* write out to an unbuffered file */
121 return (write(iop
->_file
, ptr
, strlen(ptr
)));
126 * Copy s2 to s1, stopping if character c or a NUL is copied.
127 * Copy no more than n bytes.
128 * Return a pointer to the byte after character c or NUL in the copy,
129 * or NULL if c or NUL is not found in the first n bytes.
132 memnulccpy(char *s1
, char *s2
, int c
, int n
)
138 if ((*s1
++ = cmoved
) == '\0' || cmoved
== c
)