public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Jeff Garzik <jeff@garzik.org>, linux-kernel@vger.kernel.org
Subject: Re: [SCRIPT] chomp: trim trailing whitespace
Date: Fri, 26 May 2006 21:17:29 -0700	[thread overview]
Message-ID: <4477D2D9.5050001@zytor.com> (raw)
In-Reply-To: <4477B905.9090806@garzik.org>

[-- Attachment #1: Type: text/plain, Size: 585 bytes --]

Jeff Garzik wrote:
> 
> Attached to this email is chomp.pl, a Perl script which removes trailing 
> whitespace from several files.  I've had this for years, as trailing 
> whitespace is one of my pet peeves.
> 
> Now that git-applymbox complains loudly whenever a patch adds trailing 
> whitespace, I figured this script may be useful to others.
> 

This is the script I use for the same purpose.  It's a bit more 
sophisticated, in that it detects and avoids binary files, and doesn't 
throw an error if it encounters a directory (which can happen if you 
give it a wildcard.)

	-hpa

[-- Attachment #2: cleanfile --]
[-- Type: text/plain, Size: 1126 bytes --]

#!/usr/bin/perl
#
# Clean a text file of stealth whitespace
#

use bytes;

$name = 'cleanfile';

foreach $f ( @ARGV ) {
    print STDERR "$name: $f\n";

    if (! -f $f) {
	print STDERR "$f: not a file\n";
	next;
    }
    
    if (!open(FILE, '+<', $f)) {
	print STDERR "$name: Cannot open file: $f: $!\n";
	next;
    }

    binmode FILE;

    # First, verify that it is not a binary file
    $is_binary = 0;

    while (read(FILE, $data, 65536) > 0) {
	if ($data =~ /\0/) {
	    $is_binary = 1;
	    last;
	}
    }

    if ($is_binary) {
	print STDERR "$name: $f: binary file\n";
	next;
    }

    seek(FILE, 0, 0);

    @blanks = ();
    @lines  = ();

    while ( defined($line = <FILE>) ) {
	$line =~ s/[ \t\r\n]*$/\n/;

	if ( $line eq "\n" ) {
	    push(@blanks, $line);
	} else {
	    push(@lines, @blanks);
	    push(@lines, $line);
	    @blanks = ();
	}
    }

    # Any blanks at the end of the file are discarded

    seek(FILE, 0, 0);
    print FILE @lines;

    if ( !defined($where = tell(FILE)) ||
	 !truncate(FILE, $where) ) {
	die "$name: Failed to truncate modified file: $f: $!\n";
    }
    close(FILE);
}

  reply	other threads:[~2006-05-27  4:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-27  2:27 [SCRIPT] chomp: trim trailing whitespace Jeff Garzik
2006-05-27  4:17 ` H. Peter Anvin [this message]
2006-05-27 11:42   ` Jeff Garzik
2006-05-28  9:24     ` H. Peter Anvin
2006-05-27 10:15 ` Jan Engelhardt
2006-05-27 10:24   ` Thomas Glanzmann
2006-05-27 10:36     ` Neil Brown
2006-05-27 11:32   ` Jeff Garzik
2006-05-27 11:48     ` Dmitry Fedorov
2006-05-27 12:42     ` Jan Engelhardt
2006-05-28  8:33       ` Keith Owens
2006-05-27 15:28 ` Martin Langhoff
2006-05-27 16:13   ` Linus Torvalds
2006-05-28 10:00     ` Johannes Schindelin

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=4477D2D9.5050001@zytor.com \
    --to=hpa@zytor.com \
    --cc=jeff@garzik.org \
    --cc=linux-kernel@vger.kernel.org \
    /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