blob: ea20ff370396155610fb24bdafa1d0196ba386f0 [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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000013void FAST_FUNC trim(char *s)
Eric Andersenaad1a882001-03-16 22:47:14 +000014{
Rob Landley828548a2005-09-01 10:23:57 +000015 size_t len = strlen(s);
16 size_t lws;
Mark Whitley4b66dab2001-04-23 17:04:41 +000017
Eric Andersenaad1a882001-03-16 22:47:14 +000018 /* trim trailing whitespace */
Denis Vlasenko5a28a252007-10-29 19:22:13 +000019 while (len && isspace(s[len-1]))
20 --len;
Eric Andersenaad1a882001-03-16 22:47:14 +000021
22 /* trim leading whitespace */
Denis Vlasenko51742f42007-04-12 00:32:05 +000023 if (len) {
Rob Landley828548a2005-09-01 10:23:57 +000024 lws = strspn(s, " \n\r\t\v");
Denis Vlasenko5a28a252007-10-29 19:22:13 +000025 if (lws) {
26 len -= lws;
27 memmove(s, s + lws, len);
28 }
Rob Landley828548a2005-09-01 10:23:57 +000029 }
Denis Vlasenko51742f42007-04-12 00:32:05 +000030 s[len] = '\0';
Eric Andersenaad1a882001-03-16 22:47:14 +000031}