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