void fatal_perror(const char *fmt, ...) { va_list args; char *buf, buf2[4096]; int errno_save = errno; #ifdef _WIN32 char errbuf[256]; #else char *errbuf; #endif #ifdef _WIN32 strerror_s(errbuf, sizeof(errbuf), errno_save); #else errbuf = strerror(errno_save); #endif checkday(); buf = log_gettimestamp(); va_start(args, fmt); ircvsnprintf(buf2, sizeof(buf2), fmt, args); va_end(args); if (!NoLogs && logfile) fprintf(logfile, "%sFATAL: %s: %s\n", buf, buf2, errbuf); if (stderr) fprintf(stderr, "%sFATAL: %s: %s\n", buf, buf2, errbuf); if (servsock >= 0) denora_cmd_global(NULL, langstring(GLOBAL_FATAL_ERROR), buf2, errbuf); exit(1); }
char * bson_strerror_r (int err_code, /* IN */ char *buf, /* IN */ size_t buflen) /* IN */ { static const char *unknown_msg = "Unknown error"; char *ret = NULL; #if defined(_WIN32) if (strerror_s (buf, buflen, err_code) != 0) { ret = buf; } #elif defined(__GNUC__) && defined(_GNU_SOURCE) ret = strerror_r (err_code, buf, buflen); #else /* XSI strerror_r */ if (strerror_r (err_code, buf, buflen) == 0) { ret = buf; } #endif if (!ret) { bson_strncpy (buf, unknown_msg, buflen); ret = buf; } return ret; }
void _accept() { #if _WIN32 int client_length = sizeof(sockaddr_in); #else socklen_t client_length = sizeof(sockaddr_in); #endif _m_accepted_fd = ::accept(_m_socket_fd, (sockaddr *)&_m_client_address, &client_length); // -1 on failure if (_m_accepted_fd < 0) { std::string err = "Unable to connect to socket - error number: "; err += errno; err += " -- "; #if _WIN32 char buffer[256]; err += strerror_s(buffer, 256, errno); #else err += strerror(errno); #endif throw std::runtime_error(err); } }
static char* compat53_strerror (int en, char* buff, size_t sz) { #if COMPAT53_HAVE_STRERROR_R /* use strerror_r here, because it's available on these specific platforms */ #if COMPAT53_HAVE_STRERROR_R_XSI /* XSI Compliant */ strerror_r(en, buff, sz); return buff; #else /* GNU-specific which returns const char* */ return strerror_r(en, buff, sz); #endif #elif COMPAT53_HAVE_STRERROR_S /* for MSVC and other C11 implementations, use strerror_s * since it's provided by default by the libraries */ strerror_s(buff, sz, en); return buff; #else /* fallback, but * strerror is not guaranteed to be threadsafe due to modifying * errno itself and some impls not locking a static buffer for it * ... but most known systems have threadsafe errno: this might only change * if the locale is changed out from under someone while this function is being called */ (void)buff; (void)sz; return strerror(en); #endif }
std::string errno_string(int err) { char buffer[1024]; if (strerror_s(buffer, sizeof(buffer), err)) throw std::runtime_error("strerror_s failed"); return buffer; }
static std::string thread_safe_strerror(int errnum) { #define ERRBUF_SIZE 512 #ifdef HAVE_STRERROR_S char buf[ERRBUF_SIZE] = { 0 }; strerror_s(buf, ERRBUF_SIZE, errnum); return std::string(buf); #else #if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE char buf[ERRBUF_SIZE] = { 0 }; strerror_r(errnum, buf, ERRBUF_SIZE); return std::string(buf); #elif _GNU_SOURCE /* GNU variant can return a pointer to static buffer instead of buf */ char buf[ERRBUF_SIZE] = { 0 }; return std::string(strerror_r(errnum, buf, ERRBUF_SIZE)); #else return std::string("An error occured with errnum ") + std::to_string(errnum) + ". Converting to the appropriate error message is disabled" "in this instance of the png++ library."; #endif #endif #undef ERRBUF_SIZE }
static const char *file_stdio_ferrmsg(ALLEGRO_FILE *f) { USERDATA *userdata = get_userdata(f); if (userdata->errnum == 0) return ""; /* Note: at this time MinGW has neither strerror_r nor strerror_s. */ #if defined(ALLEGRO_HAVE_STRERROR_R) { int rc = strerror_r(userdata->errnum, userdata->errmsg, sizeof(userdata->errmsg)); if (rc == 0) { return userdata->errmsg; } } #endif #if defined(ALLEGRO_HAVE_STRERROR_S) { errno_t rc = strerror_s(userdata->errmsg, sizeof(userdata->errmsg), userdata->errnum); if (rc == 0) { return userdata->errmsg; } } #endif return ""; }
/** * @brief: A secure version of Windows strerror. * * @param[in] errnum Error number. * @param[in] strErrMsg User-supplied message. * * @returns a pointer to the error-message string. * @see strerror on MSDN. */ char * win_strerror(int errnum) { static char buffer[MAXPATHLEN+1] = {'\0'}; strerror_s(buffer, sizeof(buffer)-1, errnum); return buffer; }
/* * For C-standard/POSIX/OS specific/Custom/JNI errors, this function is called. It sets a pointer which is checked * by java method when native function returns. If the pointer is set exception of class as set by this function is * thrown. * * The type 1 indicates standard (C-standard/POSIX) error, 2 indicate custom (defined by this library) * error, 3 indicates custom error with message string, 4 indicates error number specific to Windows OS. */ void throw_serialcom_exception(JNIEnv *env, int type, int error_code, const char *msg) { DWORD ret = -1; errno_t err_code = 0; char buffer[256]; jclass serialComExceptionClass = NULL; (*env)->ExceptionClear(env); serialComExceptionClass = (*env)->FindClass(env, SCOMEXPCLASS); if ((serialComExceptionClass == NULL) || ((*env)->ExceptionOccurred(env) != NULL)) { (*env)->ExceptionClear(env); LOGE(E_FINDCLASSSCOMEXPSTR, FAILTHOWEXP); return; } switch (type) { case 4: /* Caller has given Windows error code */ memset(buffer, '\0', sizeof(buffer)); ret = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&buffer, sizeof(buffer), NULL); if (ret == 0) { LOGEN(FAILTHOWEXP, "FormatMessageA()", GetLastError()); } ret = (*env)->ThrowNew(env, serialComExceptionClass, buffer); if (ret < 0) { LOGE(FAILTHOWEXP, buffer); } break; case 3: /* Caller has given exception message explicitly */ ret = (*env)->ThrowNew(env, serialComExceptionClass, msg); if (ret < 0) { LOGE(FAILTHOWEXP, msg); } break; case 2: /* Caller has given custom error code, need to get exception message corresponding to this code. */ switch (error_code) { default: _snprintf_s(buffer, 256, 256, "%s\0", E_UNKNOWN); } ret = (*env)->ThrowNew(env, serialComExceptionClass, buffer); if (ret < 0) { LOGE(FAILTHOWEXP, buffer); } break; case 1: /* Caller has given posix error code, get error message corresponding to this code. */ return; case 5: /* Caller has given <errno.h> error code for windows, get error message corresponding to this code. */ memset(buffer, '\0', sizeof(buffer)); err_code = strerror_s(buffer, sizeof(buffer), error_code); if (err_code != 0) { LOGEN(FAILTHOWEXP, "strerror_s()", err_code); } } }
nfsstat3 CNFS3Prog::ProcedureWRITE(void) { char *path; offset3 offset; count3 count; stable_how stable; opaque data; wcc_data file_wcc; writeverf3 verf; nfsstat3 stat; FILE *pFile; PrintLog("WRITE"); path = GetPath(); Read(&offset); Read(&count); Read(&stable); Read(&data); stat = CheckFile(path); file_wcc.before.attributes_follow = GetFileAttributesForNFS(path, &file_wcc.before.attributes); if (stat == NFS3_OK) { pFile = _fsopen(path, "r+b", _SH_DENYWR); if (pFile != NULL) { _fseeki64(pFile, offset, SEEK_SET) ; count = fwrite(data.contents, sizeof(char), data.length, pFile); fclose(pFile); } else { char buffer[BUFFER_SIZE]; errno_t errorNumber = errno; strerror_s(buffer, BUFFER_SIZE, errorNumber); PrintLog(buffer); if (errorNumber == 13) { stat = NFS3ERR_ACCES; } else { stat = NFS3ERR_IO; } } stable = FILE_SYNC; verf = 0; } file_wcc.after.attributes_follow = GetFileAttributesForNFS(path, &file_wcc.after.attributes); Write(&stat); Write(&file_wcc); if (stat == NFS3_OK) { Write(&count); Write(&stable); Write(&verf); } return stat; }
char* err() { static char buf[2048]; strerror_s(buf, errno); // strerror(errno) return buf; }
std::string errno_str() { char buffer[1024]; errno_t e = strerror_s(buffer, sizeof(buffer), errno); if (e == 0) { return buffer; } return "errno:" + std::to_string(errno); }
char* __strerror(int errnum) { #ifdef _MSC_VER char errBuf[512]; strerror_s(errBuf, errnum); return errBuf; #else return strerror(errnum); #endif };
nfsstat3 CNFS3Prog::ProcedureCREATE(void) { char *path; createhow3 how; post_op_fh3 obj; post_op_attr obj_attributes; wcc_data dir_wcc; nfsstat3 stat; FILE *pFile; PrintLog("CREATE"); std::string dirName; std::string fileName; ReadDirectory(dirName, fileName); path = GetFullPath(dirName, fileName); Read(&how); dir_wcc.before.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.before.attributes); pFile = _fsopen(path, "wb", _SH_DENYWR); if (pFile != NULL) { fclose(pFile); stat = NFS3_OK; } else { char buffer[BUFFER_SIZE]; errno_t errorNumber = errno; strerror_s(buffer, BUFFER_SIZE, errorNumber); PrintLog(buffer); if (errorNumber == 2) { stat = NFS3ERR_STALE; } else if (errorNumber == 13) { stat = NFS3ERR_ACCES; } else { stat = NFS3ERR_IO; } } if (stat == NFS3_OK) { obj.handle_follows = GetFileHandle(path, &obj.handle); obj_attributes.attributes_follow = GetFileAttributesForNFS(path, &obj_attributes.attributes); } dir_wcc.after.attributes_follow = GetFileAttributesForNFS((char*)dirName.c_str(), &dir_wcc.after.attributes); Write(&stat); if (stat == NFS3_OK) { Write(&obj); Write(&obj_attributes); } Write(&dir_wcc); return stat; }
char *oscap_strerror_r(int errnum, char *buf, size_t buflen) { #ifdef OS_WINDOWS strerror_s(buf, buflen, errnum); return buf; #else return strerror_r(errnum, buf, buflen); #endif }
std::string errnoWithDescription(int errNumber) { #if defined(_WIN32) if (errNumber == -1) errNumber = GetLastError(); #else if (errNumber < 0) errNumber = errno; #endif char buf[kBuflen]; char* msg{nullptr}; #if defined(__GNUC__) && defined(_GNU_SOURCE) && !(__ANDROID_API__ <= 22) && !defined(EMSCRIPTEN) msg = strerror_r(errNumber, buf, kBuflen); #elif defined(_WIN32) LPWSTR errorText = nullptr; FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, errNumber, 0, reinterpret_cast<LPWSTR>(&errorText), // output 0, // minimum size for output buffer nullptr); if (errorText) { ON_BLOCK_EXIT([&errorText] { LocalFree(errorText); }); std::string utf8ErrorText = toUtf8String(errorText); auto size = utf8ErrorText.find_first_of("\r\n"); if (size == std::string::npos) { // not found size = utf8ErrorText.length(); } if (size >= kBuflen) { size = kBuflen - 1; } memcpy(buf, utf8ErrorText.c_str(), size); buf[size] = '\0'; msg = buf; } else if (strerror_s(buf, kBuflen, errNumber) != 0) { msg = buf; } #else /* XSI strerror_r */ if (strerror_r(errNumber, buf, kBuflen) == 0) { msg = buf; } #endif if (!msg) { return str::stream() << kUnknownMsg << errNumber; } return {msg}; }
void mvm_get_error (char *message, size_t size, errno_t err) { #ifdef BUILD_WINDOWS strerror_s(message, size, err); #else cchar *src = strerror(err); if (src) { strncpy(message, src, size); } #endif }
/** * Returns a locale specific error text for the given error code (see 'errno'). This * method is thread-safe. */ std::string strerror(int err) { char buff[2048]; #ifdef _WIN32 strerror_s(buff, sizeof(buff), err); return buff; #else return strerror_r(err, buff, sizeof(buff)); #endif }
/** convert errno to string @param err [in] errno @note for both windows and linux */ inline std::string ConvertErrorNoToString(int err) { char errBuf[256]; #ifdef _WIN32 strerror_s(errBuf, sizeof(errBuf), err); return errBuf; #else return ::strerror_r(err, errBuf, sizeof(errBuf)); #endif }
std::string errstr(int err) { #ifdef _MSC_VER char buffer[1024]; strerror_s(buffer, sizeof(buffer), err); return buffer; #else // return strerror(err); return "UNIMPLEMENTED"; #endif }
void File::ThrowRuntimeError_(const AnsiString &message) { char error_msg[255]; strerror_s(error_msg, 255, errno); AnsiString complete_message = Formatter::FormatAsAnsi("{0} - Error: {1}", message, error_msg); throw std::runtime_error(complete_message); }
std::string posix_error_string(int errcode) { #ifdef _WIN32 char buff[255]; strerror_s(buff, 255, errcode); return std::string(buff); #else return std::string(strerror(errcode)); #endif }
std::string ErrorString() { int err = errno; char buf[256] = {0}; strerror_s(buf, err); return buf; }
/** * \brief Get an error string from an error code. */ static std::string get_error_string(int err) { char buf[512]; #if defined(__GNUC__) strerror_r(err, buf, sizeof(buf)); #elif defined(_MSC_VER) strerror_s(buf, sizeof(buf), err); #endif return std::string(buf); }
std::string get_errno_str(int e) { char buf[64]; memset(buf, 0, 64); #ifdef WIN32 strerror_s(buf, 63, e); #else strerror_r(e, buf, 63); #endif return std::string(buf); }
/** Prints packet timestaps regardless of format*/ int _tmain(int argc, _TCHAR* argv[]) { char errbuf[PCAP_ERRBUF_SIZE]; wchar_t cmd[1024]; wchar_t tshark_path[MAX_PATH]; wchar_t file_path[MAX_PATH]; if ( argc != 3 ) { wprintf(L"Prints packet timestaps regardless of format.\n"); wprintf(L"Usage:\n\t%ls <tshark path> <trace file>\n", argv[0]); return 1; } // conversion to short path name in case there are spaces if ( ! GetShortPathNameW(argv[1], tshark_path, MAX_PATH) || ! GetShortPathNameW(argv[2], file_path, MAX_PATH) ) { printf("Failed to convert paths to short form."); return 1; } // create tshark command, which will make the trace conversion and print in libpcap format to stdout if ( swprintf_s(cmd, 1024, L"%ls -r %ls -w - -F libpcap", tshark_path, file_path) < 0 ) { wprintf(L"Failed to create command\n"); return 1; } // start tshark FILE *tshark_out = _wpopen(cmd, L"rb"); if ( tshark_out == NULL ) { strerror_s(errbuf, PCAP_ERRBUF_SIZE, errno); printf("Failed run tshark: %s\n", errbuf); wprintf(L"Command: %ls", cmd); return 1; } // open stdout from tshark pcap_t *pcap = pcap_fopen_offline(tshark_out, errbuf); if ( pcap == NULL ) { printf("Error opening stream from tshark: %s\n", errbuf); return 1; } // print information about every packet int trace struct pcap_pkthdr hdr; while ( pcap_next(pcap, &hdr) ) { printf("packet: ts: %u.%06u, len: %4u, caplen: %4u\n", hdr.ts.tv_sec, hdr.ts.tv_usec, hdr.len, hdr.caplen); } // clean up pcap_close(pcap); _pclose(tshark_out); return 0; }
const char* error2str(errno_t err) { static char str[BUFFER_LENGTH] = ""; errno_t e = strerror_s( str, sizeof(str), err ); if ( e ) { return NULL; } return str; }
inline void strerror_x(int errNo, std::string* result) { #ifdef CFG_COMPILER_MSC_80_2005 // Eliminate warning C4996: 'strerror' was declared deprecated result->assign(128, '\0'); if (strerror_s(&result->at(0), result->size() - 1, errNo) != 0) result->assign("unknown error (cannot get error message)"); #else *result = strerror(errNo); #endif }
std::shared_ptr<ByteBuffer> File::ReadChunk(int iMaxSize) { std::shared_ptr<ByteBuffer> pFileContents = std::shared_ptr<ByteBuffer>(new ByteBuffer); if (file_ == nullptr) throw std::logic_error("Attempt to read from file which has not been opened."); // Create a buffer to hold the file pFileContents->Allocate(iMaxSize); // fread fails reading large files. If the file is too large, fread will read zero bytes and the // errno will be set to invalid argument. The below code therefore reads the file in chunks. int remaining_bytes = iMaxSize; BYTE *buffer_position = (BYTE*)pFileContents->GetBuffer(); int total_bytes_read = 0; while (remaining_bytes > 0) { int bytes_to_read = min(FileChunkSize, remaining_bytes); int bytes_actually_read = fread((void*)buffer_position, 1, bytes_to_read, file_); total_bytes_read += bytes_actually_read; if (bytes_actually_read != bytes_to_read) { if (feof(file_)) { // we've reached end of file. break; } char error_msg[255]; strerror_s(error_msg, 255, errno); throw std::runtime_error(Formatter::FormatAsAnsi("Unable to read file {0}. Expected bytes: {1}, Actual read bytes: {2}. Error: {3}", name_, bytes_to_read, bytes_actually_read, error_msg)); } buffer_position += bytes_actually_read; remaining_bytes -= bytes_actually_read; } pFileContents->DecreaseSize(iMaxSize - total_bytes_read); if (pFileContents->GetSize() == 0) { std::shared_ptr<ByteBuffer> empty; return empty; } return pFileContents; }
void ut_strerror(int errnum, char *buff, size_t bufflen) { switch (errnum) { case ENOTSUP: strcpy_s(buff, bufflen, ENOTSUP_STR); break; default: if (strerror_s(buff, bufflen, errnum)) strcpy_s(buff, bufflen, UNMAPPED_STR); } }