From 17c16a4bf597d4bc7cf3ddc8ffc9db807bf7dc50 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Mon, 14 Oct 2019 19:51:24 +0200 Subject: [PATCH] tweaks: rename a function and elide its first parameter --- src/chars.c | 2 +- src/files.c | 2 +- src/nano.c | 2 +- src/proto.h | 2 +- src/text.c | 10 +++++----- src/utils.c | 14 ++++++++++---- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/chars.c b/src/chars.c index 966a8d7f..d7343d92 100644 --- a/src/chars.c +++ b/src/chars.c @@ -244,7 +244,7 @@ char *make_mbchar(long code, int *length) } else #endif { - mb_char = mallocstrncpy(NULL, (char *)&code, 1); + mb_char = measured_copy((char *)&code, 1); *length = 1; } diff --git a/src/files.c b/src/files.c index 78c82253..5a0ee0f3 100644 --- a/src/files.c +++ b/src/files.c @@ -2243,7 +2243,7 @@ char *real_dir_from_tilde(const char *path) #ifdef HAVE_PWD_H const struct passwd *userdata; - tilded = mallocstrncpy(NULL, path, i + 1); + tilded = measured_copy(path, i + 1); tilded[i] = '\0'; do { diff --git a/src/nano.c b/src/nano.c index 34d91059..57f0e677 100644 --- a/src/nano.c +++ b/src/nano.c @@ -226,7 +226,7 @@ void partition_buffer(linestruct *top, size_t top_x, * top of the partition from it, and save the text before top_x. */ foreline = top->prev; top->prev = NULL; - antedata = mallocstrncpy(NULL, top->data, top_x + 1); + antedata = measured_copy(top->data, top_x + 1); antedata[top_x] = '\0'; /* Remember which line is below the bottom of the partition, detach the diff --git a/src/proto.h b/src/proto.h index 260119ce..f7b6b098 100644 --- a/src/proto.h +++ b/src/proto.h @@ -578,7 +578,7 @@ const char *strstrwrapper(const char *haystack, const char *needle, const char *start); void *nmalloc(size_t howmuch); void *nrealloc(void *ptr, size_t howmuch); -char *mallocstrncpy(char *dest, const char *src, size_t n); +char *measured_copy(const char *src, size_t n); char *mallocstrcpy(char *dest, const char *src); char *copy_of(const char *string); char *free_and_assign(char *dest, char *src); diff --git a/src/text.c b/src/text.c index bc0171ea..ef4ef875 100644 --- a/src/text.c +++ b/src/text.c @@ -270,7 +270,7 @@ void do_unindent(void) * possible, and saving the removed whitespace in the undo item. */ for (line = top; line != bot->next; line = line->next) { size_t indent_len = length_of_white(line->data); - char *indentation = mallocstrncpy(NULL, line->data, indent_len + 1); + char *indentation = measured_copy(line->data, indent_len + 1); indentation[indent_len] = '\0'; @@ -587,7 +587,7 @@ void do_undo(void) } t = make_new_node(f); t->data = copy_of(u->strdata); - data = mallocstrncpy(NULL, f->data, u->mark_begin_x + 1); + data = measured_copy(f->data, u->mark_begin_x + 1); data[u->mark_begin_x] = '\0'; free(f->data); f->data = data; @@ -728,7 +728,7 @@ void do_redo(void) redidmsg = _("line break"); shoveline = make_new_node(f); shoveline->data = copy_of(u->strdata); - data = mallocstrncpy(NULL, f->data, u->begin + 1); + data = measured_copy(f->data, u->begin + 1); data[u->begin] = '\0'; free(f->data); f->data = data; @@ -1902,7 +1902,7 @@ void justify_paragraph(linestruct **line, size_t par_len) /* Copy the leading part (quoting + indentation) of the sample line. */ quote_len = quote_length(sampleline->data); lead_len = quote_len + indent_length(sampleline->data + quote_len); - lead_string = mallocstrncpy(NULL, sampleline->data, lead_len + 1); + lead_string = measured_copy(sampleline->data, lead_len + 1); lead_string[lead_len] = '\0'; /* Concatenate all lines of the paragraph into a single line. */ @@ -1984,7 +1984,7 @@ void do_justify(bool full_justify) /* Copy the leading part that is to be used for the new paragraph. */ quote_len = quote_length(first_par_line->data); lead_len = quote_len + indent_length(first_par_line->data + quote_len); - the_lead = mallocstrncpy(NULL, first_par_line->data, lead_len + 1); + the_lead = measured_copy(first_par_line->data, lead_len + 1); the_lead[lead_len] = '\0'; /* Copy the leading part that is to be used for the new paragraph after diff --git a/src/utils.c b/src/utils.c index 44cacb1d..f3397f1f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -314,9 +314,10 @@ void *nrealloc(void *ptr, size_t howmuch) /* Allocate and copy the first n characters of the given src string, after * freeing the destination. Usage: "dest = mallocstrncpy(dest, src, n);". */ -char *mallocstrncpy(char *dest, const char *src, size_t n) +char *measured_copy(const char *src, size_t n) { - dest = charealloc(dest, n); + char *dest = charalloc(n); + strncpy(dest, src, n); return dest; @@ -326,13 +327,18 @@ char *mallocstrncpy(char *dest, const char *src, size_t n) * "dest = mallocstrcpy(dest, src);". */ char *mallocstrcpy(char *dest, const char *src) { - return mallocstrncpy(dest, src, strlen(src) + 1); + size_t count = strlen(src) + 1; + + dest = charealloc(dest, count); + strncpy(dest, src, count); + + return dest; } /* Return an allocated copy of the given string. */ char *copy_of(const char *string) { - return mallocstrncpy(NULL, string, strlen(string) + 1); + return measured_copy(string, strlen(string) + 1); } /* Free the string at dest and return the string at src. */