xref: /titanic_50/usr/src/lib/libc/extract-copyright.pl (revision 6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648)
1*6b5e5868SGarrett D'Amore#! /usr/perl5/bin/perl
2*6b5e5868SGarrett D'Amore#
3*6b5e5868SGarrett D'Amore# This file and its contents are supplied under the terms of the
4*6b5e5868SGarrett D'Amore# Common Development and Distribution License ("CDDL"), version 1.0.
5*6b5e5868SGarrett D'Amore# You may only use this file in accordance with the terms version
6*6b5e5868SGarrett D'Amore# 1.0 of the CDDL.
7*6b5e5868SGarrett D'Amore#
8*6b5e5868SGarrett D'Amore# A full copy of the text of the CDDL should have accompanied this
9*6b5e5868SGarrett D'Amore# source.  A copy is of the CDDL is also available via the Internet
10*6b5e5868SGarrett D'Amore# at http://www.illumos.org/license/CDDL.
11*6b5e5868SGarrett D'Amore#
12*6b5e5868SGarrett D'Amore
13*6b5e5868SGarrett D'Amore#
14*6b5e5868SGarrett D'Amore# Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
15*6b5e5868SGarrett D'Amore#
16*6b5e5868SGarrett D'Amore
17*6b5e5868SGarrett D'Amore#
18*6b5e5868SGarrett D'Amore# This extracts all the BSD copyrights (excluding the CDDL licenses)
19*6b5e5868SGarrett D'Amore# for use in a THIRDPARTYLICENSE file.  It tries hard to avoid duplicates.
20*6b5e5868SGarrett D'Amore#
21*6b5e5868SGarrett D'Amore
22*6b5e5868SGarrett D'Amoreuse strict;
23*6b5e5868SGarrett D'Amoreuse warnings;
24*6b5e5868SGarrett D'Amoreuse File::Find;
25*6b5e5868SGarrett D'Amore
26*6b5e5868SGarrett D'Amoremy %LICENSE = ();
27*6b5e5868SGarrett D'Amore
28*6b5e5868SGarrett D'Amoresub dofile
29*6b5e5868SGarrett D'Amore{
30*6b5e5868SGarrett D'Amore	my $file = shift;
31*6b5e5868SGarrett D'Amore	my $comment = 0;
32*6b5e5868SGarrett D'Amore	my @license = ();
33*6b5e5868SGarrett D'Amore	my @block = ();;
34*6b5e5868SGarrett D'Amore	my $copyr = 0;
35*6b5e5868SGarrett D'Amore	open(FILE, $file);
36*6b5e5868SGarrett D'Amore	while (<FILE>) {
37*6b5e5868SGarrett D'Amore		if (/^\/\*$/) {
38*6b5e5868SGarrett D'Amore			$comment = 1;
39*6b5e5868SGarrett D'Amore			$copyr = 0;
40*6b5e5868SGarrett D'Amore			@block = ();
41*6b5e5868SGarrett D'Amore			next;
42*6b5e5868SGarrett D'Amore		}
43*6b5e5868SGarrett D'Amore		if (!$comment) {
44*6b5e5868SGarrett D'Amore			next;
45*6b5e5868SGarrett D'Amore		}
46*6b5e5868SGarrett D'Amore		#
47*6b5e5868SGarrett D'Amore		# We don't want to know about CDDL files.  They don't
48*6b5e5868SGarrett D'Amore		# require an explicit THIRDPARTYLICENSE file.
49*6b5e5868SGarrett D'Amore		#
50*6b5e5868SGarrett D'Amore		if (/CDDL/) {
51*6b5e5868SGarrett D'Amore			#print "$file is CDDL.\n";
52*6b5e5868SGarrett D'Amore			close(FILE);
53*6b5e5868SGarrett D'Amore			return;
54*6b5e5868SGarrett D'Amore		}
55*6b5e5868SGarrett D'Amore		if (/Copyright/) {
56*6b5e5868SGarrett D'Amore			$copyr = 1;
57*6b5e5868SGarrett D'Amore		}
58*6b5e5868SGarrett D'Amore		if (!/^ \*\//) {
59*6b5e5868SGarrett D'Amore			push(@block, $_);
60*6b5e5868SGarrett D'Amore			next;
61*6b5e5868SGarrett D'Amore		}
62*6b5e5868SGarrett D'Amore		#
63*6b5e5868SGarrett D'Amore		# We have reached the end of the comment now.
64*6b5e5868SGarrett D'Amore		#
65*6b5e5868SGarrett D'Amore		$comment = 0;
66*6b5e5868SGarrett D'Amore
67*6b5e5868SGarrett D'Amore		# Check to see if we saw a copyright.
68*6b5e5868SGarrett D'Amore		if (!$copyr) {
69*6b5e5868SGarrett D'Amore			next;
70*6b5e5868SGarrett D'Amore		}
71*6b5e5868SGarrett D'Amore		my $line;
72*6b5e5868SGarrett D'Amore		foreach $line (@block) {
73*6b5e5868SGarrett D'Amore			chomp $line;
74*6b5e5868SGarrett D'Amore			$line =~ s/^ \* //;
75*6b5e5868SGarrett D'Amore			$line =~ s/^ \*//;
76*6b5e5868SGarrett D'Amore			$line =~ s/^ \*$//;
77*6b5e5868SGarrett D'Amore			push(@license, $line);
78*6b5e5868SGarrett D'Amore		}
79*6b5e5868SGarrett D'Amore	}
80*6b5e5868SGarrett D'Amore
81*6b5e5868SGarrett D'Amore	if ($#license > 0)  {
82*6b5e5868SGarrett D'Amore		my $lic = join "\n", @license;
83*6b5e5868SGarrett D'Amore		push (@{$LICENSE{$lic}}, $file);
84*6b5e5868SGarrett D'Amore	}
85*6b5e5868SGarrett D'Amore
86*6b5e5868SGarrett D'Amore	close(FILE);
87*6b5e5868SGarrett D'Amore}
88*6b5e5868SGarrett D'Amore
89*6b5e5868SGarrett D'Amoremy @FILES;
90*6b5e5868SGarrett D'Amore
91*6b5e5868SGarrett D'Amoresub wanted {
92*6b5e5868SGarrett D'Amore	my $path = $File::Find::name;
93*6b5e5868SGarrett D'Amore
94*6b5e5868SGarrett D'Amore	if (!-f $path) {
95*6b5e5868SGarrett D'Amore		if ($path =~ /\.[chs]$/) {
96*6b5e5868SGarrett D'Amore			push(@FILES, $path);
97*6b5e5868SGarrett D'Amore		}
98*6b5e5868SGarrett D'Amore	}
99*6b5e5868SGarrett D'Amore
100*6b5e5868SGarrett D'Amore}
101*6b5e5868SGarrett D'Amoreforeach $a (@ARGV) {
102*6b5e5868SGarrett D'Amore    	if (-d $a) {
103*6b5e5868SGarrett D'Amore		find(\&wanted, $a);
104*6b5e5868SGarrett D'Amore	} elsif (-f $a) {
105*6b5e5868SGarrett D'Amore		push(@FILES, $a);
106*6b5e5868SGarrett D'Amore	}
107*6b5e5868SGarrett D'Amore}
108*6b5e5868SGarrett D'Amore
109*6b5e5868SGarrett D'Amoreforeach $a (@FILES) {
110*6b5e5868SGarrett D'Amore	dofile($a);
111*6b5e5868SGarrett D'Amore}
112*6b5e5868SGarrett D'Amore
113*6b5e5868SGarrett D'Amoreforeach my $lic (keys %LICENSE) {
114*6b5e5868SGarrett D'Amore	my @files = @{$LICENSE{$lic}};
115*6b5e5868SGarrett D'Amore	print "\nThe following files from the C library:\n";
116*6b5e5868SGarrett D'Amore	foreach my $f (@files) {
117*6b5e5868SGarrett D'Amore		print("    $f\n");
118*6b5e5868SGarrett D'Amore	}
119*6b5e5868SGarrett D'Amore	print "are provided under the following terms:\n\n";
120*6b5e5868SGarrett D'Amore	print "$lic\n";
121*6b5e5868SGarrett D'Amore}
122