17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*5d54f3d8Smuffin * Copyright 1995 Sun Microsystems, Inc. All rights reserved. 24*5d54f3d8Smuffin * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * This version writes directly to the buffer rather than looping on putc. 367c478bd9Sstevel@tonic-gate * Ptr args aren't checked for NULL because the program would be a 377c478bd9Sstevel@tonic-gate * catastrophic mess anyway. Better to abort than just to return NULL. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate #include <stdio.h> 407c478bd9Sstevel@tonic-gate #include "stdiom.h" 417c478bd9Sstevel@tonic-gate #include <errno.h> 42*5d54f3d8Smuffin #include <memory.h> 437c478bd9Sstevel@tonic-gate 44*5d54f3d8Smuffin static char *memnulccpy(char *, char *, int, int); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate int 47*5d54f3d8Smuffin fputs(char *ptr, FILE *iop) 487c478bd9Sstevel@tonic-gate { 49*5d54f3d8Smuffin int ndone = 0, n; 50*5d54f3d8Smuffin unsigned char *cptr, *bufend; 51*5d54f3d8Smuffin char *p; 52*5d54f3d8Smuffin char c; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate if (_WRTCHK(iop)) { 557c478bd9Sstevel@tonic-gate iop->_flag |= _IOERR; 567c478bd9Sstevel@tonic-gate #ifdef POSIX 577c478bd9Sstevel@tonic-gate errno = EBADF; 58*5d54f3d8Smuffin #endif /* POSIX */ 597c478bd9Sstevel@tonic-gate return (EOF); 607c478bd9Sstevel@tonic-gate } 617c478bd9Sstevel@tonic-gate bufend = iop->_base + iop->_bufsiz; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate if ((iop->_flag & _IONBF) == 0) { 647c478bd9Sstevel@tonic-gate if (iop->_flag & _IOLBF) { 657c478bd9Sstevel@tonic-gate for ( ; ; ptr += n) { 667c478bd9Sstevel@tonic-gate while ((n = bufend - (cptr = iop->_ptr)) <= 0) 677c478bd9Sstevel@tonic-gate /* full buf */ 687c478bd9Sstevel@tonic-gate if (_xflsbuf(iop) == EOF) 697c478bd9Sstevel@tonic-gate return(EOF); 707c478bd9Sstevel@tonic-gate if ((p = memnulccpy((char *) cptr, ptr, '\n', n)) != NULL) { 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Copy terminated either because we 737c478bd9Sstevel@tonic-gate * saw a newline or we saw a NUL (end 747c478bd9Sstevel@tonic-gate * of string). 757c478bd9Sstevel@tonic-gate */ 767c478bd9Sstevel@tonic-gate c = *(p - 1); /* last character moved */ 777c478bd9Sstevel@tonic-gate if (c == '\0') 787c478bd9Sstevel@tonic-gate p--; /* didn't write '\0' */ 797c478bd9Sstevel@tonic-gate n = p - (char *) cptr; 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate iop->_cnt -= n; 827c478bd9Sstevel@tonic-gate iop->_ptr += n; 837c478bd9Sstevel@tonic-gate _BUFSYNC(iop); 847c478bd9Sstevel@tonic-gate ndone += n; 857c478bd9Sstevel@tonic-gate if (p != NULL) { 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * We found either a newline or a NUL. 887c478bd9Sstevel@tonic-gate * If we found a newline, flush the 897c478bd9Sstevel@tonic-gate * buffer. 907c478bd9Sstevel@tonic-gate * If we found a NUL, we're done. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate if (c == '\n') { 937c478bd9Sstevel@tonic-gate if (_xflsbuf(iop) == EOF) 947c478bd9Sstevel@tonic-gate return(EOF); 957c478bd9Sstevel@tonic-gate } else { 967c478bd9Sstevel@tonic-gate /* done */ 977c478bd9Sstevel@tonic-gate return(ndone); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } else { 1027c478bd9Sstevel@tonic-gate for ( ; ; ptr += n) { 1037c478bd9Sstevel@tonic-gate while ((n = bufend - (cptr = iop->_ptr)) <= 0) 1047c478bd9Sstevel@tonic-gate /* full buf */ 1057c478bd9Sstevel@tonic-gate if (_xflsbuf(iop) == EOF) 1067c478bd9Sstevel@tonic-gate return(EOF); 1077c478bd9Sstevel@tonic-gate if ((p = memccpy((char *) cptr, ptr, '\0', n)) != NULL) 1087c478bd9Sstevel@tonic-gate n = (p - (char *) cptr) - 1; 1097c478bd9Sstevel@tonic-gate iop->_cnt -= n; 1107c478bd9Sstevel@tonic-gate iop->_ptr += n; 1117c478bd9Sstevel@tonic-gate _BUFSYNC(iop); 1127c478bd9Sstevel@tonic-gate ndone += n; 1137c478bd9Sstevel@tonic-gate if (p != NULL) { 1147c478bd9Sstevel@tonic-gate /* done */ 1157c478bd9Sstevel@tonic-gate return(ndone); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate } else { 1207c478bd9Sstevel@tonic-gate /* write out to an unbuffered file */ 1217c478bd9Sstevel@tonic-gate return (write(iop->_file, ptr, strlen(ptr))); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Copy s2 to s1, stopping if character c or a NUL is copied. 1277c478bd9Sstevel@tonic-gate * Copy no more than n bytes. 1287c478bd9Sstevel@tonic-gate * Return a pointer to the byte after character c or NUL in the copy, 1297c478bd9Sstevel@tonic-gate * or NULL if c or NUL is not found in the first n bytes. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate static char * 132*5d54f3d8Smuffin memnulccpy(char *s1, char *s2, int c, int n) 1337c478bd9Sstevel@tonic-gate { 134*5d54f3d8Smuffin int cmoved; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate while (--n >= 0) { 1377c478bd9Sstevel@tonic-gate cmoved = *s2++; 1387c478bd9Sstevel@tonic-gate if ((*s1++ = cmoved) == '\0' || cmoved == c) 1397c478bd9Sstevel@tonic-gate return (s1); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate return (0); 1427c478bd9Sstevel@tonic-gate } 143