[BACK]Return to getopt CVS log [TXT][DIR] Up to [local] / src / share / misc

File: [local] / src / share / misc / Attic / getopt (download)

Revision 1.8, Wed Feb 1 09:27:28 2006 UTC (18 years, 3 months ago) by otto
Branch: MAIN
CVS Tags: OPENBSD_6_0_BASE, OPENBSD_6_0, OPENBSD_5_9_BASE, OPENBSD_5_9, OPENBSD_5_8_BASE, OPENBSD_5_8, OPENBSD_5_7_BASE, OPENBSD_5_7, OPENBSD_5_6_BASE, OPENBSD_5_6, OPENBSD_5_5_BASE, OPENBSD_5_5, OPENBSD_5_4_BASE, OPENBSD_5_4, OPENBSD_5_3_BASE, OPENBSD_5_3, OPENBSD_5_2_BASE, OPENBSD_5_2, OPENBSD_5_1_BASE, OPENBSD_5_1, OPENBSD_5_0_BASE, OPENBSD_5_0, OPENBSD_4_9_BASE, OPENBSD_4_9, OPENBSD_4_8_BASE, OPENBSD_4_8, OPENBSD_4_7_BASE, OPENBSD_4_7, OPENBSD_4_6_BASE, OPENBSD_4_6, OPENBSD_4_5_BASE, OPENBSD_4_5, OPENBSD_4_4_BASE, OPENBSD_4_4, OPENBSD_4_3_BASE, OPENBSD_4_3, OPENBSD_4_2_BASE, OPENBSD_4_2, OPENBSD_4_1_BASE, OPENBSD_4_1, OPENBSD_4_0_BASE, OPENBSD_4_0, OPENBSD_3_9_BASE, OPENBSD_3_9
Changes since 1.7: +18 -8 lines

Actually working example, from Ray Lai in PR 5000; with a twist (also
fix usage()) from me.

/*	$OpenBSD: getopt,v 1.8 2006/02/01 09:27:28 otto Exp $	*/
/*
 * Main/getopt(3) fragment.
 *
 *	from: @(#)getopt	5.3 (Berkeley) 3/28/94
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

__dead void usage(void);

int
main(int argc, char *argv[])
{
	int bflag, ch;
	char *file;

	bflag = 0;
	file = NULL;
	while ((ch = getopt(argc, argv, "bf:")) != -1)
		switch (ch) {
		case 'b':
			bflag = 1;
			break;
		case 'f':
			file = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	argc -= optind;
	argv += optind;

	return (0);
}

void
usage(void)
{
	extern char *__progname;

	(void)fprintf(stderr, "usage: %s [-b] [-f file]\n", __progname);
	exit(1);
}