Updating to version 1.13. Libgcrypt depends on libgpg-error
[platform/upstream/libgpg-error.git] / src / code-from-errno.c
1 /* code-from-errno.c - Mapping errnos to error codes.
2    Copyright (C) 2003 g10 Code GmbH
3
4    This file is part of libgpg-error.
5
6    libgpg-error is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public License
8    as published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10  
11    libgpg-error is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15  
16    You should have received a copy of the GNU Lesser General Public
17    License along with libgpg-error; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <errno.h> 
26
27 #include <gpg-error.h>
28
29 #include "code-from-errno.h"
30
31 /* Retrieve the error code for the system error ERR.  This returns
32    GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report
33    this).  */
34 gpg_err_code_t
35 gpg_err_code_from_errno (int err)
36 {
37   int idx;
38
39   if (!err)
40     return GPG_ERR_NO_ERROR;
41
42   idx = errno_to_idx (err);
43
44   if (idx < 0)
45     return GPG_ERR_UNKNOWN_ERRNO;
46
47   return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
48 }
49
50
51 /* Retrieve the error code directly from the ERRNO variable.  This
52    returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped
53    (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */
54 gpg_err_code_t
55 gpg_err_code_from_syserror (void)
56 {
57   int err = errno;
58   int idx;
59
60   if (!err)
61     return GPG_ERR_MISSING_ERRNO;
62
63   idx = errno_to_idx (err);
64
65   if (idx < 0)
66     return GPG_ERR_UNKNOWN_ERRNO;
67
68   return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx];
69 }