chomp.c 585 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Perl-style 'chomp', for a line we just read with fgetline.
  3. *
  4. * Unlike Perl chomp, however, we're deliberately forgiving of strange
  5. * line-ending conventions.
  6. *
  7. * Also we forgive NULL on input, so you can just write 'line =
  8. * chomp(fgetline(fp));' and not bother checking for NULL until
  9. * afterwards.
  10. */
  11. #include <string.h>
  12. #include "defs.h"
  13. #include "misc.h"
  14. char *chomp(char *str)
  15. {
  16. if (str) {
  17. int len = strlen(str);
  18. while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n'))
  19. len--;
  20. str[len] = '\0';
  21. }
  22. return str;
  23. }