-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrealpath.c
89 lines (74 loc) · 2.54 KB
/
realpath.c
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* The OCaml programmers */
/* */
/* Copyright 2020 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define EXTUNIX_WANT_REALPATH
#include "config.h"
#if defined(EXTUNIX_HAVE_REALPATH)
#if !defined(_WIN32)
CAMLprim value caml_extunix_realpath (value p)
{
CAMLparam1 (p);
char *r;
value rp;
// caml_unix_check_path (p, "realpath");
r = realpath (String_val (p), NULL);
if (r == NULL) { uerror ("realpath", p); }
rp = caml_copy_string (r);
free (r);
CAMLreturn (rp);
}
#else
#include <caml/osdeps.h>
CAMLprim value caml_extunix_realpath (value p)
{
CAMLparam1 (p);
HANDLE h;
wchar_t *wp;
wchar_t *wr;
DWORD wr_len;
value rp;
// caml_unix_check_path (p, "realpath");
wp = caml_stat_strdup_to_utf16 (String_val (p));
h = CreateFile (wp, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
caml_stat_free (wp);
if (h == INVALID_HANDLE_VALUE)
{
win32_maperr (GetLastError ());
uerror ("realpath", p);
}
wr_len = GetFinalPathNameByHandle (h, NULL, 0, VOLUME_NAME_DOS);
if (wr_len == 0)
{
win32_maperr (GetLastError ());
CloseHandle (h);
uerror ("realpath", p);
}
wr = caml_stat_alloc ((wr_len + 1) * sizeof (wchar_t));
wr_len = GetFinalPathNameByHandle (h, wr, wr_len, VOLUME_NAME_DOS);
if (wr_len == 0)
{
win32_maperr (GetLastError ());
CloseHandle (h);
caml_stat_free (wr);
uerror ("realpath", p);
}
rp = caml_copy_string_of_utf16 (wr);
CloseHandle (h);
caml_stat_free (wr);
CAMLreturn (rp);
}
#endif
#endif