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