xref: /onnv-gate/usr/src/lib/libast/common/comp/open.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * -last 3 arg open
264887Schin  */
274887Schin 
284887Schin #include <ast.h>
294887Schin 
304887Schin #if !defined(open) || !defined(_ast_O_LOCAL)
314887Schin 
324887Schin NoN(open)
334887Schin 
344887Schin #else
354887Schin 
364887Schin #undef	open
374887Schin 
384887Schin extern int	open(const char*, int, ...);
394887Schin 
404887Schin #include <ls.h>
414887Schin #include <error.h>
424887Schin 
434887Schin #ifdef O_NOCTTY
444887Schin #include <ast_tty.h>
454887Schin #endif
464887Schin 
474887Schin int
484887Schin _ast_open(const char* path, int op, ...)
494887Schin {
504887Schin 	int		fd;
514887Schin 	int		mode;
524887Schin 	int		save_errno;
534887Schin 	struct stat	st;
544887Schin 	va_list		ap;
554887Schin 
564887Schin 	save_errno = errno;
574887Schin 	va_start(ap, op);
584887Schin 	mode = (op & O_CREAT) ? va_arg(ap, int) : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
594887Schin 	va_end(ap);
604887Schin 	if (op & ~(_ast_O_LOCAL-1))
614887Schin 	{
624887Schin 		if (!(op & O_CREAT))
634887Schin 			op &= ~O_EXCL;
644887Schin 		for (;;)
654887Schin 		{
664887Schin 			if (op & O_TRUNC)
674887Schin 			{
684887Schin 				if ((op & O_EXCL) && !access(path, F_OK))
694887Schin 				{
704887Schin 					errno = EEXIST;
714887Schin 					return(-1);
724887Schin 				}
734887Schin 				if ((fd = creat(path, (op & O_EXCL) ? 0 : mode)) < 0)
744887Schin 					return(-1);
754887Schin 				if (op & O_EXCL)
764887Schin 				{
774887Schin 					if (fstat(fd, &st) || (st.st_mode & S_IPERM))
784887Schin 					{
794887Schin 						errno = EEXIST;
804887Schin 						close(fd);
814887Schin 						return(-1);
824887Schin 					}
834887Schin #if _lib_fchmod
844887Schin 					if (mode && fchmod(fd, mode))
854887Schin #else
864887Schin 					if (mode && chmod(path, mode))
874887Schin #endif
884887Schin 						errno = save_errno;
894887Schin 				}
904887Schin 				if ((op & O_ACCMODE) == O_RDWR)
914887Schin 				{
924887Schin 					close(fd);
934887Schin 					op &= ~(O_CREAT|O_TRUNC);
944887Schin 					continue;
954887Schin 				}
964887Schin 			}
974887Schin 			else if ((fd = open(path, op & (_ast_O_LOCAL-1), mode)) < 0)
984887Schin 			{
994887Schin 				if (op & O_CREAT)
1004887Schin 				{
1014887Schin 					op |= O_TRUNC;
1024887Schin 					continue;
1034887Schin 				}
1044887Schin 				return(-1);
1054887Schin 			}
1064887Schin 			else if ((op & O_APPEND) && lseek(fd, 0L, SEEK_END) == -1L)
1074887Schin 				errno = save_errno;
1084887Schin #if O_NOCTTY
1094887Schin 			if ((op & O_NOCTTY) && ioctl(fd, TIOCNOTTY, 0))
1104887Schin 				errno = save_errno;
1114887Schin #endif
1124887Schin 			break;
1134887Schin 		}
1144887Schin 	}
1154887Schin 	else fd = open(path, op, mode);
1164887Schin 	return(fd);
1174887Schin }
1184887Schin 
1194887Schin #endif
120