Make c_rehash more platform neutral and make it work in mixed environment,
[openssl.git] / tools / c_rehash.in
index 4497cbd9f178276df4131a710555c7ab1f0aff3f..ca3021ae8a4ff2ccf1a2f121a60f8ba6e20c9aef 100644 (file)
@@ -15,11 +15,14 @@ if(defined $ENV{OPENSSL}) {
        $ENV{OPENSSL} = $openssl;
 }
 
        $ENV{OPENSSL} = $openssl;
 }
 
-$ENV{PATH} .= ":$dir/bin";
+my $pwd = `pwd`; chomp $pwd;
+my $path_delim = ($pwd =~ /^[a-z]\:/i) ? ';' : ':'; # DOS/Win32 or Unix delimiter?
+
+$ENV{PATH} .= "$path_delim$dir/bin";
 
 if(! -x $openssl) {
        my $found = 0;
 
 if(! -x $openssl) {
        my $found = 0;
-       foreach (split /:/, $ENV{PATH}) {
+       foreach (split /$path_delim/, $ENV{PATH}) {
                if(-x "$_/$openssl") {
                        $found = 1;
                        last;
                if(-x "$_/$openssl") {
                        $found = 1;
                        last;
@@ -34,11 +37,16 @@ if(! -x $openssl) {
 if(@ARGV) {
        @dirlist = @ARGV;
 } elsif($ENV{SSL_CERT_DIR}) {
 if(@ARGV) {
        @dirlist = @ARGV;
 } elsif($ENV{SSL_CERT_DIR}) {
-       @dirlist = split /:/, $ENV{SSL_CERT_DIR};
+       @dirlist = split /$path_delim/, $ENV{SSL_CERT_DIR};
 } else {
        $dirlist[0] = "$dir/certs";
 }
 
 } else {
        $dirlist[0] = "$dir/certs";
 }
 
+if (-d $dirlist[0]) {
+       chdir $dirlist[0];
+       $openssl="$pwd/$openssl" if (!-x $openssl);
+       chdir $pwd;
+}
 
 foreach (@dirlist) {
        if(-d $_ and -w $_) {
 
 foreach (@dirlist) {
        if(-d $_ and -w $_) {
@@ -101,7 +109,7 @@ sub check_file {
 sub link_hash_cert {
                my $fname = $_[0];
                $fname =~ s/'/'\\''/g;
 sub link_hash_cert {
                my $fname = $_[0];
                $fname =~ s/'/'\\''/g;
-               my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in '$fname'`;
+               my ($hash, $fprint) = `"$openssl" x509 -hash -fingerprint -noout -in "$fname"`;
                chomp $hash;
                chomp $fprint;
                $fprint =~ s/^.*=//;
                chomp $hash;
                chomp $fprint;
                $fprint =~ s/^.*=//;
@@ -122,7 +130,11 @@ sub link_hash_cert {
                if ($symlink_exists) {
                        symlink $fname, $hash;
                } else {
                if ($symlink_exists) {
                        symlink $fname, $hash;
                } else {
-                       system ("cp", $fname, $hash);
+                       open IN,"<$fname" or die "can't open $fname for read";
+                       open OUT,">$hash" or die "can't open $hash for write";
+                       print OUT <IN>; # does the job for small text files
+                       close OUT;
+                       close IN;
                }
                $hashlist{$hash} = $fprint;
 }
                }
                $hashlist{$hash} = $fprint;
 }