From: David Lawrence Ramsey Date: Tue, 2 Nov 2004 15:18:30 +0000 (+0000) Subject: add new function mallocstrncpy() X-Git-Tag: v1.3.5~75 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=4a1fc558cf27254fdbc3a5556072e8a00d7a0b19;p=nano.git add new function mallocstrncpy() git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2045 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index a95aec2e..69edceba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -238,6 +238,11 @@ CVS code - regexp_bol_or_eol() - Don't assume any longer that string will be found if REG_NOTBOL and REG_NOTEOL are not set. (DLR) + mallocstrncpy() + - New function, used as a malloc()ing equivalent of strncpy(). + (DLR) + mallocstrcpy() + - Refactor to be a wrapper for mallocstrncpy(). (DLR) - winio.c: unget_kbinput() - New function used as a wrapper for ungetch(). (DLR) diff --git a/src/proto.h b/src/proto.h index bf8a7e19..c0a2e16a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -490,6 +490,7 @@ const char *strstrwrapper(const char *haystack, const char *needle, void nperror(const char *s); void *nmalloc(size_t howmuch); void *nrealloc(void *ptr, size_t howmuch); +char *mallocstrncpy(char *dest, const char *src, size_t n); char *mallocstrcpy(char *dest, const char *src); char *mallocstrassn(char *dest, char *src); void new_magicline(void); diff --git a/src/search.c b/src/search.c index 7cea9f2b..185e9aa0 100644 --- a/src/search.c +++ b/src/search.c @@ -315,8 +315,7 @@ bool findnextstr(bool can_display_wrap, bool wholeword, bool /* If we're searching for whole words, see if this potential * match is a whole word. */ if (wholeword) { - char *word = charalloc(found_len + 1); - strncpy(word, found, found_len); + char *word = mallocstrncpy(word, found, found_len + 1); word[found_len] = '\0'; found_whole = is_whole_word(found - fileptr->data, diff --git a/src/utils.c b/src/utils.c index bee1c6cf..b113cb95 100644 --- a/src/utils.c +++ b/src/utils.c @@ -394,9 +394,10 @@ void *nrealloc(void *ptr, size_t howmuch) return r; } -/* Copy one malloc()ed string to another pointer. Should be used as: - * "dest = mallocstrcpy(dest, src);". */ -char *mallocstrcpy(char *dest, const char *src) +/* Copy the first n characters of one malloc()ed string to another + * pointer. Should be used as: "dest = mallocstrncpy(dest, src, + * n);". */ +char *mallocstrncpy(char *dest, const char *src, size_t n) { if (src == NULL) src = ""; @@ -404,12 +405,19 @@ char *mallocstrcpy(char *dest, const char *src) if (src != dest) free(dest); - dest = charalloc(strlen(src) + 1); - strcpy(dest, src); + dest = charalloc(n); + strncpy(dest, src, n); return dest; } +/* Copy one malloc()ed string to another pointer. Should be used as: + * "dest = mallocstrcpy(dest, src);". */ +char *mallocstrcpy(char *dest, const char *src) +{ + return mallocstrncpy(dest, src, src == NULL ? 1 : strlen(src) + 1); +} + /* Free the malloc()ed string at dest and return the malloc()ed string * at src. Should be used as: "answer = mallocstrassn(answer, * real_dir_from_tilde(answer));". */