da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1985-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* http://www.opensource.org/licenses/cpl1.0.txt *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "sfhdr.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Open a file/string for IO.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** If f is not nil, it is taken as an existing stream that should be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** closed and its structure reused for the new stream.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin**
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if _BLD_sfio && defined(__EXPORT__)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define extern __EXPORT__
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#undef extern
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
7c2fbfb345896881c631598ee3852ce9ce33fb07April ChinSfio_t* _sfopen(Sfio_t* f, const char* file, const char* mode)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* _sfopen(f,file,mode)
7c2fbfb345896881c631598ee3852ce9ce33fb07April ChinSfio_t* f; /* old stream structure */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinchar* file; /* file/string to be opened */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chinchar* mode; /* mode of the stream */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int fd, oldfd, oflags, sflags;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin SFMTXDECL(f);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* get the control flags */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((sflags = _sftype(mode,&oflags,NIL(int*))) == 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Sfio_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* changing the control flags */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(f && !file && !((f->flags|sflags)&SF_STRING) )
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { SFMTXENTER(f, NIL(Sfio_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(f->mode&SF_INIT ) /* stream uninitialized, ok to set flags */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f->flags |= (sflags & (SF_FLAGS & ~SF_RDWR));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((sflags &= SF_RDWR) != 0) /* reset read/write modes */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f->flags = (f->flags & ~SF_RDWR) | sflags;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((f->flags&SF_RDWR) == SF_RDWR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->bits |= SF_BOTH;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else f->bits &= ~SF_BOTH;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(f->flags&SF_READ)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f->mode = (f->mode&~SF_WRITE)|SF_READ;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else f->mode = (f->mode&~SF_READ)|SF_WRITE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else /* make sure there is no buffered data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(sfsync(f) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f,NIL(Sfio_t*));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(f->file >= 0 && (oflags &= (O_TEXT|O_BINARY|O_APPEND)) != 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* set file access control */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int ctl = sysfcntlf(f->file, F_GETFL, 0);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ctl = (ctl & ~(O_TEXT|O_BINARY|O_APPEND)) | oflags;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sysfcntlf(f->file, F_SETFL, ctl);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin SFMTXRETURN(f,f);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(sflags&SF_STRING)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { f = sfnew(f,(char*)file,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin file ? (size_t)strlen((char*)file) : (size_t)SF_UNBOUND,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin -1,sflags);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(!file)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Sfio_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if _has_oflags /* open the file */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while((fd = sysopenf((char*)file,oflags,SF_CREATMODE)) < 0 && errno == EINTR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while((fd = sysopenf(file,oflags&O_ACCMODE)) < 0 && errno == EINTR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(fd >= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((oflags&(O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { CLOSE(fd); /* error: file already exists */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Sfio_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(oflags&O_TRUNC ) /* truncate file */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { reg int tf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while((tf = syscreatf(file,SF_CREATMODE)) < 0 &&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno == EINTR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin CLOSE(tf);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(oflags&O_CREAT)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { while((fd = syscreatf(file,SF_CREATMODE)) < 0 && errno == EINTR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((oflags&O_ACCMODE) != O_WRONLY)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* the file now exists, reopen it for read/write */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin CLOSE(fd);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while((fd = sysopenf(file,oflags&O_ACCMODE)) < 0 &&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno == EINTR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin errno = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(fd < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(Sfio_t*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* we may have to reset the file descriptor to its old value */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oldfd = f ? f->file : -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((f = sfnew(f,NIL(char*),(size_t)SF_UNBOUND,fd,sflags)) && oldfd >= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin (void)sfsetfd(f,oldfd);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint _sftype(reg const char* mode, int* oflagsp, int* uflagp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint _sftype(mode, oflagsp, uflagp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreg char* mode;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint* oflagsp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint* uflagp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg int sflags, oflags, uflag;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!mode)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* construct the open flags */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags = oflags = uflag = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while(1) switch(*mode++)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 'a' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags |= SF_WRITE | SF_APPENDWR;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags |= O_WRONLY | O_APPEND | O_CREAT;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case 'b' :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz oflags |= O_BINARY;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case 'm' :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz sflags |= SF_MTSAFE;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz uflag = 0;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 'r' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags |= SF_READ;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags |= O_RDONLY;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 's' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags |= SF_STRING;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 't' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags |= O_TEXT;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 'u' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags &= ~SF_MTSAFE;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin uflag = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case 'w' :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz sflags |= SF_WRITE;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz oflags |= O_WRONLY | O_CREAT;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if(!(sflags&SF_READ))
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz oflags |= O_TRUNC;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case 'x' :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz oflags |= O_EXCL;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case 'F':
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz /* stdio compatibility -- fd >= FOPEN_MAX (or other magic number) ok */
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 'W' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags |= SF_WCWIDTH;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin uflag = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin continue;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz case '+' :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if(sflags)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz sflags |= SF_READ|SF_WRITE;
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz continue;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin default :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(oflags&O_CREAT) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags &= ~O_EXCL;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if _WIN32 && !_WINIX
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(oflags&(O_BINARY|O_TEXT)))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags |= O_BINARY;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((sflags&SF_RDWR) == SF_RDWR)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oflags = (oflags&~O_ACCMODE)|O_RDWR;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(oflagsp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *oflagsp = oflags;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(uflagp)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *uflagp = uflag;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((sflags&(SF_STRING|SF_RDWR)) == SF_STRING)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sflags |= SF_READ;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return sflags;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}