PostgreSQL Source Code  git master
strerror.c File Reference
#include "c.h"
Include dependency graph for strerror.c:

Go to the source code of this file.

Functions

static char * gnuish_strerror_r (int errnum, char *buf, size_t buflen)
 
static char * get_errno_symbol (int errnum)
 
char * pg_strerror (int errnum)
 
char * pg_strerror_r (int errnum, char *buf, size_t buflen)
 

Function Documentation

◆ get_errno_symbol()

static char * get_errno_symbol ( int  errnum)
static

Definition at line 113 of file strerror.c.

References buf, EADDRINUSE, EADDRNOTAVAIL, EAFNOSUPPORT, EAGAIN, ECONNABORTED, ECONNREFUSED, ECONNRESET, EHOSTUNREACH, EIDRM, EINPROGRESS, EINTR, EISCONN, EMSGSIZE, ENOBUFS, ENOTCONN, ENOTSOCK, ENOTSUP, EOPNOTSUPP, EOVERFLOW, EPROTONOSUPPORT, EWOULDBLOCK, and snprintf.

Referenced by pg_strerror_r().

114 {
115  switch (errnum)
116  {
117  case E2BIG:
118  return "E2BIG";
119  case EACCES:
120  return "EACCES";
121 #ifdef EADDRINUSE
122  case EADDRINUSE:
123  return "EADDRINUSE";
124 #endif
125 #ifdef EADDRNOTAVAIL
126  case EADDRNOTAVAIL:
127  return "EADDRNOTAVAIL";
128 #endif
129  case EAFNOSUPPORT:
130  return "EAFNOSUPPORT";
131 #ifdef EAGAIN
132  case EAGAIN:
133  return "EAGAIN";
134 #endif
135 #ifdef EALREADY
136  case EALREADY:
137  return "EALREADY";
138 #endif
139  case EBADF:
140  return "EBADF";
141 #ifdef EBADMSG
142  case EBADMSG:
143  return "EBADMSG";
144 #endif
145  case EBUSY:
146  return "EBUSY";
147  case ECHILD:
148  return "ECHILD";
149 #ifdef ECONNABORTED
150  case ECONNABORTED:
151  return "ECONNABORTED";
152 #endif
153  case ECONNREFUSED:
154  return "ECONNREFUSED";
155 #ifdef ECONNRESET
156  case ECONNRESET:
157  return "ECONNRESET";
158 #endif
159  case EDEADLK:
160  return "EDEADLK";
161  case EDOM:
162  return "EDOM";
163  case EEXIST:
164  return "EEXIST";
165  case EFAULT:
166  return "EFAULT";
167  case EFBIG:
168  return "EFBIG";
169 #ifdef EHOSTUNREACH
170  case EHOSTUNREACH:
171  return "EHOSTUNREACH";
172 #endif
173  case EIDRM:
174  return "EIDRM";
175  case EINPROGRESS:
176  return "EINPROGRESS";
177  case EINTR:
178  return "EINTR";
179  case EINVAL:
180  return "EINVAL";
181  case EIO:
182  return "EIO";
183 #ifdef EISCONN
184  case EISCONN:
185  return "EISCONN";
186 #endif
187  case EISDIR:
188  return "EISDIR";
189 #ifdef ELOOP
190  case ELOOP:
191  return "ELOOP";
192 #endif
193  case EMFILE:
194  return "EMFILE";
195  case EMLINK:
196  return "EMLINK";
197  case EMSGSIZE:
198  return "EMSGSIZE";
199  case ENAMETOOLONG:
200  return "ENAMETOOLONG";
201  case ENFILE:
202  return "ENFILE";
203  case ENOBUFS:
204  return "ENOBUFS";
205  case ENODEV:
206  return "ENODEV";
207  case ENOENT:
208  return "ENOENT";
209  case ENOEXEC:
210  return "ENOEXEC";
211  case ENOMEM:
212  return "ENOMEM";
213  case ENOSPC:
214  return "ENOSPC";
215  case ENOSYS:
216  return "ENOSYS";
217 #ifdef ENOTCONN
218  case ENOTCONN:
219  return "ENOTCONN";
220 #endif
221  case ENOTDIR:
222  return "ENOTDIR";
223 #if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
224  case ENOTEMPTY:
225  return "ENOTEMPTY";
226 #endif
227 #ifdef ENOTSOCK
228  case ENOTSOCK:
229  return "ENOTSOCK";
230 #endif
231 #ifdef ENOTSUP
232  case ENOTSUP:
233  return "ENOTSUP";
234 #endif
235  case ENOTTY:
236  return "ENOTTY";
237  case ENXIO:
238  return "ENXIO";
239 #if defined(EOPNOTSUPP) && (!defined(ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
240  case EOPNOTSUPP:
241  return "EOPNOTSUPP";
242 #endif
243 #ifdef EOVERFLOW
244  case EOVERFLOW:
245  return "EOVERFLOW";
246 #endif
247  case EPERM:
248  return "EPERM";
249  case EPIPE:
250  return "EPIPE";
251  case EPROTONOSUPPORT:
252  return "EPROTONOSUPPORT";
253  case ERANGE:
254  return "ERANGE";
255 #ifdef EROFS
256  case EROFS:
257  return "EROFS";
258 #endif
259  case ESRCH:
260  return "ESRCH";
261 #ifdef ETIMEDOUT
262  case ETIMEDOUT:
263  return "ETIMEDOUT";
264 #endif
265 #ifdef ETXTBSY
266  case ETXTBSY:
267  return "ETXTBSY";
268 #endif
269 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
270  case EWOULDBLOCK:
271  return "EWOULDBLOCK";
272 #endif
273  case EXDEV:
274  return "EXDEV";
275  }
276 
277  return NULL;
278 }
#define ECONNABORTED
Definition: win32_port.h:331
#define EAGAIN
Definition: win32_port.h:321
#define ENOTSUP
Definition: private.h:38
#define ENOTSOCK
Definition: win32_port.h:345
#define EHOSTUNREACH
Definition: win32_port.h:353
#define EADDRNOTAVAIL
Definition: win32_port.h:351
#define ECONNREFUSED
Definition: win32_port.h:343
#define ENOTCONN
Definition: win32_port.h:355
#define EISCONN
Definition: win32_port.h:337
#define EIDRM
Definition: win32_port.h:90
#define EAFNOSUPPORT
Definition: win32_port.h:327
#define EPROTONOSUPPORT
Definition: win32_port.h:341
#define EADDRINUSE
Definition: win32_port.h:349
#define ECONNRESET
Definition: win32_port.h:333
#define EOPNOTSUPP
Definition: win32_port.h:347
#define ENOBUFS
Definition: win32_port.h:339
#define EINPROGRESS
Definition: win32_port.h:335
#define EWOULDBLOCK
Definition: win32_port.h:329
#define EINTR
Definition: win32_port.h:323
#define EMSGSIZE
Definition: win32_port.h:325
#define EOVERFLOW
Definition: private.h:41

◆ gnuish_strerror_r()

static char * gnuish_strerror_r ( int  errnum,
char *  buf,
size_t  buflen 
)
static

Definition at line 85 of file strerror.c.

References buf, strerror, strerror_r, and strlcpy().

Referenced by pg_strerror_r().

86 {
87 #ifdef HAVE_STRERROR_R
88 #ifdef STRERROR_R_INT
89  /* POSIX API */
90  if (strerror_r(errnum, buf, buflen) == 0)
91  return buf;
92  return NULL; /* let caller deal with failure */
93 #else
94  /* GNU API */
95  return strerror_r(errnum, buf, buflen);
96 #endif
97 #else /* !HAVE_STRERROR_R */
98  char *sbuf = strerror(errnum);
99 
100  if (sbuf == NULL) /* can this still happen anywhere? */
101  return NULL;
102  /* To minimize thread-unsafety hazard, copy into caller's buffer */
103  strlcpy(buf, sbuf, buflen);
104  return buf;
105 #endif
106 }
static char * buf
Definition: pg_test_fsync.c:68
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define strerror
Definition: port.h:205
#define strerror_r
Definition: port.h:209

◆ pg_strerror()

char* pg_strerror ( int  errnum)

Definition at line 35 of file strerror.c.

References pg_strerror_r(), and PG_STRERROR_R_BUFLEN.

36 {
37  static char errorstr_buf[PG_STRERROR_R_BUFLEN];
38 
39  return pg_strerror_r(errnum, errorstr_buf, sizeof(errorstr_buf));
40 }
#define PG_STRERROR_R_BUFLEN
Definition: port.h:210
char * pg_strerror_r(int errnum, char *buf, size_t buflen)
Definition: strerror.c:46

◆ pg_strerror_r()

char* pg_strerror_r ( int  errnum,
char *  buf,
size_t  buflen 
)

Definition at line 46 of file strerror.c.

References _, buf, get_errno_symbol(), gnuish_strerror_r(), snprintf, and generate_unaccent_rules::str.

Referenced by pg_strerror().

47 {
48  char *str;
49 
50  /* If it's a Windows Winsock error, that needs special handling */
51 #ifdef WIN32
52  /* Winsock error code range, per WinError.h */
53  if (errnum >= 10000 && errnum <= 11999)
54  return win32_socket_strerror(errnum, buf, buflen);
55 #endif
56 
57  /* Try the platform's strerror_r(), or maybe just strerror() */
58  str = gnuish_strerror_r(errnum, buf, buflen);
59 
60  /*
61  * Some strerror()s return an empty string for out-of-range errno. This
62  * is ANSI C spec compliant, but not exactly useful. Also, we may get
63  * back strings of question marks if libc cannot transcode the message to
64  * the codeset specified by LC_CTYPE. If we get nothing useful, first try
65  * get_errno_symbol(), and if that fails, print the numeric errno.
66  */
67  if (str == NULL || *str == '\0' || *str == '?')
68  str = get_errno_symbol(errnum);
69 
70  if (str == NULL)
71  {
72  snprintf(buf, buflen, _("operating system error %d"), errnum);
73  str = buf;
74  }
75 
76  return str;
77 }
static char * buf
Definition: pg_test_fsync.c:68
static char * get_errno_symbol(int errnum)
Definition: strerror.c:113
static char * gnuish_strerror_r(int errnum, char *buf, size_t buflen)
Definition: strerror.c:85
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:84