cat(1p): made simpler, added error handling
This commit is contained in:
parent
27a227e365
commit
b360b823ac
56
src/cat.c
56
src/cat.c
|
@ -30,14 +30,8 @@
|
|||
int main(int argc, char *argv[]) {
|
||||
bool u = false;
|
||||
int opt;
|
||||
int i = 1;
|
||||
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;
|
||||
while ((opt = getopt(argc, argv, "u")) != -1) {
|
||||
|
@ -52,14 +46,15 @@ int main(int argc, char *argv[]) {
|
|||
u = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
printf("Usage: %s (-u) file...\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
}
|
||||
argv += optind;
|
||||
argc -= optind;
|
||||
|
||||
for (int i = 0; i < argc; i++) {
|
||||
int byte = 0;
|
||||
for (i = optind; i < argc; i++) {
|
||||
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):
|
||||
*
|
||||
|
@ -71,23 +66,50 @@ int main(int argc, char *argv[]) {
|
|||
* but shall accept multiple occurrences of '-' as a file
|
||||
* operand.
|
||||
*/
|
||||
if (argv[i] == "-" || argc == 0) {
|
||||
if (argv[i] == "-" || argc == 1) {
|
||||
file = stdin;
|
||||
} else if ((file = fopen(argv[i], "r")) == NULL) {
|
||||
// TODO: Add error handling
|
||||
fputs(usage, stderr);
|
||||
switch (errno) {
|
||||
case EACCES:
|
||||
printf("%s: %s: Permission denied.\n", argv[0], argv[i]);
|
||||
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);
|
||||
|
||||
if (u) {
|
||||
putchar(byte);
|
||||
} 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); }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user