fork download
  1. #define WINDOWS_OS !defined(__linux__) && !defined(__unix__) && !defined(__APPLE__)
  2.  
  3. #if WINDOWS_OS
  4. # include <fcntl.h>
  5. # include <io.h>
  6. #else
  7. # include <sys/param.h>
  8. #endif
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #if WINDOWS_OS
  14. # if defined(__BORLANDC__)
  15. # define _setmode(f,t) setmode((f), (t))
  16. # define _O_BINARY O_BINARY
  17. # endif
  18. # define DS_CODE ('\\')
  19. #else
  20. # define _setmode(f,t) (0)
  21. # define _O_BINARY
  22. # define DS_CODE ('/')
  23. # define _MAX_PATH MAXPATHLEN
  24. # define _fullpath(a,r,n) realpath((r), (a))
  25. #endif
  26.  
  27. #define ArrLen(arr) (sizeof(arr) / sizeof((arr)[0]))
  28.  
  29. #define SIZE (1000)
  30. #define LF_CODE (0x0A)
  31. #define CR_CODE (0x0D)
  32. #define OUTPUT_SW ("-o")
  33.  
  34. #define ErrCheck(e) \
  35. { \
  36. if (e) { \
  37. perror(NULL); \
  38. exit(EXIT_FAILURE); \
  39. } \
  40. }
  41.  
  42. #define ErrDown(m) \
  43. { \
  44. usage(args[0]); \
  45. fprintf(stderr, "Error: "); \
  46. fprintf(stderr, m); \
  47. exit(EXIT_FAILURE); \
  48. }
  49.  
  50. void usage(const char arg0[]);
  51.  
  52. /* CRコードを削除する。 */
  53.  
  54. int main(int argc, char *args[]) {
  55.  
  56. int ch;
  57. char data[SIZE + 1], out[SIZE + SIZE + 1];
  58. int i, sz, md, j, flag;
  59. FILE *input = NULL, *output = NULL;
  60. char path1[_MAX_PATH + 1], path2[_MAX_PATH + 1];
  61.  
  62. if (argc > 4) {
  63. ErrDown("a lot of argument!\n");
  64. }
  65.  
  66. if (argc > 1) {
  67. switch (argc) {
  68. case 2: {
  69. if ((strcmp(args[1], "--help") == 0)
  70. || (strcmp(args[1], "-h") == 0)
  71. || (strcmp(args[1], "-?") == 0)
  72. || (strcmp(args[1], "/?") == 0)) {
  73. usage(args[0]);
  74. return 0;
  75. }
  76. input = fopen(args[1], "rb");
  77. ErrCheck(input == NULL);
  78. break;
  79. }
  80. case 3: {
  81. if (strcmp(args[1], OUTPUT_SW) != 0) {
  82. ErrDown("syntax error!\n");
  83. }
  84. output = fopen(args[2], "wb");
  85. ErrCheck(output == NULL);
  86. break;
  87. }
  88. case 4: {
  89. i = 1;
  90. j = 3;
  91. if (strcmp(args[1], OUTPUT_SW) == 0) {
  92. i = 3;
  93. j = 2;
  94. } else if (strcmp(args[2], OUTPUT_SW) != 0) {
  95. ErrDown("syntax error!\n");
  96. }
  97. if (_fullpath(path1, args[i], ArrLen(path1)) == NULL) {
  98. ErrDown("illegal input file!\n");
  99. }
  100. if (_fullpath(path2, args[j], ArrLen(path2)) == NULL) {
  101. ErrDown("illegal output file!\n");
  102. }
  103. if (strcmp(path1, path2) == 0) {
  104. ErrDown("the same input file as ouput file!\n");
  105. }
  106. input = fopen(path1, "rb");
  107. ErrCheck(input == NULL);
  108. output = fopen(path2, "wb");
  109. ErrCheck(output == NULL);
  110. break;
  111. }
  112. default: {
  113. break;
  114. }
  115. }
  116. }
  117.  
  118. if (input == NULL) {
  119. md = _setmode(_fileno(stdin), _O_BINARY);
  120. ErrCheck(md == -1);
  121. input = stdin;
  122. }
  123.  
  124. if (output == NULL) {
  125. md = _setmode(_fileno(stdout), _O_BINARY);
  126. ErrCheck(md == -1);
  127. output = stdout;
  128. }
  129.  
  130. flag = 0;
  131. while ((sz = fread(data, sizeof(data[0]), ArrLen(data), input)) != 0) {
  132. j = 0;
  133. for (i = 0; i < sz; ++i) {
  134. if (flag == 1) {
  135. flag = 0;
  136. if (data[i] != LF_CODE) {
  137. out[j] = LF_CODE;
  138. ++j;
  139. }
  140. }
  141. if (data[i] == CR_CODE) {
  142. flag = 1;
  143. continue;
  144. }
  145. out[j] = data[i];
  146. ++j;
  147. }
  148. fwrite(out, sizeof(out[0]), j, output);
  149. }
  150.  
  151. if (input != stdin) {
  152. fclose(input);
  153. }
  154.  
  155. if (output != stdout) {
  156. fclose(output);
  157. }
  158.  
  159. return 0;
  160. }
  161.  
  162. void usage(const char arg0[]) {
  163. int i, j;
  164. j = 0;
  165. for (i = 0; arg0[i] != '\0'; ++i) {
  166. if (arg0[i] == DS_CODE) {
  167. j = i + 1;
  168. }
  169. }
  170. puts("Delete CR-Code(0x0D)");
  171. printf(" usage: %s [-o <Output File>] [<Input File>]\n", &arg0[j]);
  172. }
  173.  
Success #stdin #stdout 0s 2296KB
stdin
よし
たぶん
CRコードの削除出来てるはず!
stdout
よし
たぶん
CRコードの削除出来てるはず!