trailing_whitespace.c 790 B

123456789101112131415161718192021222324252627282930313233
  1. #define STB_DEFINE
  2. #include "stb.h"
  3. int main(int argc, char **argv)
  4. {
  5. int i;
  6. for (i=1; i < argc; ++i) {
  7. int len;
  8. FILE *f;
  9. char *s = stb_file(argv[i], &len);
  10. char *end, *src, *dest;
  11. if (s == NULL) {
  12. printf("Couldn't read file '%s'.\n", argv[i]);
  13. continue;
  14. }
  15. end = s + len;
  16. src = dest = s;
  17. while (src < end) {
  18. char *start=0;
  19. while (src < end && *src != '\n' && *src != '\r')
  20. *dest++ = *src++;
  21. while (dest-1 > s && (dest[-1] == ' ' || dest[-1] == '\t'))
  22. --dest;
  23. while (src < end && (*src == '\n' || *src == '\r'))
  24. *dest++ = *src++;
  25. }
  26. f = fopen(argv[i], "wb");
  27. fwrite(s, 1, dest-s, f);
  28. fclose(f);
  29. }
  30. return 0;
  31. }