Add erasef(), formatted version of erase()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-03-27 07:08:49 +01:00
parent 098a020843
commit d4b554ccdc
5 changed files with 85 additions and 3 deletions
+4
View File
@@ -94,6 +94,10 @@ Helper Macros
Erase file/directory with `remove()`. Errors on `stderr`
- `erasef(fmt, ...)`
Like `erase()` but takes a formatted printf-like string as argument.
- `makedir(path)`
Create directory, like `mkdir()`. Errors on `stderr`
+2 -2
View File
@@ -4,8 +4,8 @@ libite_la_CPPFLAGS = -D_GNU_SOURCE
libite_la_CFLAGS = -W -Wall -Wextra
libite_la_LDFLAGS = $(AM_LDFLAGS) -version-info 7:0:2
libite_la_SOURCES = chomp.c copyfile.c \
conio.c conio.h \
dir.c fopenf.c fremove.c \
conio.c conio.h dir.c \
erasef.c fopenf.c fremove.c \
fexist.c fisdir.c \
fparseln.c fsendfile.c \
ifconfig.c lfile.c \
+66
View File
@@ -0,0 +1,66 @@
/* Formatted erase()
*
* Copyright (c) 2021 Joachim Wiberg <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include "lite.h"
/**
* erasef - Like erase() but with formatted string support
* @fmt: Formatted string to be composed into a pathname
*
* This is a wrapper for the erase() function in lite.h, lessening the
* burden of having to compose the filename from parts in a seprate
* buffer.
*
* Returns:
* Upon successful completion erasef() returns POSIX OK(0), otherwise,
* -1 is returned and errno is set to indicate the error.
*/
int erasef(const char *fmt, ...)
{
va_list ap;
char *file;
int len;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
file = alloca(len + 1);
if (!file) {
errno = ENOMEM;
return -1;
}
va_start(ap, fmt);
vsnprintf(file, len + 1, fmt, ap);
va_end(ap);
return erase(file);
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+3
View File
@@ -60,6 +60,9 @@ typedef struct lfile lfile_t;
typedef struct sdbuf sdbuf_t;
char *chomp (char *str);
int erasef (const char *fmt, ...);
char *fparseln (FILE *, size_t *, size_t *, const char[3], int);
int fexist (const char *file);
+10 -1
View File
@@ -1,3 +1,4 @@
/* test of touch(), touchf(), erase(), and erasef() */
#include <err.h>
#include <paths.h>
#include <signal.h>
@@ -53,7 +54,15 @@ static int formatted(char *file)
errx(1, "touchf() does not detect failure to create file " FMT, file);
PRINT("File " FMT " really does exist!\n", file);
erase(vrfy);
erasef(FMT, file);
if (fexist(vrfy)) {
int saved = errno;
erase(vrfy);
errno = saved;
errx(1, "erasef() failed removing the created file " FMT, file);
}
return 0;
}