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 #pragma prototyped 23da2e3ebdSchin 24da2e3ebdSchin /* 25da2e3ebdSchin * -last 3 arg open 26da2e3ebdSchin */ 27da2e3ebdSchin 28da2e3ebdSchin #include <ast.h> 29da2e3ebdSchin 30da2e3ebdSchin #if !defined(open) || !defined(_ast_O_LOCAL) 31da2e3ebdSchin 32da2e3ebdSchin NoN(open) 33da2e3ebdSchin 34da2e3ebdSchin #else 35da2e3ebdSchin 36da2e3ebdSchin #undef open 37da2e3ebdSchin 38da2e3ebdSchin extern int open(const char*, int, ...); 39da2e3ebdSchin 40da2e3ebdSchin #include <ls.h> 41da2e3ebdSchin #include <error.h> 42da2e3ebdSchin 43da2e3ebdSchin #ifdef O_NOCTTY 44da2e3ebdSchin #include <ast_tty.h> 45da2e3ebdSchin #endif 46da2e3ebdSchin 47da2e3ebdSchin int 48da2e3ebdSchin _ast_open(const char* path, int op, ...) 49da2e3ebdSchin { 50da2e3ebdSchin int fd; 51da2e3ebdSchin int mode; 52da2e3ebdSchin int save_errno; 53da2e3ebdSchin struct stat st; 54da2e3ebdSchin va_list ap; 55da2e3ebdSchin 56da2e3ebdSchin save_errno = errno; 57da2e3ebdSchin va_start(ap, op); 58da2e3ebdSchin mode = (op & O_CREAT) ? va_arg(ap, int) : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; 59da2e3ebdSchin va_end(ap); 60da2e3ebdSchin if (op & ~(_ast_O_LOCAL-1)) 61da2e3ebdSchin { 62da2e3ebdSchin if (!(op & O_CREAT)) 63da2e3ebdSchin op &= ~O_EXCL; 64da2e3ebdSchin for (;;) 65da2e3ebdSchin { 66da2e3ebdSchin if (op & O_TRUNC) 67da2e3ebdSchin { 68da2e3ebdSchin if ((op & O_EXCL) && !access(path, F_OK)) 69da2e3ebdSchin { 70da2e3ebdSchin errno = EEXIST; 71da2e3ebdSchin return(-1); 72da2e3ebdSchin } 73da2e3ebdSchin if ((fd = creat(path, (op & O_EXCL) ? 0 : mode)) < 0) 74da2e3ebdSchin return(-1); 75da2e3ebdSchin if (op & O_EXCL) 76da2e3ebdSchin { 77da2e3ebdSchin if (fstat(fd, &st) || (st.st_mode & S_IPERM)) 78da2e3ebdSchin { 79da2e3ebdSchin errno = EEXIST; 80da2e3ebdSchin close(fd); 81da2e3ebdSchin return(-1); 82da2e3ebdSchin } 83da2e3ebdSchin #if _lib_fchmod 84da2e3ebdSchin if (mode && fchmod(fd, mode)) 85da2e3ebdSchin #else 86da2e3ebdSchin if (mode && chmod(path, mode)) 87da2e3ebdSchin #endif 88da2e3ebdSchin errno = save_errno; 89da2e3ebdSchin } 90da2e3ebdSchin if ((op & O_ACCMODE) == O_RDWR) 91da2e3ebdSchin { 92da2e3ebdSchin close(fd); 93da2e3ebdSchin op &= ~(O_CREAT|O_TRUNC); 94da2e3ebdSchin continue; 95da2e3ebdSchin } 96da2e3ebdSchin } 97da2e3ebdSchin else if ((fd = open(path, op & (_ast_O_LOCAL-1), mode)) < 0) 98da2e3ebdSchin { 99da2e3ebdSchin if (op & O_CREAT) 100da2e3ebdSchin { 101da2e3ebdSchin op |= O_TRUNC; 102da2e3ebdSchin continue; 103da2e3ebdSchin } 104da2e3ebdSchin return(-1); 105da2e3ebdSchin } 106da2e3ebdSchin else if ((op & O_APPEND) && lseek(fd, 0L, SEEK_END) == -1L) 107da2e3ebdSchin errno = save_errno; 108da2e3ebdSchin #if O_NOCTTY 109da2e3ebdSchin if ((op & O_NOCTTY) && ioctl(fd, TIOCNOTTY, 0)) 110da2e3ebdSchin errno = save_errno; 111da2e3ebdSchin #endif 112da2e3ebdSchin break; 113da2e3ebdSchin } 114da2e3ebdSchin } 115da2e3ebdSchin else fd = open(path, op, mode); 116da2e3ebdSchin return(fd); 117da2e3ebdSchin } 118da2e3ebdSchin 119da2e3ebdSchin #endif 120