Switches over to cstring from string.h.

This commit is contained in:
Aaron Gyes 2019-03-12 15:07:07 -07:00
parent d5ac239f68
commit aaacdb89b6
35 changed files with 145 additions and 145 deletions

View File

@ -19,7 +19,7 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -97,7 +97,7 @@ int builtin_count_args(const wchar_t *const *argv) {
/// This function works like wperror, but it prints its result into the streams.err string instead /// This function works like wperror, but it prints its result into the streams.err string instead
/// to stderr. Used by the builtin commands. /// to stderr. Used by the builtin commands.
void builtin_wperror(const wchar_t *s, io_streams_t &streams) { void builtin_wperror(const wchar_t *s, io_streams_t &streams) {
char *err = strerror(errno); char *err = std::strerror(errno);
if (s != NULL) { if (s != NULL) {
streams.err.append(s); streams.err.append(s);
streams.err.append(L": "); streams.err.append(L": ");

View File

@ -3,7 +3,7 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <cstring>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>

View File

@ -57,7 +57,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/types.h> #include <sys/types.h>
#include <cwchar> #include <cwchar>
#include <wctype.h> #include <wctype.h>
@ -238,7 +238,7 @@ void builtin_printf_state_t::verify_numeric(const wchar_t *s, const wchar_t *end
if (errcode == ERANGE) { if (errcode == ERANGE) {
this->fatal_error(L"%ls: %ls", s, _(L"Number out of range")); this->fatal_error(L"%ls: %ls", s, _(L"Number out of range"));
} else { } else {
this->fatal_error(L"%ls: %s", s, strerror(errcode)); this->fatal_error(L"%ls: %s", s, std::strerror(errcode));
} }
} else if (*end) { } else if (*end) {
if (s == end) if (s == end)

View File

@ -1,7 +1,7 @@
// Implementation of the pwd builtin. // Implementation of the pwd builtin.
#include "config.h" // IWYU pragma: keep #include "config.h" // IWYU pragma: keep
#include <string.h> #include <cstring>
#include "builtin.h" #include "builtin.h"
#include "builtin_pwd.h" #include "builtin_pwd.h"
@ -58,7 +58,7 @@ int builtin_pwd(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
if (auto real_pwd = wrealpath(pwd)) { if (auto real_pwd = wrealpath(pwd)) {
pwd = std::move(*real_pwd); pwd = std::move(*real_pwd);
} else { } else {
const char *error = strerror(errno); const char *error = std::strerror(errno);
streams.err.append_format(L"%ls: realpath failed:", cmd, error); streams.err.append_format(L"%ls: realpath failed:", cmd, error);
return STATUS_CMD_ERROR; return STATUS_CMD_ERROR;
} }

View File

@ -6,7 +6,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -319,7 +319,7 @@ static int read_one_char_at_a_time(int fd, wcstring &buff, int nchars, bool spli
} else { } else {
size_t sz = std::mbrtowc(&res, &b, 1, &state); size_t sz = std::mbrtowc(&res, &b, 1, &state);
if (sz == (size_t)-1) { if (sz == (size_t)-1) {
memset(&state, 0, sizeof(state)); std::memset(&state, 0, sizeof(state));
} else if (sz != (size_t)-2) { } else if (sz != (size_t)-2) {
finished = true; finished = true;
} }

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <cwchar> #include <cwchar>
#include "builtin.h" #include "builtin.h"
@ -43,7 +43,7 @@ int builtin_realpath(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
// realpath() just couldn't do it. Report the error and make it clear // realpath() just couldn't do it. Report the error and make it clear
// this is an error from our builtin, not the system's realpath. // this is an error from our builtin, not the system's realpath.
streams.err.append_format(L"builtin %ls: %ls: %s\n", cmd, argv[optind], streams.err.append_format(L"builtin %ls: %ls: %s\n", cmd, argv[optind],
strerror(errno)); std::strerror(errno));
} else { } else {
// Who knows. Probably a bug in our wrealpath() implementation. // Who knows. Probably a bug in our wrealpath() implementation.
streams.err.append_format(_(L"builtin %ls: Invalid path: %ls\n"), cmd, argv[optind]); streams.err.append_format(_(L"builtin %ls: Invalid path: %ls\n"), cmd, argv[optind]);

View File

@ -4,7 +4,7 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -295,7 +295,7 @@ static bool validate_path_warning_on_colons(const wchar_t *cmd,
any_success = true; any_success = true;
} else if (looks_like_colon_sep) { } else if (looks_like_colon_sep) {
streams.err.append_format(BUILTIN_SET_PATH_ERROR, cmd, key, dir.c_str(), streams.err.append_format(BUILTIN_SET_PATH_ERROR, cmd, key, dir.c_str(),
strerror(errno)); std::strerror(errno));
streams.err.append_format(BUILTIN_SET_PATH_HINT, cmd, key, key, streams.err.append_format(BUILTIN_SET_PATH_HINT, cmd, key, key,
std::wcschr(dir.c_str(), L':') + 1); std::wcschr(dir.c_str(), L':') + 1);
} }

View File

@ -5,7 +5,7 @@
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -675,7 +675,7 @@ static bool parse_number(const wcstring &arg, number_t *number, wcstring_list_t
errors.push_back(format_string(_(L"Integer %lld in '%ls' followed by non-digit"), errors.push_back(format_string(_(L"Integer %lld in '%ls' followed by non-digit"),
integral, argcs)); integral, argcs));
} else { } else {
errors.push_back(format_string(L"%s: '%ls'", strerror(errno), argcs)); errors.push_back(format_string(L"%s: '%ls'", std::strerror(errno), argcs));
} }
return false; return false;
} }

View File

@ -11,7 +11,7 @@
#include "fallback.h" // IWYU pragma: keep #include "fallback.h" // IWYU pragma: keep
bool rgb_color_t::try_parse_special(const wcstring &special) { bool rgb_color_t::try_parse_special(const wcstring &special) {
memset(&data, 0, sizeof data); std::memset(&data, 0, sizeof data);
const wchar_t *name = special.c_str(); const wchar_t *name = special.c_str();
if (!wcscasecmp(name, L"normal")) { if (!wcscasecmp(name, L"normal")) {
this->type = type_normal; this->type = type_normal;
@ -108,7 +108,7 @@ static unsigned char convert_color(const unsigned char rgb[3], const uint32_t *c
} }
bool rgb_color_t::try_parse_rgb(const wcstring &name) { bool rgb_color_t::try_parse_rgb(const wcstring &name) {
memset(&data, 0, sizeof data); std::memset(&data, 0, sizeof data);
// We support the following style of rgb formats (case insensitive): // We support the following style of rgb formats (case insensitive):
// #FA3 // #FA3
// #F3A035 // #F3A035
@ -185,7 +185,7 @@ wcstring_list_t rgb_color_t::named_color_names() {
} }
bool rgb_color_t::try_parse_named(const wcstring &str) { bool rgb_color_t::try_parse_named(const wcstring &str) {
memset(&data, 0, sizeof data); std::memset(&data, 0, sizeof data);
size_t max = sizeof named_colors / sizeof *named_colors; size_t max = sizeof named_colors / sizeof *named_colors;
for (size_t idx = 0; idx < max; idx++) { for (size_t idx = 0; idx < max; idx++) {
if (0 == wcscasecmp(str.c_str(), named_colors[idx].name)) { if (0 == wcscasecmp(str.c_str(), named_colors[idx].name)) {
@ -299,7 +299,7 @@ void rgb_color_t::parse(const wcstring &str) {
if (!success) success = try_parse_named(str); if (!success) success = try_parse_named(str);
if (!success) success = try_parse_rgb(str); if (!success) success = try_parse_rgb(str);
if (!success) { if (!success) {
memset(&this->data, 0, sizeof this->data); std::memset(&this->data, 0, sizeof this->data);
this->type = type_none; this->type = type_none;
} }
} }

View File

@ -2,7 +2,7 @@
#ifndef FISH_COLOR_H #ifndef FISH_COLOR_H
#define FISH_COLOR_H #define FISH_COLOR_H
#include <string.h> #include <cstring>
#include <string> #include <string>
@ -159,7 +159,7 @@ class rgb_color_t {
/// Compare two colors for equality. /// Compare two colors for equality.
bool operator==(const rgb_color_t &other) const { bool operator==(const rgb_color_t &other) const {
return type == other.type && !memcmp(&data, &other.data, sizeof data); return type == other.type && !std::memcmp(&data, &other.data, sizeof data);
} }
/// Compare two colors for inequality. /// Compare two colors for inequality.

View File

@ -14,7 +14,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <termios.h> #include <termios.h>
@ -141,7 +141,7 @@ long convert_digit(wchar_t d, int base) {
} }
/// Test whether the char is a valid hex digit as used by the `escape_string_*()` functions. /// Test whether the char is a valid hex digit as used by the `escape_string_*()` functions.
static bool is_hex_digit(int c) { return strchr("0123456789ABCDEF", c) != NULL; } static bool is_hex_digit(int c) { return std::strchr("0123456789ABCDEF", c) != NULL; }
/// This is a specialization of `convert_digit()` that only handles base 16 and only uppercase. /// This is a specialization of `convert_digit()` that only handles base 16 and only uppercase.
long convert_hex_digit(wchar_t d) { long convert_hex_digit(wchar_t d) {
@ -170,8 +170,8 @@ bool is_windows_subsystem_for_linux() {
uname(&info); uname(&info);
// Sample utsname.release under WSL: 4.4.0-17763-Microsoft // Sample utsname.release under WSL: 4.4.0-17763-Microsoft
if (strstr(info.release, "Microsoft") != nullptr) { if (std::strstr(info.release, "Microsoft") != nullptr) {
const char *dash = strchr(info.release, '-'); const char *dash = std::strchr(info.release, '-');
if (dash == nullptr || strtod(dash + 1, nullptr) < 17763) { if (dash == nullptr || strtod(dash + 1, nullptr) < 17763) {
debug(1, "This version of WSL is not supported and fish will probably not work correctly!\n" debug(1, "This version of WSL is not supported and fish will probably not work correctly!\n"
"Please upgrade to Windows 10 1809 (17763) or higher to use fish!"); "Please upgrade to Windows 10 1809 (17763) or higher to use fish!");
@ -346,11 +346,11 @@ static wcstring str2wcs_internal(const char *in, const size_t in_len) {
wc = ENCODE_DIRECT_BASE + (unsigned char)in[in_pos]; wc = ENCODE_DIRECT_BASE + (unsigned char)in[in_pos];
result.push_back(wc); result.push_back(wc);
in_pos++; in_pos++;
memset(&state, 0, sizeof state); std::memset(&state, 0, sizeof state);
} else if (ret == 0) { // embedded null byte! } else if (ret == 0) { // embedded null byte!
result.push_back(L'\0'); result.push_back(L'\0');
in_pos++; in_pos++;
memset(&state, 0, sizeof state); std::memset(&state, 0, sizeof state);
} else { // normal case } else { // normal case
result.push_back(wc); result.push_back(wc);
in_pos += ret; in_pos += ret;
@ -362,7 +362,7 @@ static wcstring str2wcs_internal(const char *in, const size_t in_len) {
wcstring str2wcstring(const char *in, size_t len) { return str2wcs_internal(in, len); } wcstring str2wcstring(const char *in, size_t len) { return str2wcs_internal(in, len); }
wcstring str2wcstring(const char *in) { return str2wcs_internal(in, strlen(in)); } wcstring str2wcstring(const char *in) { return str2wcs_internal(in, std::strlen(in)); }
wcstring str2wcstring(const std::string &in) { wcstring str2wcstring(const std::string &in) {
// Handles embedded nulls! // Handles embedded nulls!
@ -423,11 +423,11 @@ std::string wcs2string(const wcstring &input) {
converted[0] = wc; converted[0] = wc;
result.append(converted, 1); result.append(converted, 1);
} else { } else {
memset(converted, 0, sizeof converted); std::memset(converted, 0, sizeof converted);
size_t len = std::wcrtomb(converted, wc, &state); size_t len = std::wcrtomb(converted, wc, &state);
if (len == (size_t)-1) { if (len == (size_t)-1) {
debug(1, L"Wide character U+%4X has no narrow representation", wc); debug(1, L"Wide character U+%4X has no narrow representation", wc);
memset(&state, 0, sizeof(state)); std::memset(&state, 0, sizeof(state));
} else { } else {
result.append(converted, len); result.append(converted, len);
} }
@ -467,7 +467,7 @@ static char *wcs2str_internal(const wchar_t *in, char *out) {
size_t len = std::wcrtomb(&out[out_pos], in[in_pos], &state); size_t len = std::wcrtomb(&out[out_pos], in[in_pos], &state);
if (len == (size_t)-1) { if (len == (size_t)-1) {
debug(1, L"Wide character U+%4X has no narrow representation", in[in_pos]); debug(1, L"Wide character U+%4X has no narrow representation", in[in_pos]);
memset(&state, 0, sizeof(state)); std::memset(&state, 0, sizeof(state));
} else { } else {
out_pos += len; out_pos += len;
} }
@ -707,8 +707,8 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para
size_t param_idx = 0; size_t param_idx = 0;
const char *cursor = msg; const char *cursor = msg;
while (*cursor != '\0') { while (*cursor != '\0') {
const char *end = strchr(cursor, '%'); const char *end = std::strchr(cursor, '%');
if (end == NULL) end = cursor + strlen(cursor); if (end == NULL) end = cursor + std::strlen(cursor);
ignore_result(write(STDERR_FILENO, cursor, end - cursor)); ignore_result(write(STDERR_FILENO, cursor, end - cursor));
@ -717,7 +717,7 @@ void debug_safe(int level, const char *msg, const char *param1, const char *para
assert(param_idx < sizeof params / sizeof *params); assert(param_idx < sizeof params / sizeof *params);
const char *format = params[param_idx++]; const char *format = params[param_idx++];
if (!format) format = "(null)"; if (!format) format = "(null)";
ignore_result(write(STDERR_FILENO, format, strlen(format))); ignore_result(write(STDERR_FILENO, format, std::strlen(format)));
cursor = end + 2; cursor = end + 2;
} else if (end[0] == '\0') { } else if (end[0] == '\0') {
// Must be at the end of the string. // Must be at the end of the string.
@ -2138,7 +2138,7 @@ void append_str(char *buff, const char *str, size_t *inout_idx, size_t max_len)
void format_size_safe(char buff[128], unsigned long long sz) { void format_size_safe(char buff[128], unsigned long long sz) {
const size_t buff_size = 128; const size_t buff_size = 128;
const size_t max_len = buff_size - 1; // need to leave room for a null terminator const size_t max_len = buff_size - 1; // need to leave room for a null terminator
memset(buff, 0, buff_size); std::memset(buff, 0, buff_size);
size_t idx = 0; size_t idx = 0;
const char *const sz_name[] = {"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL}; const char *const sz_name[] = {"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL};
if (sz < 1) { if (sz < 1) {
@ -2403,7 +2403,7 @@ void redirect_tty_output() {
[[noreturn]] void __fish_assert(const char *msg, const char *file, size_t line, int error) { [[noreturn]] void __fish_assert(const char *msg, const char *file, size_t line, int error) {
if (error) { if (error) {
debug(0, L"%s:%zu: failed assertion: %s: errno %d (%s)", file, line, msg, error, debug(0, L"%s:%zu: failed assertion: %s: errno %d (%s)", file, line, msg, error,
strerror(error)); std::strerror(error));
} else { } else {
debug(0, L"%s:%zu: failed assertion: %s", file, line, msg); debug(0, L"%s:%zu: failed assertion: %s", file, line, msg);
} }

View File

@ -9,7 +9,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
@ -411,7 +411,7 @@ static void init_locale(const environment_t &vars) {
debug(5, L"old LC_MESSAGES locale: '%s'", old_msg_locale); debug(5, L"old LC_MESSAGES locale: '%s'", old_msg_locale);
debug(5, L"new LC_MESSAGES locale: '%s'", new_msg_locale); debug(5, L"new LC_MESSAGES locale: '%s'", new_msg_locale);
#ifdef HAVE__NL_MSG_CAT_CNTR #ifdef HAVE__NL_MSG_CAT_CNTR
if (strcmp(old_msg_locale, new_msg_locale)) { if (std::strcmp(old_msg_locale, new_msg_locale)) {
// Make change known to GNU gettext. // Make change known to GNU gettext.
extern int _nl_msg_cat_cntr; extern int _nl_msg_cat_cntr;
_nl_msg_cat_cntr++; _nl_msg_cat_cntr++;
@ -455,7 +455,7 @@ static bool does_term_support_setting_title(const environment_t &vars) {
char buf[PATH_MAX]; char buf[PATH_MAX];
int retval = ttyname_r(STDIN_FILENO, buf, PATH_MAX); int retval = ttyname_r(STDIN_FILENO, buf, PATH_MAX);
if (retval != 0 || strstr(buf, "tty") || strstr(buf, "/vc/")) return false; if (retval != 0 || std::strstr(buf, "tty") || std::strstr(buf, "/vc/")) return false;
} }
return true; return true;

View File

@ -17,7 +17,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#ifdef __CYGWIN__ #ifdef __CYGWIN__
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
@ -123,7 +123,7 @@ static maybe_t<wcstring> default_vars_path() {
/// On success, updates the cursor to just past the command. /// On success, updates the cursor to just past the command.
static bool match(const wchar_t **inout_cursor, const char *cmd) { static bool match(const wchar_t **inout_cursor, const char *cmd) {
const wchar_t *cursor = *inout_cursor; const wchar_t *cursor = *inout_cursor;
size_t len = strlen(cmd); size_t len = std::strlen(cmd);
if (!std::equal(cmd, cmd + len, cursor)) { if (!std::equal(cmd, cmd + len, cursor)) {
return false; return false;
} }
@ -445,7 +445,7 @@ bool env_universal_t::write_to_fd(int fd, const wcstring &path) {
bool success = true; bool success = true;
std::string contents = serialize_with_vars(vars); std::string contents = serialize_with_vars(vars);
if (write_loop(fd, contents.data(), contents.size()) < 0) { if (write_loop(fd, contents.data(), contents.size()) < 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to write to universal variables file '%ls': %s"), path.c_str(), error); debug(0, _(L"Unable to write to universal variables file '%ls': %s"), path.c_str(), error);
success = false; success = false;
} }
@ -460,7 +460,7 @@ bool env_universal_t::write_to_fd(int fd, const wcstring &path) {
bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const wcstring &dst) { bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const wcstring &dst) {
int ret = wrename(src, dst); int ret = wrename(src, dst);
if (ret != 0) { if (ret != 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to rename file from '%ls' to '%ls': %s"), src.c_str(), dst.c_str(), debug(0, _(L"Unable to rename file from '%ls' to '%ls': %s"), src.c_str(), dst.c_str(),
error); error);
} }
@ -521,7 +521,7 @@ bool env_universal_t::open_temporary_file(const wcstring &directory, wcstring *o
} }
if (!success) { if (!success) {
const char *error = strerror(saved_errno); const char *error = std::strerror(saved_errno);
debug(0, _(L"Unable to open temporary file '%ls': %s"), out_path->c_str(), error); debug(0, _(L"Unable to open temporary file '%ls': %s"), out_path->c_str(), error);
} }
return success; return success;
@ -583,7 +583,7 @@ bool env_universal_t::open_and_acquire_lock(const wcstring &path, int *out_fd) {
continue; continue;
} }
#endif #endif
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to open universal variable file '%ls': %s"), path.c_str(), error); debug(0, _(L"Unable to open universal variable file '%ls': %s"), path.c_str(), error);
break; break;
} }
@ -801,7 +801,7 @@ uvar_format_t env_universal_t::format_for_contents(const std::string &s) {
if (sscanf(line.c_str(), "# VERSION: %64s", versionbuf) != 1) continue; if (sscanf(line.c_str(), "# VERSION: %64s", versionbuf) != 1) continue;
// Try reading the version. // Try reading the version.
if (!strcmp(versionbuf, UVARS_VERSION_3_0)) { if (!std::strcmp(versionbuf, UVARS_VERSION_3_0)) {
return uvar_format_t::fish_3_0; return uvar_format_t::fish_3_0;
} else { } else {
// Unknown future version. // Unknown future version.
@ -946,7 +946,7 @@ static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN],
strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1); strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1);
r.ifr_name[sizeof r.ifr_name - 1] = 0; r.ifr_name[sizeof r.ifr_name - 1] = 0;
if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0) { if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0) {
memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN); std::memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN);
result = true; result = true;
} }
close(dummy); close(dummy);
@ -978,7 +978,7 @@ static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN],
size_t alen = sdl.sdl_alen; size_t alen = sdl.sdl_alen;
if (alen > MAC_ADDRESS_MAX_LEN) alen = MAC_ADDRESS_MAX_LEN; if (alen > MAC_ADDRESS_MAX_LEN) alen = MAC_ADDRESS_MAX_LEN;
memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen, alen); std::memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen, alen);
ok = true; ok = true;
break; break;
} }
@ -1053,7 +1053,7 @@ class universal_notifier_shmem_poller_t : public universal_notifier_t {
bool errored = false; bool errored = false;
int fd = shm_open(path, O_RDWR | O_CREAT, 0600); int fd = shm_open(path, O_RDWR | O_CREAT, 0600);
if (fd < 0) { if (fd < 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to open shared memory with path '%s': %s"), path, error); debug(0, _(L"Unable to open shared memory with path '%s': %s"), path, error);
errored = true; errored = true;
} }
@ -1063,7 +1063,7 @@ class universal_notifier_shmem_poller_t : public universal_notifier_t {
if (!errored) { if (!errored) {
struct stat buf = {}; struct stat buf = {};
if (fstat(fd, &buf) < 0) { if (fstat(fd, &buf) < 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to fstat shared memory object with path '%s': %s"), path, debug(0, _(L"Unable to fstat shared memory object with path '%s': %s"), path,
error); error);
errored = true; errored = true;
@ -1074,7 +1074,7 @@ class universal_notifier_shmem_poller_t : public universal_notifier_t {
// Set the size, if it's too small. // Set the size, if it's too small.
bool set_size = !errored && size < (off_t)sizeof(universal_notifier_shmem_t); bool set_size = !errored && size < (off_t)sizeof(universal_notifier_shmem_t);
if (set_size && ftruncate(fd, sizeof(universal_notifier_shmem_t)) < 0) { if (set_size && ftruncate(fd, sizeof(universal_notifier_shmem_t)) < 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to truncate shared memory object with path '%s': %s"), path, error); debug(0, _(L"Unable to truncate shared memory object with path '%s': %s"), path, error);
errored = true; errored = true;
} }
@ -1084,7 +1084,7 @@ class universal_notifier_shmem_poller_t : public universal_notifier_t {
void *addr = mmap(NULL, sizeof(universal_notifier_shmem_t), PROT_READ | PROT_WRITE, void *addr = mmap(NULL, sizeof(universal_notifier_shmem_t), PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0); MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) { if (addr == MAP_FAILED) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
debug(0, _(L"Unable to memory map shared memory object with path '%s': %s"), path, debug(0, _(L"Unable to memory map shared memory object with path '%s': %s"), path,
error); error);
this->region = NULL; this->region = NULL;
@ -1489,7 +1489,7 @@ void universal_notifier_named_pipe_t::make_pipe(const wchar_t *test_path) {
int mkfifo_status = mkfifo(narrow_path.c_str(), 0600); int mkfifo_status = mkfifo(narrow_path.c_str(), 0600);
if (mkfifo_status == -1 && errno != EEXIST) { if (mkfifo_status == -1 && errno != EEXIST) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
const wchar_t *errmsg = _(L"Unable to make a pipe for universal variables using '%ls': %s"); const wchar_t *errmsg = _(L"Unable to make a pipe for universal variables using '%ls': %s");
debug(0, errmsg, vars_path.c_str(), error); debug(0, errmsg, vars_path.c_str(), error);
pipe_fd = -1; pipe_fd = -1;
@ -1498,7 +1498,7 @@ void universal_notifier_named_pipe_t::make_pipe(const wchar_t *test_path) {
int fd = wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600); int fd = wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600);
if (fd < 0) { if (fd < 0) {
const char *error = strerror(errno); const char *error = std::strerror(errno);
const wchar_t *errmsg = _(L"Unable to open a pipe for universal variables using '%ls': %s"); const wchar_t *errmsg = _(L"Unable to open a pipe for universal variables using '%ls': %s");
debug(0, errmsg, vars_path.c_str(), error); debug(0, errmsg, vars_path.c_str(), error);
pipe_fd = -1; pipe_fd = -1;

View File

@ -14,7 +14,7 @@
#include <spawn.h> #include <spawn.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <cstring>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#include <stack> #include <stack>
@ -78,7 +78,7 @@ static bool redirection_is_to_real_file(const shared_ptr<io_data_t> &io) {
if (io && io->io_mode == io_mode_t::file) { if (io && io->io_mode == io_mode_t::file) {
// It's a file redirection. Compare the path to /dev/null. // It's a file redirection. Compare the path to /dev/null.
const char *path = static_cast<const io_file_t *>(io.get())->filename_cstr; const char *path = static_cast<const io_file_t *>(io.get())->filename_cstr;
if (strcmp(path, "/dev/null") != 0) { if (std::strcmp(path, "/dev/null") != 0) {
// It's not /dev/null. // It's not /dev/null.
result = true; result = true;
} }
@ -104,9 +104,9 @@ char *get_interpreter(const char *command, char *interpreter, size_t buff_size)
close(fd); close(fd);
} }
if (strncmp(interpreter, "#! /", 4) == 0) { if (std::strncmp(interpreter, "#! /", 4) == 0) {
return interpreter + 3; return interpreter + 3;
} else if (strncmp(interpreter, "#!/", 3) == 0) { } else if (std::strncmp(interpreter, "#!/", 3) == 0) {
return interpreter + 2; return interpreter + 2;
} }
@ -1126,7 +1126,7 @@ static int exec_subshell_internal(const wcstring &cmd, parser_t &parser, wcstrin
const char *cursor = begin; const char *cursor = begin;
while (cursor < end) { while (cursor < end) {
// Look for the next separator. // Look for the next separator.
const char *stop = (const char *)memchr(cursor, '\n', end - cursor); const char *stop = (const char *)std::memchr(cursor, '\n', end - cursor);
const bool hit_separator = (stop != NULL); const bool hit_separator = (stop != NULL);
if (!hit_separator) { if (!hit_separator) {
// If it's not found, just use the end. // If it's not found, just use the end.

View File

@ -7,7 +7,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
#include <wctype.h> #include <wctype.h>
@ -1156,7 +1156,7 @@ expand_error_t expand_to_command_and_args(const wcstring &instr, const environme
static std::string escape_single_quoted_hack_hack_hack_hack(const char *str) { static std::string escape_single_quoted_hack_hack_hack_hack(const char *str) {
std::string result; std::string result;
size_t len = strlen(str); size_t len = std::strlen(str);
result.reserve(len + 2); result.reserve(len + 2);
result.push_back('\''); result.push_back('\'');
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {

View File

@ -14,7 +14,7 @@
#include <stdarg.h> // IWYU pragma: keep #include <stdarg.h> // IWYU pragma: keep
#include <stdio.h> // IWYU pragma: keep #include <stdio.h> // IWYU pragma: keep
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> // IWYU pragma: keep #include <sys/stat.h> // IWYU pragma: keep
#include <sys/types.h> // IWYU pragma: keep #include <sys/types.h> // IWYU pragma: keep
#include <unistd.h> #include <unistd.h>
@ -81,7 +81,7 @@ int fish_mkstemp_cloexec(char *name_template) {
return 0; return 0;
} }
memcpy(out, in, sizeof(wchar_t) * (len + 1)); std::memcpy(out, in, sizeof(wchar_t) * (len + 1));
return out; return out;
} }

View File

@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -69,9 +69,9 @@ class fish_cmd_opts_t {
static const char *s_profiling_output_filename = NULL; static const char *s_profiling_output_filename = NULL;
static bool has_suffix(const std::string &path, const char *suffix, bool ignore_case) { static bool has_suffix(const std::string &path, const char *suffix, bool ignore_case) {
size_t pathlen = path.size(), suffixlen = strlen(suffix); size_t pathlen = path.size(), suffixlen = std::strlen(suffix);
return pathlen >= suffixlen && return pathlen >= suffixlen &&
!(ignore_case ? strcasecmp : strcmp)(path.c_str() + pathlen - suffixlen, suffix); !(ignore_case ? strcasecmp : std::strcmp)(path.c_str() + pathlen - suffixlen, suffix);
} }
/// Modifies the given path by calling realpath. Returns true if realpath succeeded, false /// Modifies the given path by calling realpath. Returns true if realpath succeeded, false
@ -122,7 +122,7 @@ static struct config_paths_t determine_config_directory_paths(const char *argv0)
bool seems_installed = (suffix == installed_suffix); bool seems_installed = (suffix == installed_suffix);
wcstring base_path = str2wcstring(exec_path); wcstring base_path = str2wcstring(exec_path);
base_path.resize(base_path.size() - strlen(suffix)); base_path.resize(base_path.size() - std::strlen(suffix));
paths.data = base_path + (seems_installed ? L"/share/fish" : L"/share"); paths.data = base_path + (seems_installed ? L"/share/fish" : L"/share");
paths.sysconf = base_path + (seems_installed ? L"/etc/fish" : L"/etc"); paths.sysconf = base_path + (seems_installed ? L"/etc/fish" : L"/etc");

View File

@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <wctype.h> #include <wctype.h>
#include <cwchar> #include <cwchar>
@ -63,7 +63,7 @@ static wcstring read_file(FILE *f) {
// Illegal byte sequence. Try to skip past it. // Illegal byte sequence. Try to skip past it.
clearerr(f); clearerr(f);
int ch = fgetc(f); // for printing the warning, and seeks forward 1 byte. int ch = fgetc(f); // for printing the warning, and seeks forward 1 byte.
debug(1, "%s (byte=%X)", strerror(errno), ch); debug(1, "%s (byte=%X)", std::strerror(errno), ch);
ret = 1; ret = 1;
continue; continue;
} else { } else {
@ -518,7 +518,7 @@ int main(int argc, char *argv[]) {
fclose(fh); fclose(fh);
output_location = *argv; output_location = *argv;
} else { } else {
std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), *argv, strerror(errno)); std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), *argv, std::strerror(errno));
exit(1); exit(1);
} }
} else { } else {
@ -549,7 +549,7 @@ int main(int argc, char *argv[]) {
exit(0); exit(0);
} else { } else {
std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), output_location, std::fwprintf(stderr, _(L"Opening \"%s\" failed: %s\n"), output_location,
strerror(errno)); std::strerror(errno));
exit(1); exit(1);
} }
break; break;

View File

@ -14,7 +14,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -62,7 +62,7 @@ static bool should_exit(wchar_t wc) {
std::fwprintf(stderr, L"Press [ctrl-%c] again to exit\n", shell_modes.c_cc[VEOF] + 0x40); std::fwprintf(stderr, L"Press [ctrl-%c] again to exit\n", shell_modes.c_cc[VEOF] + 0x40);
return false; return false;
} }
return memcmp(recent_chars, "exit", 4) == 0 || memcmp(recent_chars, "quit", 4) == 0; return std::memcmp(recent_chars, "exit", 4) == 0 || std::memcmp(recent_chars, "quit", 4) == 0;
} }
/// Return the name if the recent sequence of characters matches a known terminfo sequence. /// Return the name if the recent sequence of characters matches a known terminfo sequence.
@ -265,7 +265,7 @@ static void install_our_signal_handlers() {
for (int signo = 1; signo < 32; signo++) { for (int signo = 1; signo < 32; signo++) {
if (sigaction(signo, &new_sa, &old_sa) != -1) { if (sigaction(signo, &new_sa, &old_sa) != -1) {
memcpy(&old_sigactions[signo], &old_sa, sizeof(old_sa)); std::memcpy(&old_sigactions[signo], &old_sa, sizeof(old_sa));
if (old_sa.sa_handler == SIG_IGN) { if (old_sa.sa_handler == SIG_IGN) {
debug(3, "signal #%d (%ls) was being ignored", signo, sig2wcs(signo)); debug(3, "signal #%d (%ls) was being ignored", signo, sig2wcs(signo));
} }

View File

@ -14,7 +14,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/select.h> #include <sys/select.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
@ -89,7 +89,7 @@ static bool should_test_function(const char *func_name) {
result = true; result = true;
} else { } else {
for (size_t i = 0; s_arguments[i] != NULL; i++) { for (size_t i = 0; s_arguments[i] != NULL; i++) {
if (!strncmp(func_name, s_arguments[i], strlen(s_arguments[i]))) { if (!std::strncmp(func_name, s_arguments[i], std::strlen(s_arguments[i]))) {
// Prefix match. // Prefix match.
result = true; result = true;
break; break;
@ -448,14 +448,14 @@ static void test_format() {
for (i = 0; i < sizeof tests / sizeof *tests; i++) { for (i = 0; i < sizeof tests / sizeof *tests; i++) {
char buff[128]; char buff[128];
format_size_safe(buff, tests[i].val); format_size_safe(buff, tests[i].val);
do_test(!strcmp(buff, tests[i].expected)); do_test(!std::strcmp(buff, tests[i].expected));
} }
for (int j = -129; j <= 129; j++) { for (int j = -129; j <= 129; j++) {
char buff1[128], buff2[128]; char buff1[128], buff2[128];
format_long_safe(buff1, j); format_long_safe(buff1, j);
sprintf(buff2, "%d", j); sprintf(buff2, "%d", j);
do_test(!strcmp(buff1, buff2)); do_test(!std::strcmp(buff1, buff2));
wchar_t wbuf1[128], wbuf2[128]; wchar_t wbuf1[128], wbuf2[128];
format_long_safe(wbuf1, j); format_long_safe(wbuf1, j);
@ -467,12 +467,12 @@ static void test_format() {
char buff1[128], buff2[128]; char buff1[128], buff2[128];
format_long_safe(buff1, q); format_long_safe(buff1, q);
sprintf(buff2, "%ld", q); sprintf(buff2, "%ld", q);
do_test(!strcmp(buff1, buff2)); do_test(!std::strcmp(buff1, buff2));
} }
/// Helper to convert a narrow string to a sequence of hex digits. /// Helper to convert a narrow string to a sequence of hex digits.
static char *str2hex(const char *input) { static char *str2hex(const char *input) {
char *output = (char *)malloc(5 * strlen(input) + 1); char *output = (char *)malloc(5 * std::strlen(input) + 1);
char *p = output; char *p = output;
for (; *input; input++) { for (; *input; input++) {
sprintf(p, "0x%02X ", (int)*input & 0xFF); sprintf(p, "0x%02X ", (int)*input & 0xFF);
@ -511,12 +511,12 @@ static void test_convert() {
L"wcs2str"); L"wcs2str");
} }
if (strcmp(o, n)) { if (std::strcmp(o, n)) {
char *o2 = str2hex(o); char *o2 = str2hex(o);
char *n2 = str2hex(n); char *n2 = str2hex(n);
err(L"Line %d - %d: Conversion cycle of string:\n%4d chars: %s\n" err(L"Line %d - %d: Conversion cycle of string:\n%4d chars: %s\n"
L"produced different string:\n%4d chars: %s", L"produced different string:\n%4d chars: %s",
__LINE__, i, strlen(o), o2, strlen(n), n2); __LINE__, i, std::strlen(o), o2, std::strlen(n), n2);
free(o2); free(o2);
free(n2); free(n2);
} }
@ -1258,7 +1258,7 @@ static void test_utf82wchar(const char *src, size_t slen, const wchar_t *dst, si
if (res != size) { if (res != size) {
err(L"u2w: %s: FAILED (rv: %lu, must be %lu)", descr, size, res); err(L"u2w: %s: FAILED (rv: %lu, must be %lu)", descr, size, res);
} else if (mem && memcmp(mem, dst, size * sizeof(*mem)) != 0) { } else if (mem && std::memcmp(mem, dst, size * sizeof(*mem)) != 0) {
err(L"u2w: %s: BROKEN", descr); err(L"u2w: %s: BROKEN", descr);
} }
@ -1303,7 +1303,7 @@ static void test_wchar2utf8(const wchar_t *src, size_t slen, const char *dst, si
size = wchar_to_utf8(src, slen, mem, dlen, flags); size = wchar_to_utf8(src, slen, mem, dlen, flags);
if (res != size) { if (res != size) {
err(L"w2u: %s: FAILED (rv: %lu, must be %lu)", descr, size, res); err(L"w2u: %s: FAILED (rv: %lu, must be %lu)", descr, size, res);
} else if (dst && memcmp(mem, dst, size) != 0) { } else if (dst && std::memcmp(mem, dst, size) != 0) {
err(L"w2u: %s: BROKEN", descr); err(L"w2u: %s: BROKEN", descr);
} }
@ -1459,7 +1459,7 @@ static void test_escape_sequences() {
err(L"test_escape_sequences failed on line %d\n", __LINE__); err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1B[2J") != 4) if (escape_code_length(L"\x1B[2J") != 4)
err(L"test_escape_sequences failed on line %d\n", __LINE__); err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1B[38;5;123mABC") != strlen("\x1B[38;5;123m")) if (escape_code_length(L"\x1B[38;5;123mABC") != std::strlen("\x1B[38;5;123m"))
err(L"test_escape_sequences failed on line %d\n", __LINE__); err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (escape_code_length(L"\x1B@") != 2) if (escape_code_length(L"\x1B@") != 2)
err(L"test_escape_sequences failed on line %d\n", __LINE__); err(L"test_escape_sequences failed on line %d\n", __LINE__);
@ -3230,7 +3230,7 @@ static void test_universal_ok_to_save() {
const char *contents = "# VERSION: 99999.99\n"; const char *contents = "# VERSION: 99999.99\n";
FILE *fp = wfopen(UVARS_TEST_PATH, "w"); FILE *fp = wfopen(UVARS_TEST_PATH, "w");
assert(fp && "Failed to open UVARS_TEST_PATH for writing"); assert(fp && "Failed to open UVARS_TEST_PATH for writing");
fwrite(contents, strlen(contents), 1, fp); fwrite(contents, std::strlen(contents), 1, fp);
fclose(fp); fclose(fp);
file_id_t before_id = file_id_for_path(UVARS_TEST_PATH); file_id_t before_id = file_id_for_path(UVARS_TEST_PATH);
@ -5176,7 +5176,7 @@ int main(int argc, char **argv) {
perror("getcwd"); perror("getcwd");
exit(-1); exit(-1);
} }
if (!strcmp(wd, "/")) { if (!std::strcmp(wd, "/")) {
std::fwprintf(stderr, std::fwprintf(stderr,
L"Unable to find 'tests' directory, which should contain file test.fish\n"); L"Unable to find 'tests' directory, which should contain file test.fish\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -8,7 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <cstdint> #include <cstdint>
// We need the sys/file.h for the flock() declaration on Linux but not OS X. // We need the sys/file.h for the flock() declaration on Linux but not OS X.
#include <sys/file.h> // IWYU pragma: keep #include <sys/file.h> // IWYU pragma: keep
@ -75,10 +75,10 @@ static constexpr int max_save_tries = 1024;
namespace { namespace {
static size_t safe_strlen(const char *s) { return s ? std::strlen(s) : 0; }
/// Helper class for certain output. This is basically a string that allows us to ensure we only /// Helper class for certain output. This is basically a string that allows us to ensure we only
/// flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to /// flush at record boundaries, and avoids the copying of ostringstream. Have you ever tried to
/// implement your own streambuf? Total insanity. /// implement your own streambuf? Total insanity.
static size_t safe_strlen(const char *s) { return s ? strlen(s) : 0; }
class history_output_buffer_t { class history_output_buffer_t {
std::vector<char> buffer; std::vector<char> buffer;
@ -271,7 +271,7 @@ class history_file_contents_t {
ptr += amt; ptr += amt;
} }
} }
memset(ptr, 0, remaining); std::memset(ptr, 0, remaining);
return true; return true;
} }
@ -350,7 +350,7 @@ static size_t read_line(const char *base, size_t cursor, size_t len, std::string
// Locate the newline. // Locate the newline.
assert(cursor <= len); assert(cursor <= len);
const char *start = base + cursor; const char *start = base + cursor;
const char *a_newline = (char *)memchr(start, '\n', len - cursor); const char *a_newline = (char *)std::memchr(start, '\n', len - cursor);
if (a_newline != NULL) { // we found a newline if (a_newline != NULL) { // we found a newline
result.assign(start, a_newline - start); result.assign(start, a_newline - start);
// Return the amount to advance the cursor; skip over the newline. // Return the amount to advance the cursor; skip over the newline.
@ -513,7 +513,7 @@ static history_item_t decode_item_fish_2_0(const char *base, size_t len) {
size_t advance = read_line(base, cursor, len, line); size_t advance = read_line(base, cursor, len, line);
if (trim_leading_spaces(line) <= indent) break; if (trim_leading_spaces(line) <= indent) break;
if (strncmp(line.c_str(), "- ", 2)) break; if (std::strncmp(line.c_str(), "- ", 2)) break;
// We're going to consume this line. // We're going to consume this line.
cursor += advance; cursor += advance;
@ -636,7 +636,7 @@ static bool parse_timestamp(const char *str, time_t *out_when) {
// Look for "when:". // Look for "when:".
size_t when_len = 5; size_t when_len = 5;
if (strncmp(cursor, "when:", when_len) != 0) return false; if (std::strncmp(cursor, "when:", when_len) != 0) return false;
cursor += when_len; cursor += when_len;
// Advance past spaces. // Advance past spaces.
@ -661,7 +661,7 @@ static const char *next_line(const char *start, size_t length) {
const char *const end = start + length; const char *const end = start + length;
// Skip past the next newline. // Skip past the next newline.
const char *nextline = (const char *)memchr(start, '\n', length); const char *nextline = (const char *)std::memchr(start, '\n', length);
if (!nextline || nextline >= end) { if (!nextline || nextline >= end) {
return NULL; return NULL;
} }
@ -671,7 +671,7 @@ static const char *next_line(const char *start, size_t length) {
} }
// Make sure this new line is itself "newline terminated". If it's not, return NULL. // Make sure this new line is itself "newline terminated". If it's not, return NULL.
const char *next_newline = (const char *)memchr(nextline, '\n', end - nextline); const char *next_newline = (const char *)std::memchr(nextline, '\n', end - nextline);
if (!next_newline) { if (!next_newline) {
return NULL; return NULL;
} }
@ -694,7 +694,7 @@ static size_t offset_of_next_item_fish_2_0(const history_file_contents_t &conten
const char *line_start = contents.address_at(cursor); const char *line_start = contents.address_at(cursor);
// Advance the cursor to the next line. // Advance the cursor to the next line.
const char *a_newline = (const char *)memchr(line_start, '\n', length - cursor); const char *a_newline = (const char *)std::memchr(line_start, '\n', length - cursor);
if (a_newline == NULL) break; if (a_newline == NULL) break;
// Advance the cursor past this line. +1 is for the newline. // Advance the cursor past this line. +1 is for the newline.
@ -707,26 +707,26 @@ static size_t offset_of_next_item_fish_2_0(const history_file_contents_t &conten
if (a_newline - line_start < 3) continue; if (a_newline - line_start < 3) continue;
// Try to be a little YAML compatible. Skip lines with leading %, ---, or ... // Try to be a little YAML compatible. Skip lines with leading %, ---, or ...
if (!memcmp(line_start, "%", 1) || !memcmp(line_start, "---", 3) || if (!std::memcmp(line_start, "%", 1) || !std::memcmp(line_start, "---", 3) ||
!memcmp(line_start, "...", 3)) !std::memcmp(line_start, "...", 3))
continue; continue;
// Hackish: fish 1.x rewriting a fish 2.0 history file can produce lines with lots of // Hackish: fish 1.x rewriting a fish 2.0 history file can produce lines with lots of
// leading "- cmd: - cmd: - cmd:". Trim all but one leading "- cmd:". // leading "- cmd: - cmd: - cmd:". Trim all but one leading "- cmd:".
const char *double_cmd = "- cmd: - cmd: "; const char *double_cmd = "- cmd: - cmd: ";
const size_t double_cmd_len = strlen(double_cmd); const size_t double_cmd_len = std::strlen(double_cmd);
while ((size_t)(a_newline - line_start) > double_cmd_len && while ((size_t)(a_newline - line_start) > double_cmd_len &&
!memcmp(line_start, double_cmd, double_cmd_len)) { !std::memcmp(line_start, double_cmd, double_cmd_len)) {
// Skip over just one of the - cmd. In the end there will be just one left. // Skip over just one of the - cmd. In the end there will be just one left.
line_start += strlen("- cmd: "); line_start += std::strlen("- cmd: ");
} }
// Hackish: fish 1.x rewriting a fish 2.0 history file can produce commands like "when: // Hackish: fish 1.x rewriting a fish 2.0 history file can produce commands like "when:
// 123456". Ignore those. // 123456". Ignore those.
const char *cmd_when = "- cmd: when:"; const char *cmd_when = "- cmd: when:";
const size_t cmd_when_len = strlen(cmd_when); const size_t cmd_when_len = std::strlen(cmd_when);
if ((size_t)(a_newline - line_start) >= cmd_when_len && if ((size_t)(a_newline - line_start) >= cmd_when_len &&
!memcmp(line_start, cmd_when, cmd_when_len)) { !std::memcmp(line_start, cmd_when, cmd_when_len)) {
continue; continue;
} }
@ -1141,7 +1141,7 @@ wcstring history_search_t::current_string() const {
} }
static void replace_all(std::string *str, const char *needle, const char *replacement) { static void replace_all(std::string *str, const char *needle, const char *replacement) {
size_t needle_len = strlen(needle), replacement_len = strlen(replacement); size_t needle_len = std::strlen(needle), replacement_len = std::strlen(replacement);
size_t offset = 0; size_t offset = 0;
while ((offset = str->find(needle, offset)) != std::string::npos) { while ((offset = str->find(needle, offset)) != std::string::npos) {
str->replace(offset, needle_len, replacement); str->replace(offset, needle_len, replacement);
@ -1820,7 +1820,7 @@ void history_t::populate_from_bash(FILE *stream) {
} }
// Deal with the newline if present. // Deal with the newline if present.
char *a_newline = strchr(buff, '\n'); char *a_newline = std::strchr(buff, '\n');
if (a_newline) *a_newline = '\0'; if (a_newline) *a_newline = '\0';
line.append(buff); line.append(buff);
if (a_newline) break; if (a_newline) break;

View File

@ -2,7 +2,7 @@
#include "config.h" #include "config.h"
#include <errno.h> #include <errno.h>
#include <string.h> #include <cstring>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
@ -205,7 +205,7 @@ wchar_t input_common_readch(int timed) {
switch (sz) { switch (sz) {
case (size_t)(-1): { case (size_t)(-1): {
memset(&state, '\0', sizeof(state)); std::memset(&state, '\0', sizeof(state));
debug(2, L"Illegal input"); debug(2, L"Illegal input");
return R_NULL; return R_NULL;
} }

View File

@ -4,7 +4,7 @@
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <cstring>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>

View File

@ -4,7 +4,7 @@
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <cstring>
#include <sys/select.h> #include <sys/select.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#if HAVE_CURSES_H #if HAVE_CURSES_H
#include <curses.h> #include <curses.h>
#elif HAVE_NCURSES_H #elif HAVE_NCURSES_H
@ -234,7 +234,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) {
if (c == c2) c = (c2 == rgb_color_t::white()) ? rgb_color_t::black() : rgb_color_t::white(); if (c == c2) c = (c2 == rgb_color_t::white()) ? rgb_color_t::black() : rgb_color_t::white();
} }
if ((enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0)) { if ((enter_bold_mode != 0) && (std::strlen(enter_bold_mode) > 0)) {
if (bg_set && !last_bg_set) { if (bg_set && !last_bg_set) {
// Background color changed and is set, so we enter bold mode to make reading easier. // Background color changed and is set, so we enter bold mode to make reading easier.
// This means bold mode is _always_ on when the background color is set. // This means bold mode is _always_ on when the background color is set.
@ -295,7 +295,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) {
} }
// Lastly, we set bold, underline, italics, dim, and reverse modes correctly. // Lastly, we set bold, underline, italics, dim, and reverse modes correctly.
if (is_bold && !was_bold && enter_bold_mode && strlen(enter_bold_mode) > 0 && !bg_set) { if (is_bold && !was_bold && enter_bold_mode && std::strlen(enter_bold_mode) > 0 && !bg_set) {
// The unconst cast is for NetBSD's benefit. DO NOT REMOVE! // The unconst cast is for NetBSD's benefit. DO NOT REMOVE!
writembs_nofail(*this, tparm((char *)enter_bold_mode)); writembs_nofail(*this, tparm((char *)enter_bold_mode));
was_bold = is_bold; was_bold = is_bold;
@ -310,27 +310,27 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) {
} }
was_underline = is_underline; was_underline = is_underline;
if (was_italics && !is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) { if (was_italics && !is_italics && enter_italics_mode && std::strlen(enter_italics_mode) > 0) {
writembs_nofail(*this, exit_italics_mode); writembs_nofail(*this, exit_italics_mode);
was_italics = is_italics; was_italics = is_italics;
} }
if (!was_italics && is_italics && enter_italics_mode && strlen(enter_italics_mode) > 0) { if (!was_italics && is_italics && enter_italics_mode && std::strlen(enter_italics_mode) > 0) {
writembs_nofail(*this, enter_italics_mode); writembs_nofail(*this, enter_italics_mode);
was_italics = is_italics; was_italics = is_italics;
} }
if (is_dim && !was_dim && enter_dim_mode && strlen(enter_dim_mode) > 0) { if (is_dim && !was_dim && enter_dim_mode && std::strlen(enter_dim_mode) > 0) {
writembs_nofail(*this, enter_dim_mode); writembs_nofail(*this, enter_dim_mode);
was_dim = is_dim; was_dim = is_dim;
} }
if (is_reverse && !was_reverse) { if (is_reverse && !was_reverse) {
// Some terms do not have a reverse mode set, so standout mode is a fallback. // Some terms do not have a reverse mode set, so standout mode is a fallback.
if (enter_reverse_mode && strlen(enter_reverse_mode) > 0) { if (enter_reverse_mode && std::strlen(enter_reverse_mode) > 0) {
writembs_nofail(*this, enter_reverse_mode); writembs_nofail(*this, enter_reverse_mode);
was_reverse = is_reverse; was_reverse = is_reverse;
} else if (enter_standout_mode && strlen(enter_standout_mode) > 0) { } else if (enter_standout_mode && std::strlen(enter_standout_mode) > 0) {
writembs_nofail(*this, enter_standout_mode); writembs_nofail(*this, enter_standout_mode);
was_reverse = is_reverse; was_reverse = is_reverse;
} }

View File

@ -69,7 +69,7 @@ class outputter_t {
} }
/// Write a narrow NUL-terminated string. /// Write a narrow NUL-terminated string.
void writestr(const char *str) { writestr(str, strlen(str)); } void writestr(const char *str) { writestr(str, std::strlen(str)); }
/// Write a narrow character. /// Write a narrow character.
void push_back(char c) { void push_back(char c) {

View File

@ -4,7 +4,7 @@
#include "config.h" // IWYU pragma: keep #include "config.h" // IWYU pragma: keep
#include <errno.h> #include <errno.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -272,7 +272,7 @@ static void maybe_issue_path_warning(const wcstring &which_dir, const wcstring &
const wchar_t *env_var = using_xdg ? xdg_var.c_str() : L"HOME"; const wchar_t *env_var = using_xdg ? xdg_var.c_str() : L"HOME";
debug(0, _(L"Unable to locate %ls directory derived from $%ls: '%ls'."), which_dir.c_str(), debug(0, _(L"Unable to locate %ls directory derived from $%ls: '%ls'."), which_dir.c_str(),
env_var, path.c_str()); env_var, path.c_str());
debug(0, _(L"The error was '%s'."), strerror(saved_errno)); debug(0, _(L"The error was '%s'."), std::strerror(saved_errno));
debug(0, _(L"Please set $%ls to a directory where you have write access."), env_var); debug(0, _(L"Please set $%ls to a directory where you have write access."), env_var);
} }
ignore_result(write(STDERR_FILENO, "\n", 1)); ignore_result(write(STDERR_FILENO, "\n", 1));

View File

@ -5,7 +5,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <cstring>
#include <time.h> #include <time.h>
#include <memory> #include <memory>
#if FISH_USE_POSIX_SPAWN #if FISH_USE_POSIX_SPAWN
@ -308,11 +308,11 @@ void safe_report_exec_error(int err, const char *actual_cmd, const char *const *
size_t sz = 0; size_t sz = 0;
const char *const *p; const char *const *p;
for (p = argv; *p; p++) { for (p = argv; *p; p++) {
sz += strlen(*p) + 1; sz += std::strlen(*p) + 1;
} }
for (p = envv; *p; p++) { for (p = envv; *p; p++) {
sz += strlen(*p) + 1; sz += std::strlen(*p) + 1;
} }
format_size_safe(sz1, sz); format_size_safe(sz1, sz);

View File

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include "common.h" #include "common.h"
#include "print_help.h" #include "print_help.h"
@ -17,6 +17,6 @@ void print_help(const char *c, int fd) {
int printed = snprintf(cmd, CMD_LEN, "fish -c '__fish_print_help %s >&%d'", c, fd); int printed = snprintf(cmd, CMD_LEN, "fish -c '__fish_print_help %s >&%d'", c, fd);
if (printed < CMD_LEN && system(cmd) == -1) { if (printed < CMD_LEN && system(cmd) == -1) {
write_loop(2, HELP_ERR, strlen(HELP_ERR)); write_loop(2, HELP_ERR, std::strlen(HELP_ERR));
} }
} }

View File

@ -23,7 +23,7 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
#endif #endif
@ -969,13 +969,13 @@ void reader_init() {
tcgetattr(STDIN_FILENO, &terminal_mode_on_startup); tcgetattr(STDIN_FILENO, &terminal_mode_on_startup);
// Set the mode used for program execution, initialized to the current mode. // Set the mode used for program execution, initialized to the current mode.
memcpy(&tty_modes_for_external_cmds, &terminal_mode_on_startup, std::memcpy(&tty_modes_for_external_cmds, &terminal_mode_on_startup,
sizeof tty_modes_for_external_cmds); sizeof tty_modes_for_external_cmds);
tty_modes_for_external_cmds.c_iflag &= ~IXON; // disable flow control tty_modes_for_external_cmds.c_iflag &= ~IXON; // disable flow control
tty_modes_for_external_cmds.c_iflag &= ~IXOFF; // disable flow control tty_modes_for_external_cmds.c_iflag &= ~IXOFF; // disable flow control
// Set the mode used for the terminal, initialized to the current mode. // Set the mode used for the terminal, initialized to the current mode.
memcpy(&shell_modes, &terminal_mode_on_startup, sizeof shell_modes); std::memcpy(&shell_modes, &terminal_mode_on_startup, sizeof shell_modes);
shell_modes.c_iflag &= ~ICRNL; // disable mapping CR (\cM) to NL (\cJ) shell_modes.c_iflag &= ~ICRNL; // disable mapping CR (\cM) to NL (\cJ)
shell_modes.c_iflag &= ~INLCR; // disable mapping NL (\cJ) to CR (\cM) shell_modes.c_iflag &= ~INLCR; // disable mapping NL (\cJ) to CR (\cM)
@ -2447,7 +2447,7 @@ maybe_t<wcstring> reader_data_t::readline(int nchars) {
(size_t)READAHEAD_MAX) (size_t)READAHEAD_MAX)
: READAHEAD_MAX; : READAHEAD_MAX;
memset(arr, 0, sizeof(arr)); std::memset(arr, 0, sizeof(arr));
arr[0] = c; arr[0] = c;
for (i = 1; i < limit; ++i) { for (i = 1; i < limit; ++i) {

View File

@ -10,7 +10,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <cwchar> #include <cwchar>
@ -460,7 +460,7 @@ static void s_move(screen_t *s, int new_x, int new_y) {
y_steps = new_y - s->actual.cursor.y; y_steps = new_y - s->actual.cursor.y;
if (y_steps > 0 && (strcmp(cursor_down, "\n") == 0)) { if (y_steps > 0 && (std::strcmp(cursor_down, "\n") == 0)) {
// This is very strange - it seems some (all?) consoles use a simple newline as the cursor // This is very strange - it seems some (all?) consoles use a simple newline as the cursor
// down escape. This will of course move the cursor to the beginning of the line as well as // down escape. This will of course move the cursor to the beginning of the line as well as
// moving it down one step. The cursor_up does not have this behaviour... // moving it down one step. The cursor_up does not have this behaviour...
@ -496,7 +496,7 @@ static void s_move(screen_t *s, int new_x, int new_y) {
// Use the bulk ('multi') output for cursor movement if it is supported and it would be shorter // Use the bulk ('multi') output for cursor movement if it is supported and it would be shorter
// Note that this is required to avoid some visual glitches in iTerm (issue #1448). // Note that this is required to avoid some visual glitches in iTerm (issue #1448).
bool use_multi = bool use_multi =
multi_str != NULL && multi_str[0] != '\0' && abs(x_steps) * strlen(str) > strlen(multi_str); multi_str != NULL && multi_str[0] != '\0' && abs(x_steps) * std::strlen(str) > std::strlen(multi_str);
if (use_multi && cur_term) { if (use_multi && cur_term) {
char *multi_param = tparm((char *)multi_str, abs(x_steps)); char *multi_param = tparm((char *)multi_str, abs(x_steps));
writembs(outp, multi_param); writembs(outp, multi_param);

View File

@ -26,7 +26,7 @@
#include "tinyexpr.h" #include "tinyexpr.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h> #include <cstring>
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
@ -96,9 +96,9 @@ static te_expr *new_expr(const int type, const te_expr *parameters[]) {
te_expr *ret = (te_expr *)malloc(size); te_expr *ret = (te_expr *)malloc(size);
// This sets float to 0, which depends on the implementation. // This sets float to 0, which depends on the implementation.
// We rely on IEEE-754 floats anyway, so it's okay. // We rely on IEEE-754 floats anyway, so it's okay.
memset(ret, 0, size); std::memset(ret, 0, size);
if (arity && parameters) { if (arity && parameters) {
memcpy(ret->parameters, parameters, psize); std::memcpy(ret->parameters, parameters, psize);
} }
ret->type = type; ret->type = type;
return ret; return ret;
@ -192,10 +192,10 @@ static const te_builtin *find_builtin(const char *name, int len) {
const te_builtin *found = std::lower_bound(std::begin(functions), end, name, const te_builtin *found = std::lower_bound(std::begin(functions), end, name,
[len](const te_builtin &lhs, const char *rhs) { [len](const te_builtin &lhs, const char *rhs) {
// The length is important because that's where the parens start // The length is important because that's where the parens start
return strncmp(lhs.name, rhs, len) < 0; return std::strncmp(lhs.name, rhs, len) < 0;
}); });
// We need to compare again because we might have gotten the first "larger" element. // We need to compare again because we might have gotten the first "larger" element.
if (found != end && strncmp(found->name, name, len) == 0) return found; if (found != end && std::strncmp(found->name, name, len) == 0) return found;
return NULL; return NULL;
} }

View File

@ -40,7 +40,7 @@
#include <stdio.h> #include <stdio.h>
#include <cwchar> #include <cwchar>
#include <string.h> #include <cstring>
// This version of `getopt' appears to the caller like standard Unix `getopt' but it behaves // This version of `getopt' appears to the caller like standard Unix `getopt' but it behaves
// differently for the user, since it allows the user to intersperse the options with the other // differently for the user, since it allows the user to intersperse the options with the other

View File

@ -10,7 +10,7 @@
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
#if defined(__linux__) #if defined(__linux__)
#include <sys/statfs.h> #include <sys/statfs.h>
@ -130,7 +130,7 @@ const wcstring wgetcwd() {
return str2wcstring(res); return str2wcstring(res);
} }
debug(0, _(L"getcwd() failed with errno %d/%s"), errno, strerror(errno)); debug(0, _(L"getcwd() failed with errno %d/%s"), errno, std::strerror(errno));
return wcstring(); return wcstring();
} }
@ -262,7 +262,7 @@ void wperror(const wchar_t *s) {
if (s[0] != L'\0') { if (s[0] != L'\0') {
std::fwprintf(stderr, L"%ls: ", s); std::fwprintf(stderr, L"%ls: ", s);
} }
std::fwprintf(stderr, L"%s\n", strerror(e)); std::fwprintf(stderr, L"%s\n", std::strerror(e));
} }
int make_fd_nonblocking(int fd) { int make_fd_nonblocking(int fd) {
@ -318,7 +318,7 @@ int fd_check_is_remote(int fd) {
} }
static inline void safe_append(char *buffer, const char *s, size_t buffsize) { static inline void safe_append(char *buffer, const char *s, size_t buffsize) {
strncat(buffer, s, buffsize - strlen(buffer) - 1); std::strncat(buffer, s, buffsize - std::strlen(buffer) - 1);
} }
// In general, strerror is not async-safe, and therefore we cannot use it directly. So instead we // In general, strerror is not async-safe, and therefore we cannot use it directly. So instead we
@ -328,7 +328,7 @@ const char *safe_strerror(int err) {
#if defined(__UCLIBC__) #if defined(__UCLIBC__)
// uClibc does not have sys_errlist, however, its strerror is believed to be async-safe. // uClibc does not have sys_errlist, however, its strerror is believed to be async-safe.
// See issue #808. // See issue #808.
return strerror(err); return std::strerror(err);
#elif defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST) #elif defined(HAVE__SYS__ERRS) || defined(HAVE_SYS_ERRLIST)
#ifdef HAVE_SYS_ERRLIST #ifdef HAVE_SYS_ERRLIST
if (err >= 0 && err < sys_nerr && sys_errlist[err] != NULL) { if (err >= 0 && err < sys_nerr && sys_errlist[err] != NULL) {
@ -373,7 +373,7 @@ void safe_perror(const char *message) {
safe_append(buff, safe_strerror(err), sizeof buff); safe_append(buff, safe_strerror(err), sizeof buff);
safe_append(buff, "\n", sizeof buff); safe_append(buff, "\n", sizeof buff);
ignore_result(write(STDERR_FILENO, buff, strlen(buff))); ignore_result(write(STDERR_FILENO, buff, std::strlen(buff)));
errno = err; errno = err;
} }

View File

@ -59,7 +59,7 @@ void wperror(const wchar_t *s);
/// Async-safe version of perror(). /// Async-safe version of perror().
void safe_perror(const char *message); void safe_perror(const char *message);
/// Async-safe version of strerror(). /// Async-safe version of std::strerror().
const char *safe_strerror(int err); const char *safe_strerror(int err);
/// Wide character version of getcwd(). /// Wide character version of getcwd().