pad.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*====================================================================*
  2. *
  3. * pad.c - file pad program;
  4. *
  5. * copy one or more files to stdout; if no files are specified
  6. * then copy stdin to stdout;
  7. *
  8. *. Motley Tools by Charles Maier
  9. *: Copyright (c) 2001-2006 by Charles Maier Associates Limited;
  10. *; Licensed under the Internet Software Consortium License;
  11. *
  12. *--------------------------------------------------------------------*/
  13. #define _GETOPT_H
  14. /*====================================================================*
  15. * system header files;
  16. *--------------------------------------------------------------------*/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <limits.h>
  22. /*====================================================================*
  23. * custom header files;
  24. *--------------------------------------------------------------------*/
  25. #include "../tools/getoptv.h"
  26. #include "../tools/putoptv.h"
  27. #include "../tools/number.h"
  28. #include "../tools/types.h"
  29. #include "../tools/files.h"
  30. /*====================================================================*
  31. * custom source files;
  32. *--------------------------------------------------------------------*/
  33. #ifndef MAKEFILE
  34. #include "../tools/getoptv.c"
  35. #include "../tools/putoptv.c"
  36. #include "../tools/version.c"
  37. #include "../tools/efreopen.c"
  38. #include "../tools/uintspec.c"
  39. #include "../tools/todigit.c"
  40. #include "../tools/error.c"
  41. #endif
  42. /*====================================================================*
  43. * program constants;
  44. *--------------------------------------------------------------------*/
  45. #define BLOCKSIZE 4
  46. /*====================================================================*
  47. *
  48. * signed function (signed blocksize, byte fill);
  49. *
  50. * copy file using fixed blocksize so that the output is always a
  51. * multiple of the blocksize;
  52. *
  53. *--------------------------------------------------------------------*/
  54. static signed function (signed blocksize, byte fill)
  55. {
  56. void * memory = malloc (blocksize);
  57. if (memory)
  58. {
  59. memset (memory, fill, blocksize);
  60. while (read (STDIN_FILENO, memory, blocksize) > 0)
  61. {
  62. write (STDOUT_FILENO, memory, blocksize);
  63. memset (memory, fill, blocksize);
  64. }
  65. free (memory);
  66. }
  67. return (0);
  68. }
  69. /*====================================================================*
  70. *
  71. * int main (int argc, char const * argv []);
  72. *
  73. *
  74. *--------------------------------------------------------------------*/
  75. int main (int argc, char const * argv [])
  76. {
  77. static char const * optv [] =
  78. {
  79. "b:",
  80. PUTOPTV_S_FUNNEL,
  81. "copy one or more files to stdout in fixed blocks",
  82. "b n\tblock size is (n) bytes [" LITERAL (BLOCKSIZE) "]",
  83. (char const *) (0)
  84. };
  85. unsigned char fill = 0;
  86. signed blocksize = BLOCKSIZE;
  87. signed c;
  88. while (~ (c = getoptv (argc, argv, optv)))
  89. {
  90. switch (c)
  91. {
  92. case 'b':
  93. blocksize = (signed) (uintspec (optarg, 1, SHRT_MAX));
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. argc -= optind;
  100. argv += optind;
  101. #if defined (WIN32)
  102. setmode (STDOUT_FILENO, O_BINARY);
  103. #endif
  104. if (! argc)
  105. {
  106. function (blocksize, fill);
  107. }
  108. while ((argc) && (* argv))
  109. {
  110. if (efreopen (* argv, "rb", stdin))
  111. {
  112. function (blocksize, fill);
  113. }
  114. argc--;
  115. argv++;
  116. }
  117. exit (0);
  118. }