Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / lib / libast / sfio / sfputr.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: sfputr.c /main/3 1995/11/01 18:34:20 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
29 *                          AT&T CORP.                          *
30 *                                                              *
31 *                Copyright (c) 1995 AT&T Corp.                 *
32 *                     All Rights Reserved                      *
33 *                                                              *
34 *           This software is licensed by AT&T Corp.            *
35 *       under the terms and conditions of the license in       *
36 *       http://www.research.att.com/orgs/ssr/book/reuse        *
37 *                                                              *
38 *               This software was created by the               *
39 *           Software Engineering Research Department           *
40 *                    AT&T Bell Laboratories                    *
41 *                                                              *
42 *               For further information contact                *
43 *                     gsf@research.att.com                     *
44 *                                                              *
45 ***************************************************************/
46 #include        "sfhdr.h"
47
48 /*      Put out a nul-terminated string
49 **      Note that the reg declarations below must be kept in
50 **      their relative order so that the code will configured
51 **      correctly on Vaxes to use "asm()".
52 **
53 **      Written by Kiem-Phong Vo
54 */
55 #if __STD_C
56 sfputr(reg Sfio_t* f, const char* s, reg int rc)
57 #else
58 sfputr(f,s,rc)
59 reg Sfio_t*     f;      /* write to this stream. r11 on Vax     */
60 char*           s;      /* string to write                      */
61 reg int         rc;     /* record separator. r10 on Vax         */
62 #endif
63 {
64         reg int         p;              /* r9 on Vax            */
65         reg uchar       *os, *ps;       /* r8, r7 on Vax        */
66         reg int         n;              /* r6 on Vax            */
67
68         if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
69                 return -1;
70
71         SFLOCK(f,0);
72         os = (uchar*)s;
73         if(f->size <= 0)
74         {       /* unbuffered stream */
75                 n = strlen((char*)os);
76                 if((p = SFWRITE(f,(Void_t*)os,n)) > 0)
77                         os += p;
78                 goto done;
79         }
80
81         while(*os)
82         {       /* peek buffer for space */
83                 if(SFWPEEK(f,ps,p) <= 0)
84                         break;
85
86 #if _vax_asm    /* p is r9, os is r8, and ps is r7 */
87                 0;                                      /* avoid if() branching bug */
88                 asm( "locc      $0,r9,(r8)" );          /* look for the \0 */
89                 asm( "subl2     r0,r9" );               /* length of data to copy */
90                 asm( "movc3     r9,(r8),(r7)" );        /* copy data */
91                 ps += p;
92                 os += p;
93 #else
94 #if _lib_memccpy
95                 if((ps = (uchar*)memccpy(ps,os,'\0',p)) != NIL(uchar*))
96                         ps -= 1;
97                 else    ps  = f->next+p;
98                 os += ps - f->next;
99 #else
100                 /* fast copy loop */
101                 while((*ps++ = *os++) != '\0' && --p > 0)
102                         ;
103                 if(*--ps != 0)
104                         ps += 1;
105                 else    os -= 1;
106 #endif
107 #endif
108                 f->next = ps;
109         }
110
111 done:
112         p = (char*)os - (char*)s;
113         if(rc >= 0)
114         {       if(f->next >= f->endb)
115                         (void)SFFLSBUF(f,(int)((uchar)rc));
116                 else    *f->next++ = (uchar)rc;
117                 p += 1;
118         }
119
120         /* sync unseekable shared streams */
121         if(f->extent < 0 && (f->flags&SF_SHARE) )
122                 (void)SFFLSBUF(f,-1);
123
124         /* check for line buffering */
125         else if((f->flags&SF_LINE) && !(f->flags&SF_STRING) && (n = f->next-f->data) > 0)
126         {       if(n > p)
127                         n = p;
128                 f->next -= n;
129                 (void)SFWRITE(f,(Void_t*)f->next,n);
130         }
131
132         SFOPEN(f,0);
133
134         return p;
135 }