trim.c 549 B

123456789101112131415161718192021222324252627282930
  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 source tree.
  9. */
  10. #include "libbb.h"
  11. void FAST_FUNC trim(char *s)
  12. {
  13. size_t len = strlen(s);
  14. /* trim trailing whitespace */
  15. while (len && isspace(s[len-1]))
  16. --len;
  17. /* trim leading whitespace */
  18. if (len) {
  19. char *nws = skip_whitespace(s);
  20. if ((nws - s) != 0) {
  21. len -= (nws - s);
  22. memmove(s, nws, len);
  23. }
  24. }
  25. s[len] = '\0';
  26. }