From e4c74e9d1c97feccb0c5337baceab4fc4aec592c Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 24 Jan 2022 21:17:08 +0100 Subject: Merged two random sources into this repository --- .gitignore | 3 +++ Makefile | 10 ++++++++++ cat.c | 41 +++++++++++++++++++++++++++++++++++++++++ fallocate.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ panic.c | 14 ++++++++++++++ panic.h | 6 ++++++ 6 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cat.c create mode 100644 fallocate.c create mode 100644 panic.c create mode 100644 panic.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b0dc11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +cat +fallocate diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2027646 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +CFLAGS+=-std=c11 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L + +all: cat fallocate + +cat: cat.o panic.o + +clean: + rm -f *.o cat fallocate + +.PHONY: all clean diff --git a/cat.c b/cat.c new file mode 100644 index 0000000..6f8ea55 --- /dev/null +++ b/cat.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "panic.h" + + +void cat(int fd, char * name) +{ + char buffer[4096]; + size_t length; + while (0 < (length=read(fd, buffer, sizeof(buffer)))) + if (length != write(1, buffer, length)) + panic("write: %s: %s", name, strerror(errno)); + if (0 > length) + panic("read: %s: %s", name, strerror(errno)); +} + + +int main(int argc, char * argv[]) +{ + int i; + int fd; + if (1 == argc) + cat(0, ""); + else for (i = 1; i < argc; ++i) + { + fd = open(argv[i], O_RDONLY); + if (0 > fd) + panic("open: %s: %s", argv[i], strerror(errno)); + else + { + cat(fd, argv[i]); + close(fd); + } + } +} diff --git a/fallocate.c b/fallocate.c new file mode 100644 index 0000000..910d191 --- /dev/null +++ b/fallocate.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + + +#define EXIT_BAD_OPTION 2 + + +void usage(int fd, char * progname) +{ + dprintf(fd, "Usage: %s [-o offset] -l length name\n", progname); +} + + +int try_fallocate(char * filename, int offset, int length) +{ + int fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644); + if (-1 == fd) return -1; + int res = posix_fallocate(fd, offset, length); + if (-1 == res) return -1; + res = close(fd); + if (-1 == res) return -1; + return 0; +} + + +int main(int argc, char * argv[]) +{ + int opt; + int offset = 0; + int length = 0; + char * filename; + while (-1 != (opt = getopt(argc, argv, "o:l:"))) { + switch (opt) { + case 'o': + offset = atoi(optarg); + break; + case 'l': + length = atoi(optarg); + break; + default: + usage(2, argv[0]); + exit(EXIT_BAD_OPTION); + } + } + if (0 >= length || argc <= optind) { + usage(2, argv[0]); + exit(EXIT_BAD_OPTION); + } + filename = argv[optind]; + int res = try_fallocate(filename, offset, length); + if (-1 == res) { + dprintf(2, "%s: %s: %s\n", argv[0], filename, strerror(errno)); + exit(EXIT_FAILURE); + } +} diff --git a/panic.c b/panic.c new file mode 100644 index 0000000..ef4475e --- /dev/null +++ b/panic.c @@ -0,0 +1,14 @@ +#include +#include +#include +#include + + +noreturn void panic(char * fmt, ...) +{ + va_list args; + va_start(args, fmt); + vdprintf(2, fmt, args); + va_end(args); + exit(1); +} diff --git a/panic.h b/panic.h new file mode 100644 index 0000000..dd0d1a8 --- /dev/null +++ b/panic.h @@ -0,0 +1,6 @@ +#pragma once + +#include + + +noreturn void panic(char * fmt, ...); -- cgit v1.1