17c478bd9Sstevel@tonic-gate /*
2*5d54f3d8Smuffin * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
3*5d54f3d8Smuffin * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
5*5d54f3d8Smuffin
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * Copyright (c) 1987 Regents of the University of California.
87c478bd9Sstevel@tonic-gate * All rights reserved.
97c478bd9Sstevel@tonic-gate *
107c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate * by the University of California, Berkeley. The name of the
167c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate
23*5d54f3d8Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
24*5d54f3d8Smuffin
25*5d54f3d8Smuffin
267c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate * get option letter from argument vector
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate /* See lib/libc/gen/common/optind.c for next 3 definitions. */
357c478bd9Sstevel@tonic-gate extern char *optarg; /* argument associated with option */
367c478bd9Sstevel@tonic-gate extern int opterr; /* if error message should be printed */
377c478bd9Sstevel@tonic-gate extern int optind; /* index into parent argv vector */
387c478bd9Sstevel@tonic-gate int optopt; /* character checked for validity */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #define BADCH (int)'?'
427c478bd9Sstevel@tonic-gate #define EMSG ""
437c478bd9Sstevel@tonic-gate
44*5d54f3d8Smuffin int
getopt(int nargc,char ** nargv,char * ostr)45*5d54f3d8Smuffin getopt(int nargc, char **nargv, char *ostr)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate static char *place = EMSG; /* option letter processing */
48*5d54f3d8Smuffin char *oli; /* option letter list index */
497c478bd9Sstevel@tonic-gate char *p;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (!*place) { /* update scanning pointer */
527c478bd9Sstevel@tonic-gate if (optind >= nargc || *(place = nargv[optind]) != '-') {
537c478bd9Sstevel@tonic-gate place = EMSG;
547c478bd9Sstevel@tonic-gate return (EOF);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate if (place[1] && *++place == '-') { /* found "--" */
577c478bd9Sstevel@tonic-gate ++optind;
587c478bd9Sstevel@tonic-gate place = EMSG;
597c478bd9Sstevel@tonic-gate return (EOF);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate } /* option letter okay? */
627c478bd9Sstevel@tonic-gate if ((optopt = (int)*place++) == (int)':' ||
637c478bd9Sstevel@tonic-gate !(oli = strchr(ostr, optopt))) {
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * For backwards compatibility: don't treat '-' as an
677c478bd9Sstevel@tonic-gate * option letter unless caller explicitly asked for it.
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate if (optopt == (int)'-')
707c478bd9Sstevel@tonic-gate return (EOF);
717c478bd9Sstevel@tonic-gate if (!*place)
727c478bd9Sstevel@tonic-gate ++optind;
737c478bd9Sstevel@tonic-gate if (opterr) {
747c478bd9Sstevel@tonic-gate if (!(p = strrchr(*nargv, '/')))
757c478bd9Sstevel@tonic-gate p = *nargv;
767c478bd9Sstevel@tonic-gate else
777c478bd9Sstevel@tonic-gate ++p;
787c478bd9Sstevel@tonic-gate (void)fprintf(stderr, "%s: illegal option -- %c\n",
797c478bd9Sstevel@tonic-gate p, optopt);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate return (BADCH);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate if (*++oli != ':') { /* don't need argument */
847c478bd9Sstevel@tonic-gate optarg = NULL;
857c478bd9Sstevel@tonic-gate if (!*place)
867c478bd9Sstevel@tonic-gate ++optind;
877c478bd9Sstevel@tonic-gate } else { /* need an argument */
887c478bd9Sstevel@tonic-gate if (*place) /* no white space */
897c478bd9Sstevel@tonic-gate optarg = place;
907c478bd9Sstevel@tonic-gate else if (nargc <= ++optind) { /* no arg */
917c478bd9Sstevel@tonic-gate place = EMSG;
927c478bd9Sstevel@tonic-gate if (!(p = strrchr(*nargv, '/')))
937c478bd9Sstevel@tonic-gate p = *nargv;
947c478bd9Sstevel@tonic-gate else
957c478bd9Sstevel@tonic-gate ++p;
967c478bd9Sstevel@tonic-gate if (opterr)
977c478bd9Sstevel@tonic-gate (void)fprintf(stderr,
987c478bd9Sstevel@tonic-gate "%s: option requires an argument -- %c\n",
997c478bd9Sstevel@tonic-gate p, optopt);
1007c478bd9Sstevel@tonic-gate return (BADCH);
1017c478bd9Sstevel@tonic-gate } else /* white space */
1027c478bd9Sstevel@tonic-gate optarg = nargv[optind];
1037c478bd9Sstevel@tonic-gate place = EMSG;
1047c478bd9Sstevel@tonic-gate ++optind;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate return (optopt); /* dump back option letter */
1077c478bd9Sstevel@tonic-gate }
108