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#pragma prototyped
1N/A
1N/A/*
1N/A * -last 3 arg open
1N/A */
1N/A
1N/A#include <ast.h>
1N/A
1N/A#if !defined(open) || !defined(_ast_O_LOCAL)
1N/A
1N/ANoN(open)
1N/A
1N/A#else
1N/A
1N/A#undef open
1N/A
1N/Aextern int open(const char*, int, ...);
1N/A
1N/A#include <ls.h>
1N/A#include <error.h>
1N/A
1N/A#ifdef O_NOCTTY
1N/A#include <ast_tty.h>
1N/A#endif
1N/A
1N/Aint
1N/A_ast_open(const char* path, int op, ...)
1N/A{
1N/A int fd;
1N/A int mode;
1N/A int save_errno;
1N/A struct stat st;
1N/A va_list ap;
1N/A
1N/A save_errno = errno;
1N/A va_start(ap, op);
1N/A mode = (op & O_CREAT) ? va_arg(ap, int) : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
1N/A va_end(ap);
1N/A if (op & ~(_ast_O_LOCAL-1))
1N/A {
1N/A if (!(op & O_CREAT))
1N/A op &= ~O_EXCL;
1N/A for (;;)
1N/A {
1N/A if (op & O_TRUNC)
1N/A {
1N/A if ((op & O_EXCL) && !access(path, F_OK))
1N/A {
1N/A errno = EEXIST;
1N/A return(-1);
1N/A }
1N/A if ((fd = creat(path, (op & O_EXCL) ? 0 : mode)) < 0)
1N/A return(-1);
1N/A if (op & O_EXCL)
1N/A {
1N/A if (fstat(fd, &st) || (st.st_mode & S_IPERM))
1N/A {
1N/A errno = EEXIST;
1N/A close(fd);
1N/A return(-1);
1N/A }
1N/A#if _lib_fchmod
1N/A if (mode && fchmod(fd, mode))
1N/A#else
1N/A if (mode && chmod(path, mode))
1N/A#endif
1N/A errno = save_errno;
1N/A }
1N/A if ((op & O_ACCMODE) == O_RDWR)
1N/A {
1N/A close(fd);
1N/A op &= ~(O_CREAT|O_TRUNC);
1N/A continue;
1N/A }
1N/A }
1N/A else if ((fd = open(path, op & (_ast_O_LOCAL-1), mode)) < 0)
1N/A {
1N/A if (op & O_CREAT)
1N/A {
1N/A op |= O_TRUNC;
1N/A continue;
1N/A }
1N/A return(-1);
1N/A }
1N/A else if ((op & O_APPEND) && lseek(fd, 0L, SEEK_END) == -1L)
1N/A errno = save_errno;
1N/A#if O_NOCTTY
1N/A if ((op & O_NOCTTY) && ioctl(fd, TIOCNOTTY, 0))
1N/A errno = save_errno;
1N/A#endif
1N/A break;
1N/A }
1N/A }
1N/A else fd = open(path, op, mode);
1N/A return(fd);
1N/A}
1N/A
1N/A#endif