If ch == '\0', do not grow the string or advance the length counter,

but do allocate a string if there is none to begin with.  This makes
it possible to use openpam_straddch(3) to preallocate the string (if
necessary) instead of manually calling malloc(3) or calloc(3) and
initializing size and len.


git-svn-id: svn+ssh://svn.openpam.org/svn/openpam/trunk@634 185d5e19-27fe-0310-9dcf-9bff6b9f3609
This commit is contained in:
Dag-Erling Smørgrav 2013-03-03 23:23:10 +00:00
parent b21442245a
commit 54d9167cea
1 changed files with 11 additions and 4 deletions

View File

@ -65,7 +65,7 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch)
*str = tmpstr;
*size = tmpsize;
*len = 0;
} else if (*len + 1 >= *size) {
} else if (ch != 0 && *len + 1 >= *size) {
/* additional space required */
tmpsize = *size * 2;
if ((tmpstr = realloc(*str, tmpsize)) == NULL) {
@ -76,8 +76,10 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch)
*size = tmpsize;
*str = tmpstr;
}
(*str)[*len] = ch;
++*len;
if (ch != 0) {
(*str)[*len] = ch;
++*len;
}
(*str)[*len] = '\0';
return (0);
}
@ -94,6 +96,11 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch)
* The =size and =len argument point to variables used to hold the size
* of the buffer and the length of the string it contains, respectively.
*
* The final argument, =ch, is the character that should be appended to
* the string. If =ch is 0, nothing is appended, but a new buffer is
* still allocated if =str is NULL. This can be used to "bootstrap" the
* string.
*
* If a new buffer is allocated or an existing buffer is reallocated to
* make room for the additional character, =str and =size are updated
* accordingly.
@ -102,7 +109,7 @@ openpam_straddch(char **str, size_t *size, size_t *len, int ch)
* NUL-terminated.
*
* If the =openpam_straddch function is successful, it increments the
* integer variable pointed to by =len and returns 0.
* integer variable pointed to by =len (unless =ch was 0) and returns 0.
* Otherwise, it leaves the variables pointed to by =str, =size and =len
* unmodified, sets :errno to =ENOMEM and returns -1.
*