err.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. err.c
  9. Abstract:
  10. This module implements support for the old err/warn functions.
  11. Author:
  12. Evan Green 25-Jul-2016
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <err.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. //
  34. // -------------------------------------------------------------------- Globals
  35. //
  36. //
  37. // ------------------------------------------------------------------ Functions
  38. //
  39. LIBC_API
  40. void
  41. err (
  42. int ExitCode,
  43. const char *Format,
  44. ...
  45. )
  46. /*++
  47. Routine Description:
  48. This routine prints the program name, the given printf-style formatted
  49. string, and the string of the current errno, separated by a colon and a
  50. space. This routine then exits the current program with the given exit
  51. value.
  52. Arguments:
  53. ExitCode - Supplies the value to pass to exit.
  54. Format - Supplies the printf style format string.
  55. ... - Supplies the remaining arguments to the string.
  56. Return Value:
  57. This routine does not return, as it calls exit.
  58. --*/
  59. {
  60. va_list Arguments;
  61. va_start(Arguments, Format);
  62. verr(ExitCode, Format, Arguments);
  63. va_end(Arguments);
  64. return;
  65. }
  66. LIBC_API
  67. void
  68. verr (
  69. int ExitCode,
  70. const char *Format,
  71. va_list Arguments
  72. )
  73. /*++
  74. Routine Description:
  75. This routine prints the program name, the given printf-style formatted
  76. string, and the string of the current errno, separated by a colon and a
  77. space. This routine then exits the current program with the given exit
  78. value.
  79. Arguments:
  80. ExitCode - Supplies the value to pass to exit.
  81. Format - Supplies the printf style format string.
  82. Arguments - Supplies the remaining arguments to the string.
  83. Return Value:
  84. This routine does not return, as it calls exit.
  85. --*/
  86. {
  87. PPROCESS_ENVIRONMENT Environment;
  88. Environment = OsGetCurrentEnvironment();
  89. fprintf(stderr, "%s: ", Environment->Arguments[0]);
  90. if (Format != NULL) {
  91. vfprintf(stderr, Format, Arguments);
  92. fprintf(stderr, ": ");
  93. }
  94. fprintf(stderr, "%s\n", strerror(errno));
  95. exit(ExitCode);
  96. return;
  97. }
  98. LIBC_API
  99. void
  100. errx (
  101. int ExitCode,
  102. const char *Format,
  103. ...
  104. )
  105. /*++
  106. Routine Description:
  107. This routine prints the program name, and the given printf-style formatted
  108. string, separated by a colon and a space. This routine then exits the
  109. current program with the given exit value.
  110. Arguments:
  111. ExitCode - Supplies the value to pass to exit.
  112. Format - Supplies the printf style format string.
  113. ... - Supplies the remaining arguments to the string.
  114. Return Value:
  115. This routine does not return, as it calls exit.
  116. --*/
  117. {
  118. va_list Arguments;
  119. va_start(Arguments, Format);
  120. verrx(ExitCode, Format, Arguments);
  121. va_end(Arguments);
  122. return;
  123. }
  124. LIBC_API
  125. void
  126. verrx (
  127. int ExitCode,
  128. const char *Format,
  129. va_list Arguments
  130. )
  131. /*++
  132. Routine Description:
  133. This routine prints the program name, and the given printf-style formatted
  134. string, separated by a colon and a space. This routine then exits the
  135. current program with the given exit value.
  136. Arguments:
  137. ExitCode - Supplies the value to pass to exit.
  138. Format - Supplies the printf style format string.
  139. Arguments - Supplies the remaining arguments to the string.
  140. Return Value:
  141. This routine does not return, as it calls exit.
  142. --*/
  143. {
  144. PPROCESS_ENVIRONMENT Environment;
  145. Environment = OsGetCurrentEnvironment();
  146. fprintf(stderr, "%s: ", Environment->Arguments[0]);
  147. if (Format != NULL) {
  148. vfprintf(stderr, Format, Arguments);
  149. fprintf(stderr, ": ");
  150. }
  151. fprintf(stderr, "\n");
  152. exit(ExitCode);
  153. return;
  154. }
  155. LIBC_API
  156. void
  157. warn (
  158. const char *Format,
  159. ...
  160. )
  161. /*++
  162. Routine Description:
  163. This routine prints the program name, the given printf-style formatted
  164. string, and the string of the current errno, separated by a colon and a
  165. space.
  166. Arguments:
  167. Format - Supplies the printf style format string.
  168. ... - Supplies the remaining arguments to the string.
  169. Return Value:
  170. None.
  171. --*/
  172. {
  173. va_list Arguments;
  174. va_start(Arguments, Format);
  175. vwarn(Format, Arguments);
  176. va_end(Arguments);
  177. return;
  178. }
  179. LIBC_API
  180. void
  181. vwarn (
  182. const char *Format,
  183. va_list Arguments
  184. )
  185. /*++
  186. Routine Description:
  187. This routine prints the program name, the given printf-style formatted
  188. string, and the string of the current errno, separated by a colon and a
  189. space.
  190. Arguments:
  191. Format - Supplies the printf style format string.
  192. Arguments - Supplies the remaining arguments to the string.
  193. Return Value:
  194. None.
  195. --*/
  196. {
  197. PPROCESS_ENVIRONMENT Environment;
  198. Environment = OsGetCurrentEnvironment();
  199. fprintf(stderr, "%s: ", Environment->Arguments[0]);
  200. if (Format != NULL) {
  201. vfprintf(stderr, Format, Arguments);
  202. fprintf(stderr, ": ");
  203. }
  204. fprintf(stderr, "%s\n", strerror(errno));
  205. return;
  206. }
  207. LIBC_API
  208. void
  209. warnx (
  210. const char *Format,
  211. ...
  212. )
  213. /*++
  214. Routine Description:
  215. This routine prints the program name, and the given printf-style formatted
  216. string, separated by a colon and a space.
  217. Arguments:
  218. Format - Supplies the printf style format string.
  219. ... - Supplies the remaining arguments to the string.
  220. Return Value:
  221. None.
  222. --*/
  223. {
  224. va_list Arguments;
  225. va_start(Arguments, Format);
  226. vwarnx(Format, Arguments);
  227. va_end(Arguments);
  228. return;
  229. }
  230. LIBC_API
  231. void
  232. vwarnx (
  233. const char *Format,
  234. va_list Arguments
  235. )
  236. /*++
  237. Routine Description:
  238. This routine prints the program name, and the given printf-style formatted
  239. string, separated by a colon and a space.
  240. Arguments:
  241. Format - Supplies the printf style format string.
  242. Arguments - Supplies the remaining arguments to the string.
  243. Return Value:
  244. None.
  245. --*/
  246. {
  247. PPROCESS_ENVIRONMENT Environment;
  248. Environment = OsGetCurrentEnvironment();
  249. fprintf(stderr, "%s: ", Environment->Arguments[0]);
  250. if (Format != NULL) {
  251. vfprintf(stderr, Format, Arguments);
  252. fprintf(stderr, ": ");
  253. }
  254. fprintf(stderr, "\n");
  255. return;
  256. }
  257. //
  258. // --------------------------------------------------------- Internal Functions
  259. //