logo

utils-std

Collection of commonly available Unix tools
commit: 1196a7778a82234d8d6b3e4bc994ec50fd9002d5
parent e70c520358088dc14fce426e9d69606f47e4657d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 23 Apr 2024 18:30:18 +0200

cmd/truncate,lib/truncation: clang-format

Diffstat:

Mcmd/truncate.c17++++++++++-------
Mlib/truncation.c16+++++++++++-----
2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/cmd/truncate.c b/cmd/truncate.c @@ -11,11 +11,11 @@ #include <errno.h> #include <fcntl.h> // open #include <stdbool.h> -#include <stdio.h> // fprintf -#include <stdlib.h> // abort -#include <string.h> // strerror -#include <unistd.h> // getopt +#include <stdio.h> // fprintf +#include <stdlib.h> // abort +#include <string.h> // strerror #include <sys/stat.h> +#include <unistd.h> // getopt char *argv0 = "truncate"; @@ -28,8 +28,8 @@ usage() int main(int argc, char *argv[]) { - int open_flags = O_WRONLY | O_CREAT | O_NONBLOCK | O_LARGEFILE; - bool size_set = false; + int open_flags = O_WRONLY | O_CREAT | O_NONBLOCK | O_LARGEFILE; + bool size_set = false; struct truncation tr; char *ref_file = NULL; @@ -97,7 +97,10 @@ main(int argc, char *argv[]) struct stat ref_stats; if(stat(optarg, &ref_stats) < 0) { - fprintf(stderr, "truncate: Error: Couldn't get status for file '%s': %s\n", optarg, strerror(errno)); + fprintf(stderr, + "truncate: Error: Couldn't get status for file '%s': %s\n", + optarg, + strerror(errno)); return 1; } tr.op = OP_SET; diff --git a/lib/truncation.c b/lib/truncation.c @@ -7,9 +7,9 @@ #include <assert.h> #include <errno.h> -#include <stdio.h> // fprintf -#include <stdlib.h> // strtol -#include <string.h> // strerror +#include <stdio.h> // fprintf +#include <stdlib.h> // strtol +#include <string.h> // strerror #include <sys/stat.h> // fstat #include <unistd.h> // ftruncate @@ -24,7 +24,8 @@ apply_truncation(int fd, struct truncation tr, char *arg) assert(errno == 0); if(fstat(fd, &stats) < 0) { - fprintf(stderr, "truncate: Error: Failed to get status of file '%s': %s\n", arg, strerror(errno)); + fprintf( + stderr, "truncate: Error: Failed to get status of file '%s': %s\n", arg, strerror(errno)); return -1; } @@ -40,6 +41,7 @@ apply_truncation(int fd, struct truncation tr, char *arg) break; case OP_CHK_DOWN: q = stats.st_size / tr.size; + tr.size = tr.size * q; break; case OP_CHK_UP: @@ -54,7 +56,11 @@ apply_truncation(int fd, struct truncation tr, char *arg) if(ftruncate(fd, tr.size) < 0) { - fprintf(stderr, "truncate: Error: Failed to truncate '%s' to %ld: %s\n", arg, tr.size, strerror(errno)); + fprintf(stderr, + "truncate: Error: Failed to truncate '%s' to %ld: %s\n", + arg, + tr.size, + strerror(errno)); return -1; }