]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/otherlibs/win32unix/windir.c
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / otherlibs / win32unix / windir.c
1 /***********************************************************************/
2 /*                                                                     */
3 /*                           Objective Caml                            */
4 /*                                                                     */
5 /*   Pascal Cuoq and Xavier Leroy, projet Cristal, INRIA Rocquencourt  */
6 /*                                                                     */
7 /*  Copyright 1996 Institut National de Recherche en Informatique et   */
8 /*  en Automatique.  All rights reserved.  This file is distributed    */
9 /*  under the terms of the GNU Library General Public License, with    */
10 /*  the special exception on linking described in file ../../LICENSE.  */
11 /*                                                                     */
12 /***********************************************************************/
13
14 /* $Id: windir.c 5029 2002-07-23 14:12:03Z doligez $ */
15
16 #include <mlvalues.h>
17 #include <memory.h>
18 #include <errno.h>
19 #include <alloc.h>
20 #include <fail.h>
21 #include "unixsupport.h"
22
23 CAMLprim value win_findfirst(name)
24      value name;
25 {
26   HANDLE h;
27   value v;
28   WIN32_FIND_DATA fileinfo;
29   value valname = Val_unit;
30   value valh = Val_unit;
31
32   Begin_roots2 (valname,valh);
33     h = FindFirstFile(String_val(name),&fileinfo);
34     if (h == INVALID_HANDLE_VALUE) {
35       DWORD err = GetLastError();
36       if (err == ERROR_NO_MORE_FILES)
37         raise_end_of_file();
38       else {
39         win32_maperr(err);
40         uerror("opendir", Nothing);
41       }
42     }
43     valname = copy_string(fileinfo.cFileName);
44     valh = win_alloc_handle(h);
45     v = alloc_small(2, 0);
46     Field(v,0) = valname;
47     Field(v,1) = valh;
48   End_roots();
49   return v;
50 }
51
52 CAMLprim value win_findnext(valh)
53      value valh;
54 {
55   WIN32_FIND_DATA fileinfo;
56   BOOL retcode;
57
58   retcode = FindNextFile(Handle_val(valh), &fileinfo);
59   if (!retcode) {
60     DWORD err = GetLastError();
61     if (err == ERROR_NO_MORE_FILES)
62       raise_end_of_file();
63     else {
64       win32_maperr(err);
65       uerror("readdir", Nothing);
66     }
67   }
68   return copy_string(fileinfo.cFileName);
69 }
70
71 CAMLprim value win_findclose(valh)
72      value valh;
73 {
74   if (! FindClose(Handle_val(valh))) {
75     win32_maperr(GetLastError());
76     uerror("closedir", Nothing);
77   }
78   return Val_unit;
79 }
80