summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/extract-copyright.pl
blob: 2172216ae71a80d1dcc41d55ccc02efc7879377d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/perl5/bin/perl
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy is of the CDDL is also available via the Internet
# at http://www.illumos.org/license/CDDL.
#

#
# Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
#

#
# This extracts all the BSD copyrights (excluding the CDDL licenses)
# for use in a THIRDPARTYLICENSE file.  It tries hard to avoid duplicates.
#

use strict;
use warnings;
use File::Find;

my %LICENSE = ();

sub dofile
{
	my $file = shift;
	my $comment = 0;
	my @license = ();
	my @block = ();;
	my $copyr = 0;
	open(FILE, $file);
	while (<FILE>) {
		if (/^\/\*$/) {
			$comment = 1;
			$copyr = 0;
			@block = ();
			next;
		}
		if (!$comment) {
			next;
		}
		#
		# We don't want to know about CDDL files.  They don't
		# require an explicit THIRDPARTYLICENSE file.
		#
		if (/CDDL/) {
			#print "$file is CDDL.\n";
			close(FILE);
			return;
		}
		if (/Copyright/) {
			$copyr = 1;
		}
		if (!/^ \*\//) {
			push(@block, $_);
			next;
		}
		#
		# We have reached the end of the comment now.
		#
		$comment = 0;

		# Check to see if we saw a copyright.
		if (!$copyr) {
			next;
		}
		my $line;
		foreach $line (@block) {
			chomp $line;
			$line =~ s/^ \* //;
			$line =~ s/^ \*//;
			$line =~ s/^ \*$//;
			push(@license, $line);
		}
	}

	if ($#license > 0)  {
		my $lic = join "\n", @license;
		push (@{$LICENSE{$lic}}, $file);
	}

	close(FILE);
}

my @FILES;

sub wanted {
	my $path = $File::Find::name;

	if (!-f $path) {
		if ($path =~ /\.[chs]$/) {
			push(@FILES, $path);
		}
	}
	
}
foreach $a (@ARGV) {
    	if (-d $a) {
		find(\&wanted, $a);
	} elsif (-f $a) {
		push(@FILES, $a);
	}
}

foreach $a (@FILES) {
	dofile($a);
}

foreach my $lic (keys %LICENSE) {
	my @files = @{$LICENSE{$lic}};
	print "\nThe following files from the C library:\n";
	foreach my $f (@files) {
		print("    $f\n");
	}
	print "are provided under the following terms:\n\n";
	print "$lic\n";
}