Steve Frécinaux

Inserting licences into a bunch of files

Here is a small Perl script to insert a license excerpt into C files if there isn’t one already. Note that it only checks the first paragraph to see whether there is already one or not.

#!/usr/bin/perl -wi.bak -00
use strict;

my $author = 'author <email>';

my $gplexcerpt = <<EOGPL
/* Copyright (C) 2006 $author
 *
 * Licensed under the GNU General Public License Version 2
 * [...]
 */

EOGPL
;

while (<>) {
	$.-- if /-\*-.*-\*-/ # skip modeline
        print $gplexcerpt if $. == 1 and ! /License/;
        print;
} continue {
       close ARGV  if eof;
}

Use it this way:

find src/ -name "*.c" -o -name "*.h" \
 | xargs ../addlicense.pl

Thanks to Liam Quin and Krunch for their help on decrypting Perl line noise.