70-stdarg.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdarg.h>
  22. int
  23. //stdarg1 (long one, ...)
  24. stdarg1 (long *one, ...)
  25. {
  26. va_list ap;
  27. char c;
  28. int r;
  29. va_start (ap, one);
  30. #if __GNUC__ && __x86_64__ && !SYSTEM_LIBC
  31. #define __FUNCTION_ARGS 1
  32. ap += (__FOO_VARARGS + (__FUNCTION_ARGS << 1)) << 3;
  33. #undef __FUNCTION_ARGS
  34. #endif
  35. c = va_arg (ap, char);
  36. r = c;
  37. eputs ("c:");
  38. eputs (itoa (c));
  39. eputs ("\n");
  40. va_end (ap);
  41. return r;
  42. }
  43. int
  44. ///stdarg2 (long one, long two, ...)
  45. stdarg2 (long *one, long *two, ...)
  46. {
  47. va_list ap;
  48. char c;
  49. int r;
  50. va_start (ap, two);
  51. #if __GNUC__ && __x86_64__ && !SYSTEM_LIBC
  52. #define __FUNCTION_ARGS 2
  53. ap += (__FOO_VARARGS + (__FUNCTION_ARGS << 1)) << 3;
  54. #undef __FUNCTION_ARGS
  55. #endif
  56. c = va_arg (ap, char);
  57. r = c;
  58. eputs ("c:");
  59. eputs (itoa (c));
  60. eputs ("\n");
  61. va_end (ap);
  62. return r;
  63. }
  64. int
  65. //stdarg3 (long one, long two, long three, ...)
  66. stdarg3 (long *one, long *two, long *three, ...)
  67. {
  68. va_list ap;
  69. char c;
  70. int r;
  71. va_start (ap, three);
  72. #if __GNUC__ && __x86_64__ && !SYSTEM_LIBC
  73. #define __FUNCTION_ARGS 3
  74. ap += (__FOO_VARARGS + (__FUNCTION_ARGS << 1)) << 3;
  75. #undef __FUNCTION_ARGS
  76. #endif
  77. c = va_arg (ap, char);
  78. r = c;
  79. eputs ("c:");
  80. eputs (itoa (c));
  81. eputs ("\n");
  82. va_end (ap);
  83. return r;
  84. }
  85. int
  86. main ()
  87. {
  88. char c = 'm';
  89. char buf[20];
  90. if (stdarg1 (-1, c) != c)
  91. return 1;
  92. c = 'w';
  93. if (stdarg2 (-1, -2, c) != c)
  94. return 2;
  95. c = 'g';
  96. if (stdarg3 (-1, -2, -3, c) != c)
  97. return 3;
  98. return 0;
  99. }