blob: 16cb4fbb07f256794e1047041d710d3b923dd816 [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 */
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);
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 }
Denis Vlasenko51742f42007-04-12 00:32:05 +000029 s[len] = '\0';
Eric Andersenaad1a882001-03-16 22:47:14 +000030}