summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/Libgpg-error/strerror.c
blob: 4013e7ae269c5f5199ea1854e810f2780e1ff275 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* strerror.c - Describing an error code.
   Copyright (C) 2003 g10 Code GmbH

   This file is part of libgpg-error.

   libgpg-error is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   libgpg-error is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with libgpg-error; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   02111-1307, USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <gpg-error.h>

#include "gettext.h"
#include "err-codes.h"

/* Return a pointer to a string containing a description of the error
   code in the error value ERR.  This function is not thread-safe.  */
const char *
_gpg_strerror (gpg_error_t err)
{
  gpg_err_code_t code = gpg_err_code (err);

  if (code & GPG_ERR_SYSTEM_ERROR)
    {
      int no = gpg_err_code_to_errno (code);
      if (no)
	return strerror (no);
      else
	code = GPG_ERR_UNKNOWN_ERRNO;
    }
  return dgettext (PACKAGE, msgstr + msgidx[msgidxof (code)]);
}


#ifdef HAVE_STRERROR_R
#ifdef STRERROR_R_CHAR_P
/* The GNU C library and probably some other systems have this weird
   variant of strerror_r.  */

/* Return a dynamically allocated string in *STR describing the system
   error NO.  If this call succeeds, return 1.  If this call fails due
   to a resource shortage, set *STR to NULL and return 1.  If this
   call fails because the error number is not valid, don't set *STR
   and return 0.  */
static int
system_strerror_r (int no, char *buf, size_t buflen)
{
  char *errstr;

  errstr = strerror_r (no, buf, buflen);
  if (errstr != buf)
    {
      size_t errstr_len = strlen (errstr) + 1;
      size_t cpy_len = errstr_len < buflen ? errstr_len : buflen;
      memcpy (buf, errstr, cpy_len);

      return cpy_len == errstr_len ? 0 : ERANGE;
    }
  else
    {
      /* We can not tell if the buffer was large enough, but we can
	 try to make a guess.  */
      if (strlen (buf) + 1 >= buflen)
	return ERANGE;

      return 0;
    }
}

#else	/* STRERROR_R_CHAR_P */
/* Now the POSIX version.  */

static int
system_strerror_r (int no, char *buf, size_t buflen)
{
  return strerror_r (no, buf, buflen);
}

#endif	/* STRERROR_R_CHAR_P */

#else	/* HAVE_STRERROR_H */
/* Without strerror_r(), we can still provide a non-thread-safe
   version.  Maybe we are even lucky and the system's strerror() is
   already thread-safe.  */

static int
system_strerror_r(int no, char *buf, size_t buflen)
{
	char *errstr = strerror(no);

	if (!errstr)
	{
		int saved_errno = errno;

		if (saved_errno != EINVAL)
			_snprintf(buf, buflen, "strerror failed: %i\n", errno);
		return saved_errno;
	}
	else
	{
		size_t errstr_len = strlen(errstr) + 1;
		size_t cpy_len = errstr_len < buflen ? errstr_len : buflen;
		memcpy(buf, errstr, cpy_len);
		return cpy_len == errstr_len ? 0 : ERANGE;
	}
}
#endif


/* Return the error string for ERR in the user-supplied buffer BUF of
   size BUFLEN.  This function is, in contrast to gpg_strerror,
   thread-safe if a thread-safe strerror_r() function is provided by
   the system.  If the function succeeds, 0 is returned and BUF
   contains the string describing the error.  If the buffer was not
   large enough, ERANGE is returned and BUF contains as much of the
   beginning of the error string as fits into the buffer.  */
int
_gpg_strerror_r (gpg_error_t err, char *buf, size_t buflen)
{
  gpg_err_code_t code = gpg_err_code (err);
  const char *errstr;
  size_t errstr_len;
  size_t cpy_len;

  if (code & GPG_ERR_SYSTEM_ERROR)
    {
      int no = gpg_err_code_to_errno (code);
      if (no)
	{
	  int system_err = system_strerror_r (no, buf, buflen);

	  if (system_err != EINVAL)
	    {
	      if (buflen)
		buf[buflen - 1] = '\0';
	      return system_err;
	    }
	}
      code = GPG_ERR_UNKNOWN_ERRNO;
    }

  errstr = dgettext (PACKAGE, msgstr + msgidx[msgidxof (code)]);
  errstr_len = strlen (errstr) + 1;
  cpy_len = errstr_len < buflen ? errstr_len : buflen;
  memcpy (buf, errstr, cpy_len);
  if (buflen)
    buf[buflen - 1] = '\0';

  return cpy_len == errstr_len ? 0 : ERANGE;
}