Make directory path: Difference between revisions

Content added Content deleted
No edit summary
(→‎OCaml: make the solution cross-platform)
Line 580: Line 580:
=={{header|OCaml}}==
=={{header|OCaml}}==


<syntaxhighlight lang="ocaml">#load "unix.cma"
<syntaxhighlight lang="ocaml">let rec mkdir_p path perm =
if path <> "" then

try Unix.mkdir path perm with
let mkdir_p ~path ~perms =
| Unix.Unix_error (EEXIST, _, _) when Sys.is_directory path -> ()
let ps = String.split_on_char '/' path in
| Unix.Unix_error (ENOENT, _, _) ->
let rec aux acc = function [] -> ()
mkdir_p (Filename.dirname path) perm;
| p::ps ->
Unix.mkdir path perm</syntaxhighlight>Requires <code>[https://v2.ocaml.org/manual/libunix.html unix]</code> library to be loaded
let this = String.concat Filename.dir_sep (List.rev (p::acc)) in
Unix.mkdir this perms;
aux (p::acc) ps
in
aux [] ps

let () =
mkdir_p "path/to/dir" 0o700</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==