mirror of
https://github.com/troglobit/libite.git
synced 2026-10-04 14:34:12 +07:00
Simplify va_start/_end handling and return ENOMEM if alloca() fails
Be consistent with errno codes from libite, we already have other APIs that return ENOMEM if alloca() fails, so let's do the same for these new ones as well. Also, simplify va_start()/va_end() handling in these APIs, no point in the extra complexity before this patch. Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
+8
-6
@@ -15,6 +15,7 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
@@ -34,7 +35,6 @@
|
||||
FILE *fopenf(const char *mode, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
FILE *fp = NULL;
|
||||
char *file;
|
||||
int len;
|
||||
|
||||
@@ -42,15 +42,17 @@ FILE *fopenf(const char *mode, const char *fmt, ...)
|
||||
len = vsnprintf(NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
va_start(ap, fmt);
|
||||
file = alloca(len + 1);
|
||||
if (file) {
|
||||
vsnprintf(file, len + 1, fmt, ap);
|
||||
fp = fopen(file, mode);
|
||||
if (!file) {
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(file, len + 1, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return fp;
|
||||
return fopen(file, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
-1
@@ -70,8 +70,10 @@ int fmkpath(mode_t mode, const char *fmt, ...)
|
||||
va_end(ap);
|
||||
|
||||
path = alloca(len + 1);
|
||||
if (!path)
|
||||
if (!path) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = vsnprintf(path, len + 1, fmt, ap);
|
||||
|
||||
+1
-2
@@ -32,14 +32,13 @@ int systemf(const char *fmt, ...)
|
||||
len = vsnprintf(NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
va_start(ap, fmt);
|
||||
cmd = alloca(++len);
|
||||
if (!cmd) {
|
||||
va_end(ap);
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(cmd, len, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user