Browse Source

tidy up examples

- use err() for error handling
- add lint hints
- spacing nits and missing braces
ok otto
OPENBSD_3_9
jaredy 19 years ago
parent
commit
04a4c1a148
3 changed files with 16 additions and 17 deletions
  1. +4
    -7
      src/lib/libc/stdlib/getopt.3
  2. +5
    -5
      src/lib/libc/stdlib/getopt_long.3
  3. +7
    -5
      src/lib/libc/stdlib/getsubopt.3

+ 4
- 7
src/lib/libc/stdlib/getopt.3 View File

@ -25,7 +25,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" $OpenBSD: getopt.3,v 1.36 2005/07/26 13:38:41 jmc Exp $
.\" $OpenBSD: getopt.3,v 1.37 2005/10/11 01:23:41 jaredy Exp $
.\" .\"
.Dd December 17, 2002 .Dd December 17, 2002
.Dt GETOPT 3 .Dt GETOPT 3
@ -187,15 +187,12 @@ while ((ch = getopt(argc, argv, "bf:")) != -1) {
bflag = 1; bflag = 1;
break; break;
case 'f': case 'f':
if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
(void)fprintf(stderr,
"myname: %s: %s\en", optarg, strerror(errno));
exit(1);
}
if ((fd = open(optarg, O_RDONLY, 0)) == -1)
err(1, "%s", optarg);
break; break;
case '?':
default: default:
usage(); usage();
/* NOTREACHED */
} }
} }
argc -= optind; argc -= optind;


+ 5
- 5
src/lib/libc/stdlib/getopt_long.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: getopt_long.3,v 1.11 2005/07/26 04:17:44 jaredy Exp $
.\" $OpenBSD: getopt_long.3,v 1.12 2005/10/11 01:23:41 jaredy Exp $
.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ .\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $
.\" .\"
.\" Copyright (c) 1988, 1991, 1993 .\" Copyright (c) 1988, 1991, 1993
@ -223,14 +223,14 @@ while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
err(1, "unable to open %s", optarg); err(1, "unable to open %s", optarg);
break; break;
case 0: case 0:
if (daggerset) {
fprintf(stderr,"Buffy will use her dagger to "
if (daggerset)
fprintf(stderr, "Buffy will use her dagger to "
"apply fluoride to dracula's teeth\en"); "apply fluoride to dracula's teeth\en");
}
break; break;
default: default:
usage(); usage();
}
/* NOTREACHED */
}
argc -= optind; argc -= optind;
argv += optind; argv += optind;
.Ed .Ed


+ 7
- 5
src/lib/libc/stdlib/getsubopt.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: getsubopt.3,v 1.10 2005/07/26 04:20:23 jaredy Exp $
.\" $OpenBSD: getsubopt.3,v 1.11 2005/10/11 01:23:41 jaredy Exp $
.\" .\"
.\" Copyright (c) 1990, 1991, 1993 .\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved. .\" The Regents of the University of California. All rights reserved.
@ -104,15 +104,15 @@ char *tokens[] = {
extern char *optarg, *suboptarg; extern char *optarg, *suboptarg;
char *options, *value; char *options, *value;
while ((ch = getopt(argc, argv, "ab:")) != \-1) {
switch(ch) {
while ((ch = getopt(argc, argv, "ab:")) != -1) {
switch (ch) {
case 'a': case 'a':
/* process ``a'' option */ /* process ``a'' option */
break; break;
case 'b': case 'b':
options = optarg; options = optarg;
while (*options) { while (*options) {
switch(getsubopt(&options, tokens, &value)) {
switch (getsubopt(&options, tokens, &value)) {
case ONE: case ONE:
/* process ``one'' sub option */ /* process ``one'' sub option */
break; break;
@ -122,16 +122,18 @@ while ((ch = getopt(argc, argv, "ab:")) != \-1) {
error("no value for two"); error("no value for two");
i = atoi(value); i = atoi(value);
break; break;
case \-1:
case -1:
if (suboptarg) if (suboptarg)
error("illegal sub option %s", error("illegal sub option %s",
suboptarg); suboptarg);
else else
error("missing sub option"); error("missing sub option");
break; break;
}
} }
break; break;
} }
}
.Ed .Ed
.Sh SEE ALSO .Sh SEE ALSO
.Xr getopt 3 , .Xr getopt 3 ,


Loading…
Cancel
Save