1 /*
2 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef WIN_POSIX_H
8 #define WIN_POSIX_H
9
10 #define _CRT_SECURE_NO_WARNINGS
11
12 #include <direct.h>
13 #include <io.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18
19 #include "uuid.h"
20
21 /* Derive or provide Windows equivalents of Posix/GCC/Unix stuff. */
22 #ifndef PATH_MAX
23 # ifdef MAX_PATH
24 # define PATH_MAX MAX_PATH
25 # else
26 # ifdef _MAX_PATH
27 # define MAX_PATH _MAX_PATH
28 # define PATH_MAX _MAX_PATH
29 # else
30 # define PATH_MAX 260
31 # endif
32 # endif
33 #endif
34
35 #ifndef _CRT_SECURE_NO_WARNINGS
36 # define _CRT_SECURE_NO_WARNINGS 1
37 #endif
38
39 /*
40 * Platform specific names.
41 *
42 * Visual Studio deprecates a number of POSIX functions and only provides
43 * ISO C++ compliant alternatives (distinguished by their '_' prefix).
44 * These macros help provide a stopgap for that.
45 */
46
47 /* fileno cannot be an inline function, because _fileno is a macro. */
48 #define fileno(fileptr) _fileno(fileptr)
49
50 /* _fstat uses the _stat structure, not stat. */
51 #define BLD_PLAT_STAT _stat
52
53 /* Define flag values for _access. */
54 #define F_OK 0
55
56
57 /* getopt implementation for Windows: Data. */
58
59 /* Legitimate values for option.has_arg. */
60 enum has_arg_values {
61 no_argument, /* No argument value required */
62 required_argument, /* value must be specified. */
63 optional_argument /* value may be specified. */
64 };
65
66 /* Long option table entry for get_opt_long. */
67 struct option {
68 /* The name of the option. */
69 const char *name;
70
71 /*
72 * Indicates whether the option takes an argument.
73 * Possible values: see has_arg_values above.
74 */
75 int has_arg;
76
77 /* If not null, when option present, *flag is set to val. */
78 int *flag;
79
80 /*
81 * The value associated with this option to return
82 * (and save in *flag when not null)
83 */
84 int val;
85 };
86
87 /*
88 * This variable is set by getopt to point at the value of the option
89 * argument, for those options that accept arguments.
90 */
91 extern char *optarg;
92
93 /*
94 * When this variable is not zero, getopt emits an error message to stderr
95 * if it encounters an unspecified option, or a missing argument.
96 * Otherwise no message is reported.
97 */
98 extern const int opterr; /* const as NOT used in this implementation. */
99
100 /*
101 * This variable is set by getopt to the index of the next element of the
102 * argv array to be processed. Once getopt has found all of the option
103 * arguments, you can use this variable to determine where the remaining
104 * non-option arguments begin. The initial value of this variable is 1.
105 */
106 extern int optind;
107
108 /*
109 * When getopt encounters an unknown option character or an option with a
110 * missing required argument, it stores that option character in this
111 * variable.
112 */
113 extern int optopt;
114
115
116 /*
117 * Platform specific names.
118 *
119 * Visual Studio deprecates a number of POSIX functions and only provides
120 * ISO C++ compliant alternatives (distinguished by their '_' prefix).
121 * These inline functions provide a stopgap for that.
122 */
123
access(const char * path,int mode)124 inline int access(const char *path, int mode)
125 {
126 return _access(path, mode);
127 }
128
chdir(const char * s)129 inline int chdir(const char *s)
130 {
131 return _chdir(s);
132 }
133
fstat(int fd,struct _stat * buffer)134 inline int fstat(int fd, struct _stat *buffer)
135 {
136 return _fstat(fd, buffer);
137 }
138
strdup(const char * s)139 inline char *strdup(const char *s)
140 {
141 return _strdup(s);
142 }
143
144 /*
145 * getopt implementation for Windows: Functions.
146 *
147 * Windows does not have the getopt family of functions, as it normally
148 * uses '/' instead of '-' as the command line option delimiter.
149 * These functions provide a Windows version that uses '-', which precludes
150 * using '-' as the intial letter of a program argument.
151 * This is not seen as a problem in the specific instance of fiptool,
152 * and enables existing makefiles to work on a Windows build environment.
153 */
154
155 /*
156 * The getopt function gets the next option argument from the argument list
157 * specified by the argv and argc arguments.
158 */
159 int getopt(int argc,
160 char *argv[],
161 char *options);
162
163 /*
164 * getopt_long gets the next option argument from the argument list
165 * specified by the argv and argc arguments. Options may be either short
166 * (single letter) as for getopt, or longer names (preceded by --).
167 */
168 int getopt_long(int argc,
169 char *argv[],
170 const char *shortopts,
171 const struct option *longopts,
172 int *indexptr);
173
174 /*
175 * getopt_long_only gets the next option argument from the argument list
176 * specified by the argv and argc arguments. Options may be either short
177 * or long as for getopt_long, but the long names may have a single '-'
178 * prefix, too.
179 */
180 int getopt_long_only(int argc,
181 char *argv[],
182 const char *shortopts,
183 const struct option *longopts,
184 int *indexptr);
185
186 #endif /* WIN_POSIX_H */
187