mirror of
https://github.com/troglobit/libite.git
synced 2026-10-02 13:52:51 +07:00
Add support for truncatef(), formatted truncate() replacement
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
@@ -335,6 +335,13 @@ Generic Functions
|
||||
**Note:** Requires Linux v3.11, or later, will fall back to the old
|
||||
and unsafe `tmpfile()` on older systems.
|
||||
|
||||
- `truncatef(length, fmt, ...)`
|
||||
|
||||
Like `truncate()`, but takes a formatted string as argument. This
|
||||
simplifies some operations when the filename otherwise have to be
|
||||
composed from parts into a separate array before calling the real
|
||||
function.
|
||||
|
||||
- `which(cmd)`, `whichp(cmd)`
|
||||
|
||||
C implementation of UNIX which(1). Returns a malloc'ed string with the
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ libite_la_SOURCES = chomp.c copyfile.c \
|
||||
strlcpy.c strlcat.c strtonum.c \
|
||||
strdupa.h strndupa.h strnlen.h \
|
||||
strmatch.c systemf.c \
|
||||
telnet.c tempfile.c \
|
||||
telnet.c tempfile.c truncatef.c \
|
||||
yorn.c which.c \
|
||||
lite.h strlite.h \
|
||||
queue.h tree.h
|
||||
|
||||
@@ -72,6 +72,7 @@ ssize_t copyfile (const char *src, const char *dst, int len, int opt);
|
||||
int movefile (const char *src, const char *dst);
|
||||
int fcopyfile (FILE *src, FILE *dst);
|
||||
ssize_t fsendfile (FILE *src, FILE *dst, size_t len);
|
||||
int truncatef (off_t length, const char *fmt, ...);
|
||||
|
||||
int ifconfig (const char *ifname, const char *addr, const char *mask, int up);
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/* Formatted truncate()
|
||||
*
|
||||
* Copyright (c) 2020 Joachim Nilsson <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>
|
||||
|
||||
/**
|
||||
* truncatef - Truncate a file based on the formatted string
|
||||
* @length: Size in bytes to truncate file to, zero to empty it
|
||||
* @fmt: Formatted string to be composed into a pathname
|
||||
*
|
||||
* This is an extension to the truncate() family, lessening the burden
|
||||
* of having to compose the filename from parts in a seprate buffer.
|
||||
*
|
||||
* Returns:
|
||||
|
||||
* Upon successful completion truncate() returns POSIX OK(0), otherwise,
|
||||
* -1 is returned and errno is set to indicate the error.
|
||||
*/
|
||||
int truncatef(off_t length, 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 truncate(file, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
Reference in New Issue
Block a user