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