#include <stdarg.h>
#include <stddef.h>
-#include <stdio.h>
#include "attr.h"
+#include "string/sprintf/snprintf.h"
#if !defined(HAVE_STPEPRINTF)
*
* RETURN VALUE
* dst + strlen(dst)
- * • On success, these functions return a pointer to the
- * terminating NUL byte.
+ * On success, these functions return a pointer to the
+ * terminating NUL byte.
*
- * end
- * • If this call truncated the resulting string.
- * • If `dst == end` (a previous chained call to these
- * functions truncated).
- * NULL
- * • If this function failed (see ERRORS).
- * • If `dst == NULL` (a previous chained call to these
- * functions failed).
+ * NULL On error.
*
* ERRORS
+ * E2BIG The string was truncated.
+ *
* These functions may fail for the same reasons as vsnprintf(3).
+ *
+ * If dst is NULL at input, this function doesn't clobber errno.
*/
int len;
ptrdiff_t size;
- if (dst == end)
- return end;
if (dst == NULL)
return NULL;
size = end - dst;
- len = vsnprintf(dst, size, fmt, ap);
-
+ len = vsnprintf_(dst, size, fmt, ap);
if (len == -1)
return NULL;
- if (len >= size)
- return end;
return dst + len;
}
#include "config.h"
+#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
*
* RETURN VALUE
* dst + strlen(dst)
- * • On success, this function returns a pointer to the
- * terminating NUL byte.
+ * On success, this function returns a pointer to the
+ * terminating NUL byte.
*
- * end
- * • If this call truncated the resulting string.
- * • If `dst == end` (a previous chained call to these
- * functions truncated).
- * NULL
- * • If `dst == NULL` (a previous chained call to
- * [v]stpeprintf() failed).
+ * NULL On error.
*
* ERRORS
- * This function doesn't set errno.
+ * E2BIG The string was truncated.
+ *
+ * If dst is NULL at input, this function doesn't clobber errno.
*/
stpecpy(char *dst, char *end, const char *restrict src)
{
bool trunc;
+ char *p;
size_t dsize, dlen, slen;
- if (dst == end)
- return end;
if (dst == NULL)
return NULL;
trunc = (slen == dsize);
dlen = slen - trunc;
- return stpcpy(mempcpy(dst, src, dlen), "") + trunc;
+ p = stpcpy(mempcpy(dst, src, dlen), "");
+ if (trunc) {
+ errno = E2BIG;
+ return NULL;
+ }
+
+ return p;
}
#endif