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