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