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