forked from bonsai/harakit
cat(1p): refactor to get no args working
This commit is contained in:
parent
da2ebdb1e1
commit
b57a7179a5
133
src/cat.c
133
src/cat.c
@ -27,68 +27,7 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool u = false;
|
||||
int opt;
|
||||
|
||||
extern int optind;
|
||||
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':
|
||||
u = true;
|
||||
break;
|
||||
default:
|
||||
printf("Usage: %s (-u) file...\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
FILE *file;
|
||||
|
||||
for (i = optind; i < argc; 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.
|
||||
*/
|
||||
if (argv[i] == "-" || argc == 1) {
|
||||
file = stdin;
|
||||
} else if ((file = fopen(argv[i], "r")) == NULL) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void cat(FILE *file, bool u) {
|
||||
int byte = 0; /* variable for storing bytes as they are read */
|
||||
int p = 0; /* index counter for bytes in buffered reading */
|
||||
char buf[4096]; /* buffer for buffered reading */
|
||||
@ -115,5 +54,75 @@ int main(int argc, char *argv[]) {
|
||||
if (file != stdin) { fclose(file); }
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool u = false;
|
||||
int opt;
|
||||
int i;
|
||||
|
||||
extern int optind;
|
||||
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':
|
||||
u = true;
|
||||
break;
|
||||
default:
|
||||
printf("Usage: %s (-u) file...\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
if (optind == argc) {
|
||||
cat(stdin, u);
|
||||
}
|
||||
|
||||
FILE *file;
|
||||
for (i = optind; i < argc; i++) {
|
||||
if (argv[i][0] == '-' && argv[i][1] != '\0') {
|
||||
continue;
|
||||
} if (strcmp(argv[i], "-") == 0) {
|
||||
file = stdin;
|
||||
} else if ((file = fopen(argv[i], "r")) == NULL) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
cat(file, u);
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user