1
0
forked from bonsai/harakit

cat(1p): made simpler, added error handling

This commit is contained in:
Emma Tebibyte 2023-08-10 15:24:07 -06:00
parent 27a227e365
commit b360b823ac
Signed by untrusted user: emma
GPG Key ID: 6D661C738815E7DD

View File

@ -30,14 +30,8 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool u = false; bool u = false;
int opt; int opt;
int i = 1;
FILE *file; FILE *file;
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 ) {}
extern int optind; extern int optind;
while ((opt = getopt(argc, argv, "u")) != -1) { while ((opt = getopt(argc, argv, "u")) != -1) {
@ -52,14 +46,15 @@ int main(int argc, char *argv[]) {
u = true; u = true;
break; break;
default: default:
break; printf("Usage: %s (-u) file...\n", argv[0]);
return EX_USAGE;
} }
} }
argv += optind;
argc -= optind;
for (int i = 0; i < argc; i++) { for (i = optind; i < argc; i++) {
int byte = 0; int byte = 0; // variable for storing bytes as they are read
char buf[4096]; // buffer for buffered
int p = 0; // index counter for bytes in buffered reading
/* /*
* From cat(1p): * From cat(1p):
* *
@ -71,23 +66,50 @@ int main(int argc, char *argv[]) {
* but shall accept multiple occurrences of '-' as a file * but shall accept multiple occurrences of '-' as a file
* operand. * operand.
*/ */
if (argv[i] == "-" || argc == 0) { if (argv[i] == "-" || argc == 1) {
file = stdin; file = stdin;
} else if ((file = fopen(argv[i], "r")) == NULL) { } else if ((file = fopen(argv[i], "r")) == NULL) {
// TODO: Add error handling switch (errno) {
fputs(usage, stderr); case EACCES:
printf("%s: %s: Permission denied.\n", argv[0], argv[i]);
return EX_NOINPUT; return EX_NOINPUT;
case EISDIR:
printf("%s: %s: Is a directory.\n", argv[0], argv[i]);
return EX_NOINPUT;
case ELOOP:
printf("%s: %s: Is a symbolic link loop.\n", argv[0], argv[i]);
return EX_UNAVAILABLE;
case EMFILE:
printf("%s: Internal error.\n", argv[0]);
return EX_SOFTWARE;
case ENOENT: case ENOTDIR: case ENXIO:
printf("%s: %s: No such file or directory.\n", argv[0], argv[i]);
return EX_NOINPUT;
default:
printf("%s: Unknown error.\n", argv[0]);
return EX_UNAVAILABLE;
}
} }
for (char *buf = calloc(4096, 1); byte != EOF; fputs(buf, stdout)) { while (byte != EOF) {
byte = fgetc(file); byte = fgetc(file);
if (u) { if (u) {
putchar(byte); putchar(byte);
} else { } else {
snprintf(buf, 4096, "%c", byte); if (p > sizeof(buf)) {
fputs(buf, stdout);
p = 0;
}
buf[p] = byte;
p += 1;
} }
} }
fputs(buf, stdout);
fflush(stdout);
if (file != stdin) { fclose(file); } if (file != stdin) { fclose(file); }
} }