catrm

'cat' a file, then optionally 'rm' it. I just felt like writing C.
Log | Files | Refs | Atom | README

commit d183304f9cb7af48482aa6e3c5626a5c7a0aa9de
Author: GeaRSiX <gearsix@tuta.io>
Date:   Tue,  8 Jun 2021 10:41:52 +0100

tmp commit; tidyup later

Diffstat:
Acat.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Acatrm | 0
Acatrm.h | 18++++++++++++++++++
Arm.c | 44++++++++++++++++++++++++++++++++++++++++++++
Atest | 0
Atest_cat.old | 41+++++++++++++++++++++++++++++++++++++++++
Atest_rm.c | 11+++++++++++
7 files changed, 164 insertions(+), 0 deletions(-)

diff --git a/cat.c b/cat.c @@ -0,0 +1,50 @@ +/** + * TODO +**/ + +#include "catrm.h" + +int cat(FILE *dst, FILE *src) +{ + int n = -1; + errno = 0; + + if (dst == NULL) dst = stdout; + if (src == NULL) { + errno = EBADF; + goto EXIT; + } else fseek(src, 0, SEEK_END); + + if (errno != 0) goto EXIT; + + if (ftell(src) < 1) + return EXIT_SUCCESS; + rewind(src); + + int br = -1, bw = -1; + char buf[BUFSIZ]; + do { + if (br > -1 && br != BUFSIZ) { + n = -1; + errno = EIO; + perror("invalid num. bytes read from source file"); + goto EXIT; + } + br = fread(buf, sizeof(char), BUFSIZ, src); + if (br > 0) { + bw = fwrite(buf, sizeof(char), br, dst); + if (bw != br) { + n = -1; + errno = EIO; + perror("invalid num. bytes written to destination file"); + goto EXIT; + } + n += bw; + } else { + n = 0; + } + } while (!feof(src)); + +EXIT: + return n; +} diff --git a/catrm b/catrm Binary files differ. diff --git a/catrm.h b/catrm.h @@ -0,0 +1,18 @@ +/** + * TODO +**/ + +#ifndef CATRM +#define CATRM + +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> + +#define OPTS_NONE 0x00 +#define OPTS_INTERACTIVE 0x01 + +int cat(FILE *dst, FILE *src); +int rm(const char *file, const int opts); + +#endif diff --git a/rm.c b/rm.c @@ -0,0 +1,44 @@ +/** + * +**/ + +#include "catrm.h" + +int prompt(const char *fpath); + +int rm(const char *fpath, const int opts) +{ + int n = 0; + + if (opts | OPTS_INTERACTIVE) + prompt(fpath); + + return n; +} + +int prompt(const char *fpath) +{ + int ret = 0; + char *answer = malloc(4); + FILE *f = NULL; + + while (ret == 0) { + printf("remove file '%s' ([y]es/[n]o/[c]at)? ", fpath); + fgets(answer, 4, stdin); + if (ferror(stdin)) { + perror("failed to read input"); + break; + } + switch (answer[0]) { + case 'y': ret = 'y'; break; + case 'n': ret = 'n'; break; + case 'c': + f = fopen(fpath, "r"); + if (f == NULL) + perror("failed to open file"); + if (cat(stdout, f) < 0) + perror("cat failed"); + break; + } + } +} diff --git a/test b/test diff --git a/test_cat.old b/test_cat.old @@ -0,0 +1,41 @@ +/** + * TODO +**/ + +#include "catrm.h" + +int main(int argc, char *argv[]) +{ + FILE *dst = fopen(argv[1], "a"); + if (dst == NULL) { + perror("failed to open dst"); + } + + while (--argc > 0) { +/* + FILE *src = fopen(argv[argc], "r"); + if (src == NULL) { + perror("failed to open src"); + return EXIT_FAILURE; + } + + if (cat(dst, src) < 0 || cat(NULL, src) < 0) { + perror("cat failed"); + return EXIT_FAILURE; + } + + if (fclose(src) == EOF) { + perror("failed to close src"); + return EXIT_FAILURE; + } +*/ + rm(argv[argc], OPTS_INTERACTIVE); + } + + if (fclose(dst) == EOF) { + perror("failed to close dst"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/test_rm.c b/test_rm.c @@ -0,0 +1,11 @@ +/** + * TODO +**/ + +#include "catrm.h" + +int main(int argc, char *argv[]) +{ + while (--argc > 0) + rm(argv[argc], OPTS_INTERACTIVE); +}