summaryrefslogtreecommitdiff
path: root/otherlibs/unix/windir.c
blob: 10dd2fd1bf9e05664a658447c6e084080f3af939 (plain)
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
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*    Pascal Cuoq and Xavier Leroy, projet Cristal, INRIA Rocquencourt    */
/*                                                                        */
/*   Copyright 1996 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 CAML_INTERNALS

#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <errno.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/osdeps.h>
#include "unixsupport.h"

CAMLprim value caml_unix_findfirst(value name)
{
  CAMLparam0();
  CAMLlocal2(valname, valh);
  HANDLE h;
  value v;
  WIN32_FIND_DATAW fileinfo;
  wchar_t * wname;

  caml_unix_check_path(name, "opendir");
  wname = caml_stat_strdup_to_utf16(String_val(name));
  h = FindFirstFile(wname,&fileinfo);
  caml_stat_free(wname);
  if (h == INVALID_HANDLE_VALUE) {
    DWORD err = GetLastError();
    if (err == ERROR_NO_MORE_FILES)
      caml_raise_end_of_file();
    else {
      caml_win32_maperr(err);
      caml_uerror("opendir", Nothing);
    }
  }
  valname = caml_copy_string_of_utf16(fileinfo.cFileName);
  valh = caml_win32_alloc_handle(h);
  v = caml_alloc_small(2, 0);
  Field(v,0) = valname;
  Field(v,1) = valh;
  CAMLreturn(v);
}

CAMLprim value caml_unix_findnext(value valh)
{
  WIN32_FIND_DATAW fileinfo;
  BOOL retcode;

  retcode = FindNextFile(Handle_val(valh), &fileinfo);
  if (!retcode) {
    DWORD err = GetLastError();
    if (err == ERROR_NO_MORE_FILES)
      caml_raise_end_of_file();
    else {
      caml_win32_maperr(err);
      caml_uerror("readdir", Nothing);
    }
  }
  return caml_copy_string_of_utf16(fileinfo.cFileName);
}

CAMLprim value caml_unix_findclose(value valh)
{
  if (! FindClose(Handle_val(valh))) {
    caml_win32_maperr(GetLastError());
    caml_uerror("closedir", Nothing);
  }
  return Val_unit;
}