blob: e360ba138bad0062da4670a4424454d4ab0d91c1 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) many different people.
Eric Andersencb81e642003-07-14 21:21:08 +00006 * If you wrote this, please acknowledge your work.
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
Denys Vlasenko20077c12017-08-05 17:50:35 +020012char* FAST_FUNC trim(char *s)
Eric Andersenaad1a882001-03-16 22:47:14 +000013{
Rob Landley828548a2005-09-01 10:23:57 +000014 size_t len = strlen(s);
Denys Vlasenko20077c12017-08-05 17:50:35 +020015 size_t old = len;
Mark Whitley4b66dab2001-04-23 17:04:41 +000016
Eric Andersenaad1a882001-03-16 22:47:14 +000017 /* trim trailing whitespace */
Denis Vlasenko5a28a252007-10-29 19:22:13 +000018 while (len && isspace(s[len-1]))
19 --len;
Eric Andersenaad1a882001-03-16 22:47:14 +000020
21 /* trim leading whitespace */
Denis Vlasenko51742f42007-04-12 00:32:05 +000022 if (len) {
Denys Vlasenkoc0dab372009-10-22 22:28:08 +020023 char *nws = skip_whitespace(s);
24 if ((nws - s) != 0) {
25 len -= (nws - s);
26 memmove(s, nws, len);
Denis Vlasenko5a28a252007-10-29 19:22:13 +000027 }
Rob Landley828548a2005-09-01 10:23:57 +000028 }
Denys Vlasenko20077c12017-08-05 17:50:35 +020029
30 s += len;
31 /* If it was a "const char*" which does not need trimming,
32 * avoid superfluous store */
33 if (old != len)
34 *s = '\0';
35
36 return s;
Eric Andersenaad1a882001-03-16 22:47:14 +000037}