add new function mallocstrncpy()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2045 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
David Lawrence Ramsey 2004-11-02 15:18:30 +00:00
parent 1e1f572c51
commit 4a1fc558cf
4 changed files with 20 additions and 7 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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,

View File

@ -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));". */