rmcr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /****************************************************************************
  2. * tools/rmcr.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. /****************************************************************************
  28. * Pre-processor Definitions
  29. ****************************************************************************/
  30. #define LINESIZE 1024
  31. /****************************************************************************
  32. * Private Data
  33. ****************************************************************************/
  34. static char g_line[LINESIZE];
  35. /****************************************************************************
  36. * Public Data
  37. ****************************************************************************/
  38. /****************************************************************************
  39. * Private Functions
  40. ****************************************************************************/
  41. int main(int argc, char **argv)
  42. {
  43. FILE *instream;
  44. FILE *outstream;
  45. int len;
  46. int ret = 1;
  47. if (argc != 3)
  48. {
  49. fprintf(stderr, "ERROR: Two arguments expected\n");
  50. return 1;
  51. }
  52. /* Open the source file read-only */
  53. instream = fopen(argv[1], "r");
  54. if (instream == NULL)
  55. {
  56. fprintf(stderr, "ERROR: Failed to open %s for reading\n", argv[1]);
  57. return 1;
  58. }
  59. /* Open the destination file write-only */
  60. outstream = fopen(argv[2], "w");
  61. if (outstream == NULL)
  62. {
  63. fprintf(stderr, "ERROR: Failed to open %s for reading\n", argv[2]);
  64. goto errout_with_instream;
  65. }
  66. /* Process each line in the file */
  67. while ((fgets(g_line, LINESIZE, instream) != NULL))
  68. {
  69. /* Remove all whitespace (including newline) from the end of the line */
  70. len = strlen(g_line) - 1;
  71. while (len >= 0 && isspace(g_line[len]))
  72. {
  73. len--;
  74. }
  75. /* Put the newline back. len is either -1, or points to the last, non-
  76. * space character in the line.
  77. */
  78. g_line[len + 1] = '\n';
  79. g_line[len + 2] = '\0';
  80. fputs(g_line, outstream);
  81. }
  82. ret = 0;
  83. fclose(outstream);
  84. errout_with_instream:
  85. fclose(instream);
  86. return ret;
  87. }