xref: /titanic_51/usr/src/lib/libast/common/sfio/_sfopen.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6da2e3ebdSchin *                  Common Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10da2e3ebdSchin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebdSchin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebdSchin *                                                                      *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include	"sfhdr.h"
23da2e3ebdSchin 
24da2e3ebdSchin /*	Open a file/string for IO.
25da2e3ebdSchin **	If f is not nil, it is taken as an existing stream that should be
26da2e3ebdSchin **	closed and its structure reused for the new stream.
27da2e3ebdSchin **
28da2e3ebdSchin **	Written by Kiem-Phong Vo.
29da2e3ebdSchin */
30da2e3ebdSchin 
31da2e3ebdSchin #if _BLD_sfio && defined(__EXPORT__)
32da2e3ebdSchin #define extern  __EXPORT__
33da2e3ebdSchin #endif
34da2e3ebdSchin extern
35da2e3ebdSchin #undef  extern
36da2e3ebdSchin 
37da2e3ebdSchin #if __STD_C
_sfopen(Sfio_t * f,const char * file,const char * mode)387c2fbfb3SApril Chin Sfio_t* _sfopen(Sfio_t* f, const char* file, const char* mode)
39da2e3ebdSchin #else
40da2e3ebdSchin Sfio_t* _sfopen(f,file,mode)
417c2fbfb3SApril Chin Sfio_t*		f;		/* old stream structure */
42da2e3ebdSchin char*		file;		/* file/string to be opened */
437c2fbfb3SApril Chin char*		mode;		/* mode of the stream */
44da2e3ebdSchin #endif
45da2e3ebdSchin {
46da2e3ebdSchin 	int	fd, oldfd, oflags, sflags;
477c2fbfb3SApril Chin 	SFMTXDECL(f);
48da2e3ebdSchin 
49da2e3ebdSchin 	/* get the control flags */
50da2e3ebdSchin 	if((sflags = _sftype(mode,&oflags,NIL(int*))) == 0)
51da2e3ebdSchin 		return NIL(Sfio_t*);
52da2e3ebdSchin 
53da2e3ebdSchin 	/* changing the control flags */
54da2e3ebdSchin 	if(f && !file && !((f->flags|sflags)&SF_STRING) )
557c2fbfb3SApril Chin 	{	SFMTXENTER(f, NIL(Sfio_t*));
56da2e3ebdSchin 
57da2e3ebdSchin 		if(f->mode&SF_INIT ) /* stream uninitialized, ok to set flags */
58da2e3ebdSchin 		{	f->flags |= (sflags & (SF_FLAGS & ~SF_RDWR));
59da2e3ebdSchin 
60da2e3ebdSchin 			if((sflags &= SF_RDWR) != 0) /* reset read/write modes */
61da2e3ebdSchin 			{	f->flags = (f->flags & ~SF_RDWR) | sflags;
62da2e3ebdSchin 
63da2e3ebdSchin 				if((f->flags&SF_RDWR) == SF_RDWR)
64da2e3ebdSchin 					f->bits |= SF_BOTH;
65da2e3ebdSchin 				else	f->bits &= ~SF_BOTH;
66da2e3ebdSchin 
67da2e3ebdSchin 				if(f->flags&SF_READ)
68da2e3ebdSchin 					f->mode = (f->mode&~SF_WRITE)|SF_READ;
69da2e3ebdSchin 				else	f->mode = (f->mode&~SF_READ)|SF_WRITE;
70da2e3ebdSchin 			}
71da2e3ebdSchin 		}
72da2e3ebdSchin 		else /* make sure there is no buffered data */
73da2e3ebdSchin 		{	if(sfsync(f) < 0)
74da2e3ebdSchin 				SFMTXRETURN(f,NIL(Sfio_t*));
75da2e3ebdSchin 		}
76da2e3ebdSchin 
77da2e3ebdSchin 		if(f->file >= 0 && (oflags &= (O_TEXT|O_BINARY|O_APPEND)) != 0 )
78da2e3ebdSchin 		{	/* set file access control */
79da2e3ebdSchin 			int ctl = sysfcntlf(f->file, F_GETFL, 0);
80da2e3ebdSchin 			ctl = (ctl & ~(O_TEXT|O_BINARY|O_APPEND)) | oflags;
81da2e3ebdSchin 			sysfcntlf(f->file, F_SETFL, ctl);
82da2e3ebdSchin 		}
83da2e3ebdSchin 
84da2e3ebdSchin 		SFMTXRETURN(f,f);
85da2e3ebdSchin 	}
86da2e3ebdSchin 
87da2e3ebdSchin 	if(sflags&SF_STRING)
88da2e3ebdSchin 	{	f = sfnew(f,(char*)file,
89da2e3ebdSchin 		  	  file ? (size_t)strlen((char*)file) : (size_t)SF_UNBOUND,
90da2e3ebdSchin 		  	  -1,sflags);
91da2e3ebdSchin 	}
92da2e3ebdSchin 	else
93da2e3ebdSchin 	{	if(!file)
94da2e3ebdSchin 			return NIL(Sfio_t*);
95da2e3ebdSchin 
96da2e3ebdSchin #if _has_oflags /* open the file */
97da2e3ebdSchin 		while((fd = sysopenf((char*)file,oflags,SF_CREATMODE)) < 0 && errno == EINTR)
98da2e3ebdSchin 			errno = 0;
99da2e3ebdSchin #else
100da2e3ebdSchin 		while((fd = sysopenf(file,oflags&O_ACCMODE)) < 0 && errno == EINTR)
101da2e3ebdSchin 			errno = 0;
102da2e3ebdSchin 		if(fd >= 0)
103da2e3ebdSchin 		{	if((oflags&(O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) )
104da2e3ebdSchin 			{	CLOSE(fd);	/* error: file already exists */
105da2e3ebdSchin 				return NIL(Sfio_t*);
106da2e3ebdSchin 			}
107da2e3ebdSchin 			if(oflags&O_TRUNC )	/* truncate file */
108da2e3ebdSchin 			{	reg int	tf;
109da2e3ebdSchin 				while((tf = syscreatf(file,SF_CREATMODE)) < 0 &&
110da2e3ebdSchin 				      errno == EINTR)
111da2e3ebdSchin 					errno = 0;
112da2e3ebdSchin 				CLOSE(tf);
113da2e3ebdSchin 			}
114da2e3ebdSchin 		}
115da2e3ebdSchin 		else if(oflags&O_CREAT)
116da2e3ebdSchin 		{	while((fd = syscreatf(file,SF_CREATMODE)) < 0 && errno == EINTR)
117da2e3ebdSchin 				errno = 0;
118da2e3ebdSchin 			if((oflags&O_ACCMODE) != O_WRONLY)
119da2e3ebdSchin 			{	/* the file now exists, reopen it for read/write */
120da2e3ebdSchin 				CLOSE(fd);
121da2e3ebdSchin 				while((fd = sysopenf(file,oflags&O_ACCMODE)) < 0 &&
122da2e3ebdSchin 				      errno == EINTR)
123da2e3ebdSchin 					errno = 0;
124da2e3ebdSchin 			}
125da2e3ebdSchin 		}
126da2e3ebdSchin #endif
127da2e3ebdSchin 		if(fd < 0)
128da2e3ebdSchin 			return NIL(Sfio_t*);
129da2e3ebdSchin 
130da2e3ebdSchin 		/* we may have to reset the file descriptor to its old value */
131da2e3ebdSchin 		oldfd = f ? f->file : -1;
132da2e3ebdSchin 		if((f = sfnew(f,NIL(char*),(size_t)SF_UNBOUND,fd,sflags)) && oldfd >= 0)
133da2e3ebdSchin 			(void)sfsetfd(f,oldfd);
134da2e3ebdSchin 	}
135da2e3ebdSchin 
136da2e3ebdSchin 	return f;
137da2e3ebdSchin }
138da2e3ebdSchin 
139da2e3ebdSchin #if __STD_C
_sftype(reg const char * mode,int * oflagsp,int * uflagp)140da2e3ebdSchin int _sftype(reg const char* mode, int* oflagsp, int* uflagp)
141da2e3ebdSchin #else
142da2e3ebdSchin int _sftype(mode, oflagsp, uflagp)
143da2e3ebdSchin reg char*	mode;
144da2e3ebdSchin int*		oflagsp;
145da2e3ebdSchin int*		uflagp;
146da2e3ebdSchin #endif
147da2e3ebdSchin {
148da2e3ebdSchin 	reg int	sflags, oflags, uflag;
149da2e3ebdSchin 
150da2e3ebdSchin 	if(!mode)
151da2e3ebdSchin 		return 0;
152da2e3ebdSchin 
153da2e3ebdSchin 	/* construct the open flags */
154da2e3ebdSchin 	sflags = oflags = uflag = 0;
155da2e3ebdSchin 	while(1) switch(*mode++)
156da2e3ebdSchin 	{
157da2e3ebdSchin 	case 'a' :
158da2e3ebdSchin 		sflags |= SF_WRITE | SF_APPENDWR;
159da2e3ebdSchin 		oflags |= O_WRONLY | O_APPEND | O_CREAT;
160da2e3ebdSchin 		continue;
16134f9b3eeSRoland Mainz 	case 'b' :
16234f9b3eeSRoland Mainz 		oflags |= O_BINARY;
16334f9b3eeSRoland Mainz 		continue;
16434f9b3eeSRoland Mainz 	case 'm' :
16534f9b3eeSRoland Mainz 		sflags |= SF_MTSAFE;
16634f9b3eeSRoland Mainz 		uflag = 0;
16734f9b3eeSRoland Mainz 		continue;
168da2e3ebdSchin 	case 'r' :
169da2e3ebdSchin 		sflags |= SF_READ;
170da2e3ebdSchin 		oflags |= O_RDONLY;
171da2e3ebdSchin 		continue;
172da2e3ebdSchin 	case 's' :
173da2e3ebdSchin 		sflags |= SF_STRING;
174da2e3ebdSchin 		continue;
175da2e3ebdSchin 	case 't' :
176da2e3ebdSchin 		oflags |= O_TEXT;
177da2e3ebdSchin 		continue;
178da2e3ebdSchin 	case 'u' :
179da2e3ebdSchin 		sflags &= ~SF_MTSAFE;
180da2e3ebdSchin 		uflag = 1;
181da2e3ebdSchin 		continue;
18234f9b3eeSRoland Mainz 	case 'w' :
18334f9b3eeSRoland Mainz 		sflags |= SF_WRITE;
18434f9b3eeSRoland Mainz 		oflags |= O_WRONLY | O_CREAT;
18534f9b3eeSRoland Mainz 		if(!(sflags&SF_READ))
18634f9b3eeSRoland Mainz 			oflags |= O_TRUNC;
18734f9b3eeSRoland Mainz 		continue;
18834f9b3eeSRoland Mainz 	case 'x' :
18934f9b3eeSRoland Mainz 		oflags |= O_EXCL;
19034f9b3eeSRoland Mainz 		continue;
19134f9b3eeSRoland Mainz 	case 'F':
19234f9b3eeSRoland Mainz 		/* stdio compatibility -- fd >= FOPEN_MAX (or other magic number) ok */
19334f9b3eeSRoland Mainz 		continue;
194da2e3ebdSchin 	case 'W' :
195da2e3ebdSchin 		sflags |= SF_WCWIDTH;
196da2e3ebdSchin 		uflag = 0;
197da2e3ebdSchin 		continue;
19834f9b3eeSRoland Mainz 	case '+' :
19934f9b3eeSRoland Mainz 		if(sflags)
20034f9b3eeSRoland Mainz 			sflags |= SF_READ|SF_WRITE;
20134f9b3eeSRoland Mainz 		continue;
202da2e3ebdSchin 	default :
203da2e3ebdSchin 		if(!(oflags&O_CREAT) )
204da2e3ebdSchin 			oflags &= ~O_EXCL;
205da2e3ebdSchin #if _WIN32 && !_WINIX
206da2e3ebdSchin 		if(!(oflags&(O_BINARY|O_TEXT)))
207da2e3ebdSchin 			oflags |= O_BINARY;
208da2e3ebdSchin #endif
209da2e3ebdSchin 		if((sflags&SF_RDWR) == SF_RDWR)
210da2e3ebdSchin 			oflags = (oflags&~O_ACCMODE)|O_RDWR;
211da2e3ebdSchin 		if(oflagsp)
212da2e3ebdSchin 			*oflagsp = oflags;
213da2e3ebdSchin 		if(uflagp)
214da2e3ebdSchin 			*uflagp = uflag;
215da2e3ebdSchin 		if((sflags&(SF_STRING|SF_RDWR)) == SF_STRING)
216da2e3ebdSchin 			sflags |= SF_READ;
217da2e3ebdSchin 		return sflags;
218da2e3ebdSchin 	}
219da2e3ebdSchin }
220