2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* $XConsortium: sfopen.c /main/3 1995/11/01 18:32:00 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS PROPRIETARY SOURCE CODE LICENSED BY *
31 * Copyright (c) 1995 AT&T Corp. *
32 * All Rights Reserved *
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 *
38 * This software was created by the *
39 * Software Engineering Research Department *
40 * AT&T Bell Laboratories *
42 * For further information contact *
43 * gsf@research.att.com *
45 ***************************************************************/
48 /* Open a file/string for IO.
49 ** If f is not nil, it is taken as an existing stream that should be
50 ** closed and its structure reused for the new stream.
52 ** Written by Kiem-Phong Vo (06/27/90)
56 Sfio_t *sfopen(reg Sfio_t* f, const char* file, const char* mode)
58 Sfio_t *sfopen(f,file,mode)
59 reg Sfio_t *f; /* old stream structure */
60 char *file; /* file/string to be opened */
61 reg char *mode; /* mode of the stream */
64 int fd, oldfd, oflags, sflags;
66 if((sflags = _sftype(mode,&oflags)) == 0)
77 while((fd = open((char*)file,oflags,0666)) < 0 && errno == EINTR)
80 while((fd = open(file,oflags&03)) < 0 && errno == EINTR)
85 while((tf = creat(file,0666)) < 0 && errno == EINTR)
90 else if(oflags&O_CREAT)
91 { while((fd = creat(file,0666)) < 0 && errno == EINTR)
93 if(!(oflags&O_WRONLY))
94 { /* the file now exists, reopen it for read/write */
96 while((fd = open(file,oflags&03)) < 0 && errno == EINTR)
105 oldfd = (f && !(f->flags&SF_STRING)) ? f->file : -1;
108 f = sfnew(f,(char*)file,file ? (int)strlen((char*)file) : -1,fd,sflags);
109 else if((f = sfnew(f,NIL(char*),-1,fd,sflags|SF_OPEN)) && oldfd >= 0)
110 (void)sfsetfd(f,oldfd);
116 _sftype(reg const char *mode, int *oflagsp)
118 _sftype(mode, oflagsp)
123 reg int sflags, oflags;
128 /* construct the open flags */
130 while(1) switch(*mode++)
134 oflags |= O_WRONLY | O_CREAT;
135 if(!(sflags&SF_READ))
139 sflags |= SF_WRITE | SF_APPEND;
140 oflags |= O_WRONLY | O_APPEND | O_CREAT;
159 sflags |= SF_READ|SF_WRITE;
162 if((sflags&SF_RDWR) == SF_RDWR)
163 oflags = (oflags&~(O_RDONLY|O_WRONLY))|O_RDWR;
164 if(!(oflags&O_BINARY) && (sflags&SF_READ))
168 if((sflags&(SF_STRING|SF_RDWR)) == SF_STRING)