File: getopt.c

package info (click to toggle)
dnstracer 1.9-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 600 kB
  • ctags: 153
  • sloc: sh: 3,343; ansic: 1,364; makefile: 55
file content (231 lines) | stat: -rw-r--r-- 5,590 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// $Id: getopt.c,v 1.1 2002/01/25 00:38:18 mavetju Exp $
//
// Only used in the win32-version of dnstracer.
// Supplied by Mike Black <mblack@csihq.com>
//

/*
 *  Copyright (C) 1994 Arno Schaefer
 *
 *  AU: Auswertung der Kommandozeile, der POSIX-Version von getopt ()
 *      nachempfunden.
 *
 *  PO: ANSI C
 */
/*-----------------------------------------------------------------
 |
 |      FILE: getopt.c
 |
 |   PURPOSE: standard getopt() for platforms that don't have it. 
 |
 |
 |  ROUTINES:
 |            getopt()       parse args from command string
 |
 |
 |    AUTHOR:  Arno Schaefer
 |
 |      DATE:  1994
 |
 |  MODIFICATIONS:
 |  Revision 1.2  2001/11/05 20:56:04  dan
 |  Update to establish common header format
 |
 |
 |
 +---------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>

#include "getopt.h"


/* Globale Variablen */

char *optarg;
int optind = 1;
int opterr = 1;
int optopt;

static char *nextarg = NULL;


/* Funktion */

int getopt (int argc, char *argv[], char *optstring)

/*
 *  AU: Auswertung der Kommandozeile
 *
 *  VB: argc und argv sind die Parameter, die an main () uebergeben werden.
 *      optstring ist ein String, der die Zeichen enthaelt, die als
 *      Optionen erkannt werden. Wenn ein Zeichen von einem Doppelpunkt
 *      gefolgt wird, hat die Option ein Argument, das direkt auf das Zeichen
 *      folgt oder durch Space davon getrennt ist. Gueltige Optionszeichen
 *      sind alle druckbaren Zeichen ausser '?', ' ' und ':'.
 *
 *      optind ist der Index auf das naechste Element von argv[], das
 *      bearbeitet wird.
 *
 *      opterr ist ein Flag, das festlegt, ob bei Fehlern Fehlermeldungen
 *      ausgegeben werden.
 *
 *      optarg ist ein Zeiger auf das Argument, wenn eine Option ein
 *      Argument hat.
 *
 *      optopt enthaelt bei Fehlern das Optionszeichen, das den Fehler aus-
 *      geloest hat.
 *
 *  NB: Rueckgabewert ist das jeweils naechste Optionszeichen, oder -1 am
 *      Ende der Optionsliste.
 *
 *      Die Optionsliste ist zu Ende, wenn argv[optind] NULL ist, oder
 *      argv[optind] nicht mit '-' (oder '/') beginnt, oder argv[optind]
 *      ein einzelnes "-" ist. In diesem Fall wird optind nicht erhoeht.
 *      Das Ende der Optionsliste kann mit "--" erzwungen werden, dann ist
 *      argv[optind] das erste Argument nach "--".
 *
 *  FB: Ein '?' wird zurueckgegeben, wenn ein Optionszeichen nicht in
 *      optstring enthalten war oder ein ungueltiges Optionszeichen
 *      uebergeben wurde ('?' oder ':'). Ausserdem bei einem fehlenden
 *      Argument, wenn das erste Zeichen von optstring kein ':' ist.
 *
 *      Ein ':' wird zurueckgegeben bei einem fehlenden Argument, wenn
 *      das erste Zeichen von optstring ein ':' ist.
 */

{
        char *search;

        optarg = NULL;
        
        if (nextarg == NULL)
        {
                nextarg = argv[optind];

                if (nextarg == NULL)
                {
                        return (-1);
                }

#ifdef __MSDOS__
                if (*nextarg != '-' && *nextarg != '/')
#else
                if (*nextarg != '-')
#endif
                {
                        return (-1);
                }

                nextarg++;

        } /* if */

        optopt = *nextarg++;

        if (optopt == 0)
        {
                return (-1);
        }

        optind++;

        if (optopt == '-' && *nextarg == 0)
        {
                return (-1);
        }

        if (optopt == ':' || optopt == '?')
        {
                if (opterr)
                {
                        fprintf
                        (
                                stderr,
                                "%s: illegal option -- %c\n",
                                argv[0],
                                optopt
                        );
                }

                return ('?');

        } /* if */

        search = strchr (optstring, optopt);

        if (search == NULL)
        {
                if (opterr)
                {
                        fprintf
                        (
                                stderr,
                                "%s: illegal option -- %c\n",
                                argv[0],
                                optopt
                        );
                }

                return ('?');

        } /* if */

        if (*nextarg == 0)
        {
                nextarg = NULL;
        }

        if (search[1] != ':')
        {
                if (nextarg != NULL)
                {
                        optind--;
                }

                return (optopt);
        }

        if (nextarg != NULL)
        {
                optarg = nextarg;
                nextarg = NULL;
                return (optopt);
        }

        optarg = argv[optind];

        if (optind == argc)
        {
                if (opterr)
                {
                        fprintf
                        (
                                stderr,
                                "%s: option requires an argument -- %c\n",
                                argv[0],
                                optopt
                        );

                } /* if */

                if (optstring[0] == ':')
                {
                        return (':');
                }
                else
                {
                        return ('?');
                }

        } /* if */

        else
        {
                optind++;
        }

        return (optopt);

} /* end getopt() */