public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Michal Marek <mmarek@suse.com>
To: Paul Bolle <pebolle@tiscali.nl>
Cc: Josh Boyer <jwboyer@fedoraproject.org>,
	linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] kconfig: add chomp like helper function
Date: Wed, 16 Mar 2016 12:42:25 +0100	[thread overview]
Message-ID: <56E946A1.2060400@suse.com> (raw)
In-Reply-To: <1458125619-28093-1-git-send-email-pebolle@tiscali.nl>

On 2016-03-16 11:53, Paul Bolle wrote:
> Add a helper function that strips trailing new lines and carriage
> returns from strings. Call it chomp, after the perl function that
> inspired it.
> 
> Signed-off-by: Paul Bolle <pebolle@tiscali.nl>
> ---
>  scripts/kconfig/confdata.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
> index 0b7dc2fd7bac..51904c423411 100644
> --- a/scripts/kconfig/confdata.c
> +++ b/scripts/kconfig/confdata.c
> @@ -248,6 +248,28 @@ e_out:
>  	return -1;
>  }
>  
> +/*
> + * Return newly allocated copy of string "in" with all trailing new lines and
> + * carriage returns removed.
> + */
> +static char *chomp(char *in)
> +{
> +	size_t last = strlen(in);
> +	char *copy;
> +
> +	copy = malloc(last + 1);
> +	if (!copy)
> +		return NULL;
> +
> +	strcpy(copy, in);
> +	if (last)
> +		last--;
> +	while (last  && (copy[last] == '\r' || copy[last] == '\n'))
> +		copy[last--] = '\0';
> +
> +	return copy;
> +}
> +

For this particular use, it's probably easier to just write

conf_warning("unexpected data: %.*s",
	     (int)strcspn(line, "\r\n"), line);

Or do you see more use cases for the chomp function?

No matter how the string is constructed, I like the verbose warning :)

Thanks,
Michal

  parent reply	other threads:[~2016-03-16 11:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-16 10:53 [PATCH 1/2] kconfig: add chomp like helper function Paul Bolle
2016-03-16 10:53 ` [PATCH 2/2] kconfig: add unexpected data itself to warning Paul Bolle
2016-03-16 11:42 ` Michal Marek [this message]
2016-03-16 12:41   ` [PATCH 1/2] kconfig: add chomp like helper function Paul Bolle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56E946A1.2060400@suse.com \
    --to=mmarek@suse.com \
    --cc=jwboyer@fedoraproject.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pebolle@tiscali.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox