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