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