block.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * block.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <limits.h>
  21. #include <unistd.h>
  22. /*====================================================================*
  23. * custom header files;
  24. *--------------------------------------------------------------------*/
  25. #include "../tools/getoptv.h"
  26. #include "../tools/putoptv.h"
  27. #include "../tools/version.h"
  28. #include "../tools/error.h"
  29. /*====================================================================*
  30. * custom source files;
  31. *--------------------------------------------------------------------*/
  32. #ifndef MAKEFILE
  33. #include "../tools/getoptv.c"
  34. #include "../tools/putoptv.c"
  35. #include "../tools/version.c"
  36. #include "../tools/uintspec.c"
  37. #include "../tools/todigit.c"
  38. #include "../tools/error.c"
  39. #endif
  40. /*====================================================================*
  41. * program constants;
  42. *--------------------------------------------------------------------*/
  43. #define MAIN_LENGTH 1024
  44. #define MAIN_BYTE 0x00
  45. /*====================================================================*
  46. *
  47. * void function (unsigned length, unsigned char byte);
  48. *
  49. * write the specified number of byte values to stdout;
  50. *
  51. *--------------------------------------------------------------------*/
  52. static void function (unsigned length, unsigned char byte)
  53. {
  54. while (length--)
  55. {
  56. write (STDOUT_FILENO, & byte, sizeof (byte));
  57. }
  58. return;
  59. }
  60. /*====================================================================*
  61. *
  62. * int main (int argc, char const * argv []);
  63. *
  64. *--------------------------------------------------------------------*/
  65. int main (int argc, char const * argv [])
  66. {
  67. static char const * optv [] =
  68. {
  69. "l:b:",
  70. "> file",
  71. "output a file of fixed length containing constant byte values",
  72. "l n\tdata length [" LITERAL (MAIN_LENGTH) "]",
  73. "b x\tbyte value [" LITERAL (MAIN_BYTE) "]",
  74. (char const *) (0)
  75. };
  76. unsigned length = MAIN_LENGTH;
  77. unsigned char byte = MAIN_BYTE;
  78. signed c;
  79. while (~ (c = getoptv (argc, argv, optv)))
  80. {
  81. switch (c)
  82. {
  83. case 'l':
  84. length = (size_t) (uintspec (optarg, 0, UINT_MAX));
  85. break;
  86. case 'b':
  87. byte = (unsigned char) (uintspec (optarg, 0, UCHAR_MAX));
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. argc -= optind;
  94. argv += optind;
  95. #if defined (WIN32)
  96. setmode (STDOUT_FILENO, O_BINARY);
  97. #endif
  98. if (! argc)
  99. {
  100. function (length, byte);
  101. }
  102. while ((argc) && (* argv))
  103. {
  104. function (length, byte);
  105. argc--;
  106. argv++;
  107. }
  108. exit (0);
  109. }