handle --

This commit is contained in:
Connor Lane Smith 2011-05-24 11:05:36 +01:00
parent 8c76381e91
commit 4c6c379812
3 changed files with 13 additions and 9 deletions

14
cat.c
View File

@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "util.h" #include "util.h"
static void cat(FILE *, const char *); static void cat(FILE *, const char *);
@ -8,15 +9,16 @@ static void cat(FILE *, const char *);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i;
FILE *fp; FILE *fp;
if(argc == 1) if(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
if(optind == argc)
cat(stdin, "<stdin>"); cat(stdin, "<stdin>");
else for(i = 1; i < argc; i++) { else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[i], "r"))) if(!(fp = fopen(argv[optind], "r")))
eprintf("fopen %s:", argv[i]); eprintf("fopen %s:", argv[optind]);
cat(fp, argv[i]); cat(fp, argv[optind]);
fclose(fp); fclose(fp);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

2
grep.c
View File

@ -36,7 +36,7 @@ main(int argc, char *argv[])
vflag = true; vflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); exit(2);
} }
if(optind == argc) { if(optind == argc) {
fprintf(stderr, "usage: %s [-cilnqv] pattern [files...]\n", argv[0]); fprintf(stderr, "usage: %s [-cilnqv] pattern [files...]\n", argv[0]);

View File

@ -8,10 +8,12 @@ main(int argc, char *argv[])
{ {
unsigned int seconds; unsigned int seconds;
if(argc != 2) if(getopt(argc, argv, "") != -1)
exit(EXIT_FAILURE);
if(optind != argc-1)
eprintf("usage: %s seconds\n", argv[0]); eprintf("usage: %s seconds\n", argv[0]);
seconds = atoi(argv[1]); seconds = atoi(argv[optind]);
while((seconds = sleep(seconds)) > 0) while((seconds = sleep(seconds)) > 0)
; ;
return EXIT_SUCCESS; return EXIT_SUCCESS;