1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
6 /*                                                                        */
7 /*   Copyright 2001 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 #include <stdio.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <caml/mlvalues.h>
20 #include <caml/alloc.h>
21 #include "unixsupport.h"
22 
23 extern int error_table[];
24 
unix_error_message(value err)25 CAMLprim value unix_error_message(value err)
26 {
27   int errnum;
28   char buffer[512];
29 
30   errnum = Is_block(err) ? Int_val(Field(err, 0)) : error_table[Int_val(err)];
31   if (errnum > 0)
32     return caml_copy_string(strerror(errnum));
33   if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
34                     NULL,
35                     -errnum,
36                     0,
37                     buffer,
38                     sizeof(buffer),
39                     NULL))
40     return caml_copy_string(buffer);
41   sprintf(buffer, "unknown error #%d", errnum);
42   return caml_copy_string(buffer);
43 }
44