blob: f08bf2f4bdde77ec7bbbd21c2f484d58988be0b2 [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "kudu/util/errno.h"
#include <errno.h>
#include <string.h>
#include "kudu/util/logging.h"
namespace kudu {
void ErrnoToCString(int err, char *buf, size_t buf_len) {
CHECK_GT(buf_len, 0);
#if !defined(__GLIBC__) || \
((_POSIX_C_SOURCE >= 200112 || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
// Using POSIX version 'int strerror_r(...)'.
int ret = strerror_r(err, buf, buf_len);
if (ret && ret != ERANGE && ret != EINVAL) {
strncpy(buf, "unknown error", buf_len);
buf[buf_len - 1] = '\0';
}
#else
// Using GLIBC version
char* ret = strerror_r(err, buf, buf_len);
if (ret != buf) {
strncpy(buf, ret, buf_len);
buf[buf_len - 1] = '\0';
}
#endif
}
} // namespace kudu