From bb099036fbbecf718e4eb8333ffcb39049622cc4 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 23 Jul 2023 21:54:43 -0600 Subject: [PATCH] cat(1p), true(1p), false(1p): added source files & Makefile --- Makefile | 28 +++++++++++++++ src/cat.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/false.c | 21 ++++++++++++ src/true.c | 21 ++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 Makefile create mode 100644 src/cat.c create mode 100644 src/false.c create mode 100644 src/true.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7c1a847 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +# Copyright (c) 2023 YAC +# SPDX-License-Identifier: FSFAP +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice and this +# notice are preserved. This file is offered as-is, without any warranty. + +.POSIX: + +PREFIX=/usr/local/bin + +build: build_dir cat false true + +cat: build_dir + cc -o build/cat src/cat.c + +false: build_dir + cc -o build/false src/false.c + +true: build_dir + cc -o build/true src/true.c + +build_dir: + mkdir -p build + +install: build + mkdir -p $(PREFIX) + cp -f build/* $(PREFIX)/ diff --git a/src/cat.c b/src/cat.c new file mode 100644 index 0000000..74df7d5 --- /dev/null +++ b/src/cat.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2023 YAC + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This file is part of YAC coreutils. + * + * YAC coreutils is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * YAC coreutils is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + int i = 1; + bool u = false; + + char *usage_text = "(-u) [file...]"; + int buf_size = strlen(argv[0]) + strlen("Usage: ") + strlen(usage_text) + 3; + char *usage = calloc(buf_size, buf_size); + + if (( + snprintf(usage, buf_size, "Usage: %s (-u) [file...]\n", argv[0]) + ) < 0 ) {} + + int opt; + + while ((opt = getopt(argc, argv, "u")) != -1) { + switch(opt) { + /* + * From cat(1p): + * + * -u Write bytes from the input file to the standard output + * without delay as each is read. + */ + case 'u': + i = 2; + u = true; + break; + default: + break; + } + } + + FILE *file; + + for (; i <= (argc - 1); i++) { + /* + * From cat(1p): + * + * file A pathname of an input file. If no file operands are + * specified, the standard input shall be used. If a file is + * '-', the cat utility shall read from the standard input at + * that point in the sequence. The cat utility shall not close + * and reopen standard input when it is referenced in this way, + * but shall accept multiple occurrences of '-' as a file + * operand. + */ + printf("argv[%d]: %s\n", i, argv[1]); + + if (argc == 1 || argv[i] == "-") { file = fdopen(1, "r"); } + else if ((file = fopen(argv[i], O_RDONLY)) == NULL) { + fputs(usage, stderr); + return EX_NOINPUT; + } + + int byte = 0; + + if (u) { + while (byte != EOF) { + byte = fgetc(file); + putchar(byte); + } + } else { + char *buf = calloc(4096, 1); + } + if (fclose(file) == -1) { + fprintf(stderr, "%s: %s: Error closing file.\n", argv[0], argv[i]); + return EX_OSERR; + } + } + + return EX_OK; +} diff --git a/src/false.c b/src/false.c new file mode 100644 index 0000000..d255a9a --- /dev/null +++ b/src/false.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 YAC + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This file is part of YAC coreutils. + * + * YAC coreutils is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * YAC coreutils is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +int main() { return 1; } diff --git a/src/true.c b/src/true.c new file mode 100644 index 0000000..2791766 --- /dev/null +++ b/src/true.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 YAC + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This file is part of YAC coreutils. + * + * YAC coreutils is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * YAC coreutils is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +int main() {}