dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libast / common / comp / open.c
blobbd9d4ee482b713607ccee16ec5aa7c3c62418cf8
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
25 * -last 3 arg open
28 #include <ast.h>
30 #if !defined(open) || !defined(_ast_O_LOCAL)
32 NoN(open)
34 #else
36 #undef open
38 extern int open(const char*, int, ...);
40 #include <ls.h>
41 #include <error.h>
43 #ifdef O_NOCTTY
44 #include <ast_tty.h>
45 #endif
47 int
48 _ast_open(const char* path, int op, ...)
50 int fd;
51 int mode;
52 int save_errno;
53 struct stat st;
54 va_list ap;
56 save_errno = errno;
57 va_start(ap, op);
58 mode = (op & O_CREAT) ? va_arg(ap, int) : S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
59 va_end(ap);
60 if (op & ~(_ast_O_LOCAL-1))
62 if (!(op & O_CREAT))
63 op &= ~O_EXCL;
64 for (;;)
66 if (op & O_TRUNC)
68 if ((op & O_EXCL) && !access(path, F_OK))
70 errno = EEXIST;
71 return(-1);
73 if ((fd = creat(path, (op & O_EXCL) ? 0 : mode)) < 0)
74 return(-1);
75 if (op & O_EXCL)
77 if (fstat(fd, &st) || (st.st_mode & S_IPERM))
79 errno = EEXIST;
80 close(fd);
81 return(-1);
83 #if _lib_fchmod
84 if (mode && fchmod(fd, mode))
85 #else
86 if (mode && chmod(path, mode))
87 #endif
88 errno = save_errno;
90 if ((op & O_ACCMODE) == O_RDWR)
92 close(fd);
93 op &= ~(O_CREAT|O_TRUNC);
94 continue;
97 else if ((fd = open(path, op & (_ast_O_LOCAL-1), mode)) < 0)
99 if (op & O_CREAT)
101 op |= O_TRUNC;
102 continue;
104 return(-1);
106 else if ((op & O_APPEND) && lseek(fd, 0L, SEEK_END) == -1L)
107 errno = save_errno;
108 #if O_NOCTTY
109 if ((op & O_NOCTTY) && ioctl(fd, TIOCNOTTY, 0))
110 errno = save_errno;
111 #endif
112 break;
115 else fd = open(path, op, mode);
116 return(fd);
119 #endif