win_posix.h 4.6 KB

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