James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file str_error.c System error messages |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #define _GNU_SOURCE 1 |
| 7 | #define __EXTENSIONS__ 1 |
| 8 | #include <string.h> |
| 9 | #include <re_types.h> |
| 10 | #include <re_fmt.h> |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * Look up an error message string corresponding to an error number. |
| 15 | * |
| 16 | * @param errnum Error Code |
| 17 | * @param buf Buffer for storing error message |
| 18 | * @param sz Buffer size |
| 19 | * |
| 20 | * @return Error message string |
| 21 | */ |
| 22 | const char *str_error(int errnum, char *buf, size_t sz) |
| 23 | { |
| 24 | const char *s; |
| 25 | |
| 26 | if (!buf || !sz) |
| 27 | return NULL; |
| 28 | |
| 29 | buf[0] = '\0'; |
| 30 | #ifdef HAVE_STRERROR_R |
| 31 | |
| 32 | #ifdef __GLIBC__ |
| 33 | s = strerror_r(errnum, buf, sz); |
| 34 | #else |
| 35 | (void)strerror_r(errnum, buf, sz); |
| 36 | s = buf; |
| 37 | #endif |
| 38 | |
| 39 | #elif defined (WIN32) & !defined (__MINGW32__) |
| 40 | (void)strerror_s(buf, sz, errnum); |
| 41 | s = buf; |
| 42 | #else |
| 43 | /* fallback */ |
| 44 | (void)errnum; |
| 45 | s = "unknown error"; |
| 46 | #endif |
| 47 | |
| 48 | buf[sz - 1] = '\0'; |
| 49 | |
| 50 | return s; |
| 51 | } |