tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / _open.c
blobf76e92e76de93beb015aaf75d9912031068b21fa
1 /* $Id$
3 * _open.c - Unix compatible open() (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 #include <ios1.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <dos.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <dos/dos.h>
17 #include <proto/dos.h>
18 #include <proto/usergroup.h>
19 #include <stdarg.h>
20 #include <unistd.h>
22 #include <bsdsocket.h>
24 #include "netlib.h"
26 extern int (*__closefunc)(int);
28 __stdargs int
29 __open(const char *name, int mode, ...)
31 struct UFB *ufb;
32 int fd;
33 int flags;
34 char newfile = TRUE;
35 BPTR file;
38 * Set up __closefunc (which is used at cleanup)
40 __closefunc = __close;
43 * Check for the break signals
45 __chkabort();
48 * find first free ufb
50 ufb = __allocufb(&fd);
51 if (ufb == NULL)
52 return -1; /* errno is set by the __allocufb() */
55 * malloc space for the name & copy it
57 if ((ufb->ufbfn = malloc(strlen(name)+1)) == NULL) {
58 SET_OSERR(ERROR_NO_FREE_STORE);
59 errno = ENOMEM;
60 return -1;
62 strcpy(ufb->ufbfn, name);
64 * Translate mode to ufb flags
66 switch (mode & (O_WRONLY | O_RDWR)) {
67 case O_RDONLY:
68 if (mode & (O_APPEND | O_CREAT | O_TRUNC | O_EXCL)) {
69 errno = EINVAL;
70 return -1;
72 flags = UFB_RA;
73 break;
74 case O_WRONLY:
75 flags = UFB_WA;
76 break;
77 case O_RDWR:
78 flags = UFB_RA | UFB_WA;
79 break;
80 default:
81 errno = EINVAL;
82 return -1;
84 if (mode & O_APPEND)
85 flags |= UFB_APP;
86 if (mode & O_XLATE)
87 flags |= UFB_XLAT;
88 if (mode & O_TEMP)
89 flags |= UFB_TEMP;
90 if (mode & O_CREAT) {
91 BPTR lock;
92 if (lock = Lock((char *)name, SHARED_LOCK)) {
93 if (mode & O_EXCL) {
94 UnLock(lock);
95 errno = EEXIST;
96 free(ufb->ufbfn);
97 return -1;
100 if (mode & O_TRUNC)
101 newfile = FALSE;
102 else
103 mode &= ~O_CREAT;
105 UnLock(lock);
108 if (mode & O_CREAT) {
109 if ((file = Open((char *)name, MODE_NEWFILE)) == NULL)
110 goto osfail;
112 if (newfile) {
113 va_list va;
114 int cmode;
116 va_start(va, mode);
118 cmode = va_arg(va, int) & ~getumask();
120 chmod((char *)name, cmode); /* hope this doesn't fail :-) */
123 else {
124 if ((file = Open((char *)name,
125 (flags & UFB_WA && mode & O_LOCK) ?
126 MODE_READWRITE : MODE_OLDFILE)) == NULL)
127 goto osfail;
131 * All done! Setting the ufb->ufbflg field to non-zero value marks
132 * this ufb used.
134 ufb->ufbflg = flags;
135 ufb->ufbfh = (long)file;
137 return fd;
139 osfail:
141 int code = IoErr();
142 if (ufb->ufbfn)
143 free(ufb->ufbfn);
144 set_errno(code);
146 return -1;