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