Hi,
This brings /usr/share/misc/getopt in sync with the example in getopt(3).
ok?
Index: getopt
===================================================================
RCS file: /cvs/src/share/misc/getopt,v
retrieving revision 1.8
diff -u -p -r1.8 getopt
--- getopt 1 Feb 2006 09:27:28 -0000 1.8
+++ getopt 4 Sep 2016 17:43:54 -0000
@@ -5,6 +5,8 @@
* from: @(#)getopt 5.3 (Berkeley) 3/28/94
*/
+#include <err.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -14,23 +16,22 @@ __dead void usage(void);
int
main(int argc, char *argv[])
{
- int bflag, ch;
- char *file;
+ int bflag, ch, fd;
bflag = 0;
- file = NULL;
- while ((ch = getopt(argc, argv, "bf:")) != -1)
+ while ((ch = getopt(argc, argv, "bf:")) != -1) {
switch (ch) {
case 'b':
bflag = 1;
break;
case 'f':
- file = optarg;
+ if ((fd = open(optarg, O_RDONLY, 0)) == -1)
+ err(1, "%s", optarg);
break;
default:
usage();
- /* NOTREACHED */
}
+ }
argc -= optind;
argv += optind;