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