cat(1p), true(1p), false(1p): added source files & Makefile

This commit is contained in:
Emma Tebibyte 2023-07-23 21:54:43 -06:00 committed by Emma Tebibyte
parent 3b58aa764a
commit bb099036fb
4 changed files with 169 additions and 0 deletions

28
Makefile Normal file
View File

@ -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)/

99
src/cat.c Normal file
View File

@ -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 <fcntl.h>
#include <sysexits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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;
}

21
src/false.c Normal file
View File

@ -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; }

21
src/true.c Normal file
View File

@ -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() {}