1 /*****************************************************************************\
2 * Copyright (c) 2004 Mark Aylett <mark@emantic.co.uk> *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, sublicense, and/or sell copies of the Software, and to permit *
9 * persons to whom the Software is furnished to do so, subject to the *
10 * following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN *
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 \*****************************************************************************/
23
24 static const char rcsid[] = "$Id: mar_flags_c.c,v 1.3 2004/12/14 13:09:23 marayl Exp $";
25
26 #include "mar_flags_c.h"
27
28 #include "mar_debug_c.h"
29 #include "mar_types_c.h"
30
31 #include <assert.h>
32 #include <errno.h>
33
34 #define MAR_ALL (MAR_RDONLY | MAR_WRONLY | MAR_RDWR \
35 | MAR_APPEND | MAR_CREAT | MAR_TRUNC | MAR_EXCL)
36
37 MAR_EXTERN int
mar_toflags_(int * to,int from)38 mar_toflags_(int* to, int from)
39 {
40 #if !defined(WIN32)
41 int flags = 0;
42 #else /* WIN32 */
43 int flags = O_BINARY;
44 #endif /* WIN32 */
45
46 assert(to);
47 if (from & ~MAR_ALL)
48 goto fail;
49
50 switch (from & (MAR_RDONLY | MAR_WRONLY | MAR_RDWR)) {
51 case MAR_RDONLY:
52 flags |= O_RDONLY;
53 break;
54 case MAR_WRONLY:
55 case MAR_RDWR:
56 flags |= O_RDWR;
57 break;
58 default:
59 goto fail;
60 }
61 if (from & MAR_APPEND) {
62 assert(MAR_APPEND == (from & MAR_APPEND));
63 flags |= MAR_APPEND;
64 }
65 if (from & MAR_CREAT) {
66 assert(MAR_CREAT == (from & MAR_CREAT));
67 flags |= O_CREAT;
68 }
69 if (from & MAR_TRUNC) {
70 assert(MAR_TRUNC == (from & MAR_TRUNC));
71 flags |= O_TRUNC;
72 }
73 if (from & MAR_EXCL) {
74 assert(MAR_EXCL == (from & MAR_EXCL));
75 flags |= O_EXCL;
76 }
77 *to = flags;
78 return 0;
79
80 fail:
81
82 MAR_DEBUG_MSG("Failed to map open flags:"
83 " Invalid source value\n");
84 errno = EINVAL;
85 return -1;
86 }
87