trim.c 545 B

12345678910111213141516171819202122232425262728293031
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please acknowledge your work.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include "libbb.h"
  11. void trim(char *s)
  12. {
  13. size_t len = strlen(s);
  14. size_t lws;
  15. /* trim trailing whitespace */
  16. while (len && isspace(s[len-1]))
  17. --len;
  18. /* trim leading whitespace */
  19. if (len) {
  20. lws = strspn(s, " \n\r\t\v");
  21. if (lws) {
  22. len -= lws;
  23. memmove(s, s + lws, len);
  24. }
  25. }
  26. s[len] = '\0';
  27. }