~ubuntu-branches/ubuntu/breezy/sox/breezy

« back to all changes in this revision

Viewing changes to getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Guenter Geiger (Debian/GNU)
  • Date: 2005-03-21 13:08:29 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050321130829-ycexf5si46wn9p25
Tags: 12.17.7-2
Moved ststdint.h to -dev package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Date: Tue, 25 Dec 84 19:20:50 EST
3
 
From: Keith Bostic <harvard!seismo!keith>
4
 
To: genrad!sources
5
 
Subject: public domain getopt(3)
6
 
 
7
 
There have recently been several requests for a public
8
 
domain version of getopt(3), recently.  Thought this
9
 
might be worth reposting.
10
 
 
11
 
                Keith Bostic
12
 
                        ARPA: keith@seismo 
13
 
                        UUCP: seismo!keith
14
 
 
15
 
======================================================================
16
 
In April of this year, Henry Spencer (utzoo!henry) released a public
17
 
domain version of getopt (USG, getopt(3)).  Well, I've been trying to
18
 
port some USG dependent software and it didn't seem to work.  The problem
19
 
ended up being that the USG version of getopt has some external variables
20
 
that aren't mentioned in the documentation.  Anyway, to fix these problems,
21
 
I rewrote the public version of getopt.  It has the following advantages:
22
 
 
23
 
        -- it includes those "unknown" variables
24
 
        -- it's smaller/faster 'cause it doesn't use the formatted
25
 
                output conversion routines in section 3 of the UNIX manual.
26
 
        -- the error messages are the same as S5's.
27
 
        -- it has the same side-effects that S5's has.
28
 
        -- the posted bug on how the error messages are flushed has been
29
 
                implemented.  (posting by Tony Hansen; pegasus!hansen)
30
 
 
31
 
I won't post the man pages since Henry already did; a special note,
32
 
it's not documented in the S5 manual that the options ':' and '?' are
33
 
illegal.  It should be obvious, but I thought I'd mention it...
34
 
This software was derived from binaries of S5 and the S5 man page, and is
35
 
(I think?) totally (I'm pretty sure?) compatible with S5 and backward
36
 
compatible to Henry's version.
37
 
 
38
 
                Keith Bostic
39
 
                        ARPA: keith@seismo 
40
 
                        UUCP: seismo!keith
41
 
 
42
 
*UNIX is a trademark of Bell Laboratories
43
 
 
44
 
.. cut along the dotted line .........................................
45
 
*/
46
 
 
47
 
#include "st_i.h"
48
 
 
49
 
#ifndef HAVE_GETOPT
50
 
#include <stdio.h>
51
 
 
52
 
#include <string.h>
53
 
 
54
 
/*
55
 
 * get option letter from argument vector
56
 
 */
57
 
int     optind = 1,             /* index into parent argv vector */
58
 
        optopt;                 /* character checked for validity */
59
 
char    *optarg;                /* argument associated with option */
60
 
 
61
 
#define BADCH   (int)'?'
62
 
#define EMSG    ""
63
 
#define tell(s) fputs(*nargv,stderr);fputs(s,stderr); \
64
 
                fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
65
 
 
66
 
int getopt(int nargc, char **nargv, char *ostr)
67
 
{
68
 
        static char     *place = EMSG;  /* option letter processing */
69
 
        static char     *lastostr = (char *) 0;
70
 
        register char   *oli;           /* option letter list index */
71
 
 
72
 
        /* LANCE PATCH: dynamic reinitialization */
73
 
        if (ostr != lastostr) {
74
 
                lastostr = ostr;
75
 
                place = EMSG;
76
 
        }
77
 
        if(!*place) {                   /* update scanning pointer */
78
 
                if((optind >= nargc) || (*(place = nargv[optind]) != '-')
79
 
                                || ! *++place) {
80
 
                        place = EMSG;
81
 
                        return(EOF);
82
 
                }
83
 
                if (*place == '-') {    /* found "--" */
84
 
                        ++optind;
85
 
                        return(EOF);
86
 
                }
87
 
        }                               /* option letter okay? */
88
 
        if ((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt))) {
89
 
                if(!*place) ++optind;
90
 
                tell(": illegal option -- ");
91
 
        }
92
 
        if (*++oli != ':') {            /* don't need argument */
93
 
                optarg = NULL;
94
 
                if (!*place) ++optind;
95
 
        }
96
 
        else {                          /* need an argument */
97
 
                if (*place) optarg = place;     /* no white space */
98
 
                else if (nargc <= ++optind) {   /* no arg */
99
 
                        place = EMSG;
100
 
                        tell(": option requires an argument -- ");
101
 
                }
102
 
                else optarg = nargv[optind];    /* white space */
103
 
                place = EMSG;
104
 
                ++optind;
105
 
        }
106
 
        return(optopt);                 /* dump back option letter */
107
 
}
108
 
 
109
 
#endif