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