1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin 
24*4887Schin /*
25*4887Schin  * -last 3 arg open
26*4887Schin  */
27*4887Schin 
28*4887Schin #include <ast.h>
29*4887Schin 
30*4887Schin #if !defined(open) || !defined(_ast_O_LOCAL)
31*4887Schin 
32*4887Schin NoN(open)
33*4887Schin 
34*4887Schin #else
35*4887Schin 
36*4887Schin #undef	open
37*4887Schin 
38*4887Schin extern int	open(const char*, int, ...);
39*4887Schin 
40*4887Schin #include <ls.h>
41*4887Schin #include <error.h>
42*4887Schin 
43*4887Schin #ifdef O_NOCTTY
44*4887Schin #include <ast_tty.h>
45*4887Schin #endif
46*4887Schin 
47*4887Schin int
48*4887Schin _ast_open(const char* path, int op, ...)
49*4887Schin {
50*4887Schin 	int		fd;
51*4887Schin 	int		mode;
52*4887Schin 	int		save_errno;
53*4887Schin 	struct stat	st;
54*4887Schin 	va_list		ap;
55*4887Schin 
56*4887Schin 	save_errno = errno;
57*4887Schin 	va_start(ap, op);
58*4887Schin 	mode = (op & O_CREAT) ? va_arg(ap, int) : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
59*4887Schin 	va_end(ap);
60*4887Schin 	if (op & ~(_ast_O_LOCAL-1))
61*4887Schin 	{
62*4887Schin 		if (!(op & O_CREAT))
63*4887Schin 			op &= ~O_EXCL;
64*4887Schin 		for (;;)
65*4887Schin 		{
66*4887Schin 			if (op & O_TRUNC)
67*4887Schin 			{
68*4887Schin 				if ((op & O_EXCL) && !access(path, F_OK))
69*4887Schin 				{
70*4887Schin 					errno = EEXIST;
71*4887Schin 					return(-1);
72*4887Schin 				}
73*4887Schin 				if ((fd = creat(path, (op & O_EXCL) ? 0 : mode)) < 0)
74*4887Schin 					return(-1);
75*4887Schin 				if (op & O_EXCL)
76*4887Schin 				{
77*4887Schin 					if (fstat(fd, &st) || (st.st_mode & S_IPERM))
78*4887Schin 					{
79*4887Schin 						errno = EEXIST;
80*4887Schin 						close(fd);
81*4887Schin 						return(-1);
82*4887Schin 					}
83*4887Schin #if _lib_fchmod
84*4887Schin 					if (mode && fchmod(fd, mode))
85*4887Schin #else
86*4887Schin 					if (mode && chmod(path, mode))
87*4887Schin #endif
88*4887Schin 						errno = save_errno;
89*4887Schin 				}
90*4887Schin 				if ((op & O_ACCMODE) == O_RDWR)
91*4887Schin 				{
92*4887Schin 					close(fd);
93*4887Schin 					op &= ~(O_CREAT|O_TRUNC);
94*4887Schin 					continue;
95*4887Schin 				}
96*4887Schin 			}
97*4887Schin 			else if ((fd = open(path, op & (_ast_O_LOCAL-1), mode)) < 0)
98*4887Schin 			{
99*4887Schin 				if (op & O_CREAT)
100*4887Schin 				{
101*4887Schin 					op |= O_TRUNC;
102*4887Schin 					continue;
103*4887Schin 				}
104*4887Schin 				return(-1);
105*4887Schin 			}
106*4887Schin 			else if ((op & O_APPEND) && lseek(fd, 0L, SEEK_END) == -1L)
107*4887Schin 				errno = save_errno;
108*4887Schin #if O_NOCTTY
109*4887Schin 			if ((op & O_NOCTTY) && ioctl(fd, TIOCNOTTY, 0))
110*4887Schin 				errno = save_errno;
111*4887Schin #endif
112*4887Schin 			break;
113*4887Schin 		}
114*4887Schin 	}
115*4887Schin 	else fd = open(path, op, mode);
116*4887Schin 	return(fd);
117*4887Schin }
118*4887Schin 
119*4887Schin #endif
120