may work
This commit is contained in:
parent
d6909207df
commit
03b51b7b9b
@ -2,6 +2,6 @@ cat: cat.c
|
||||
$(CC) -g -o cat cat.c
|
||||
|
||||
clean:
|
||||
rm -f cat.c
|
||||
rm -f cat
|
||||
|
||||
.PHONY: clean
|
||||
|
31
cat/cat.c
31
cat/cat.c
@ -14,14 +14,12 @@ static char *default_argv[] = {
|
||||
"-",
|
||||
(char *)NULL
|
||||
};
|
||||
static char *stdout_name = "/dev/stdin";
|
||||
static char *stdin_name = "/dev/stdin";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int arguing;
|
||||
char *argv0;
|
||||
int c;
|
||||
extern int errno;
|
||||
extern int opterr;
|
||||
extern int optind;
|
||||
struct stat fi; /* info */
|
||||
char *fn; /* name */
|
||||
FILE *fo; /* object */
|
||||
@ -33,31 +31,27 @@ int main(int argc, char *argv[]){
|
||||
default_argv[0] = argv[0];
|
||||
argv = default_argv;
|
||||
}
|
||||
arguing = 1;
|
||||
opterr = 0;
|
||||
while(optind < argc){
|
||||
if(arguing && (c = getopt(argc, argv, "hu")) != -1)
|
||||
switch(c){
|
||||
|
||||
while((c = getopt(argc, argv, "hu")) != -1)
|
||||
switch(c){
|
||||
case 'u':
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
continue;
|
||||
case '-':
|
||||
arguing = 0;
|
||||
continue;
|
||||
case 'h': default: usage:
|
||||
fprintf(stderr, "Usage: %s (-h) (file...)\n",
|
||||
argv[0]);
|
||||
return EX_USAGE;
|
||||
}
|
||||
if(arguing && *argv[optind] == '-'){ /* "--" */
|
||||
arguing = 0;
|
||||
continue;
|
||||
}
|
||||
if(*argv[optind] == '-' && argv[optind][1] == '\0'){ /* "-" */
|
||||
argv0 = argv[0];
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
while(argc --> 0){
|
||||
if(argv[0][0] == '-' && argv[0][1] == '\0'){ /* "-" */
|
||||
fn = stdin_name;
|
||||
fo = stdin;
|
||||
}else{
|
||||
fn = argv[optind];
|
||||
fn = argv[0];
|
||||
if(stat(fn, &fi) == -1){
|
||||
switch(errno){
|
||||
case EFAULT: case ENOENT: case ENOTDIR:
|
||||
@ -105,7 +99,6 @@ int main(int argc, char *argv[]){
|
||||
|
||||
if(fo != stdin)
|
||||
fclose(fo);
|
||||
++optind;
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
|
Loading…
Reference in New Issue
Block a user