mkdir-p.pl 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #! /usr/bin/env perl
  2. # Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # On some systems, the -p option to mkdir (= also create any missing parent
  9. # directories) is not available.
  10. my $arg;
  11. foreach $arg (@ARGV) {
  12. $arg =~ tr|\\|/|;
  13. &do_mkdir_p($arg);
  14. }
  15. sub do_mkdir_p {
  16. local($dir) = @_;
  17. $dir =~ s|/*\Z(?!\n)||s;
  18. if (-d $dir) {
  19. return;
  20. }
  21. if ($dir =~ m|[^/]/|s) {
  22. local($parent) = $dir;
  23. $parent =~ s|[^/]*\Z(?!\n)||s;
  24. do_mkdir_p($parent);
  25. }
  26. unless (mkdir($dir, 0777)) {
  27. local($err) = $!;
  28. if (-d $dir) {
  29. # We raced against another instance doing the same thing.
  30. return;
  31. }
  32. die "Cannot create directory $dir: $err\n";
  33. }
  34. print "created directory `$dir'\n";
  35. }